简体   繁体   中英

PrimeNG Accordion expand all/collapse all

I am looking for a way to Open and Close the entire accordion with one button. Right now I'm doing it like this. Currently only the expansion works but the collapse does not for some reason. I checked and the tab.selected is getting set to False on collapse, but it won't close

openCloseAllProjectStatusTabs(requestToOpen: boolean) { if (this.projectStatusTabs.== undefined && this projectStatusTabs = null){ this.projectStatusTabs.forEach(tab => tab.selected = requestToOpen); this.expanded = requestToOpen; } }

I didn't test this code, but I think all you need is to give the p-accordion the multiple flag. Then all you need to do is a single isExpanded value that will control all of the tabs using the selected input.

app.component.html

<p-accordion [multiple]="true">
  <p-accordionTab header="Header 1" [selected]="isExpanded">
    <p>
      Content 1
    </p>
  </p-accordionTab>
  <p-accordionTab header="Header 2" [selected]="isExpanded">
    <p>
     content 2
    </p>
  </p-accordionTab>
  <p-accordionTab header="Header 3" [selected]="isExpanded">
    <p>
      content 3
    </p>
  </p-accordionTab>
</p-accordion>

<button (click)="isExpanded = !isExpanded">Toggle</button>

app.component.ts

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent { 

  private _isExpanded = false;
  
  public get isExpanded() {
    return this._isExpanded;
  }

  public set isExpanded(value: boolean) {
     this._isExpanded = value;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM