Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
How can I Collapse/Expand Multiple Divs in ngx-virtual-scroll?

https://stackblitz.com/edit/angular-ngx-virtual-scroller-byykpn

Here is what I have done so far, now I need to Expand/Collapse all divs which is after the group header.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You could wrap those divs in an ng-container with ngIf and toggle expansion. I just considered you have a unique id for each item. So we can just use it to mark which item is expanded. In case you do not have a unique key for each item, you could just use the index of the item.

Your Template:

<button (click)="toggleExpand(item.id)">expand</button>

  <ng-container *ngIf="item.id == isExpanded">
          <div class="Question">Question1</div>
        <div class="Question">Question2</div>
        <div class="Question">Question3</div>
        <div class="Question">Question4</div>
        <div class="Question">Question5</div>
  </ng-container>

The toggle method:

toggleExpand(val) {
    this.isExpanded= val == this.isExpanded? '' : val;
  }

If you want to expand more than one row in the same time, then you should memorize the expanded rows in an object isCollapsed where the key is the expanded item.id and the value is a boolean e.g.

  isExpanded = {};

  toggleExpand(val) {
    this.isExpanded[val] = !this.isExpanded[val];
  }

And adjust your ng-container to:

<ng-container *ngIf="isExpanded[item.name]">
over 4 years ago · Santiago Trujillo Report

0

You only need to have a boolean array for mapping collapsed divs.

See your code working HERE

https://stackblitz.com/edit/angular-ngx-virtual-scroller-nzqnbw?file=src/app/app.component.html

isCollapsed: boolean[] = [];

  constructor(private cd: ChangeDetectorRef) {
    for (let i = 1; i < 10; i++) {
      this.items.push({ name: 'Item ' + i });
      this.isCollapsed[i] = true;
    }

    // Just to see the first item not collapsed, you can omit it if you want them all collapsed by default.
       if (this.items.length > 0) { this.isCollapsed[0] = false; }
  }

HTML:

 <div
        *ngFor="let item of scroll.viewPortItems; let indexOfColaps = index"
        class="groupQuestion"
      >

<div *ngIf="!isCollapsed[indexOfColaps]">
          <div class="Question">Question1</div>
          <div class="Question">Question2</div>
          <div class="Question">Question3</div>
          <div class="Question">Question4</div>
          <div class="Question">Question5</div>
</div>

\\You can change div *ngIf="... for container *ngIf="... if you want.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!