Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda