简体   繁体   中英

How to collapse/expand multiple panels in PrimeNG?

I have multiple panels and 2 buttons. One button will expand them all and the other one will collapse them all. I'm not able to expand or collapse them. Does anyone know how to make this happen? Here's my code. Thank you in advance!

PLUNKER

 <p-panel header="Panel 1" [toggleable]="true" [style]="{'margin-bottom':'20px'}">

  The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
  His beloved son Michael has just come home from the war, but does not intend to become part of his father's business.
  Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family,
  kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.

</p-panel>

PrimeNG panels have a number of properties - one of these is collapsed This defaults to false but can be set to a variable, and then have a button where clicking it toggles the state of the variable:

app.component.ts

export class AppComponent {
  collapsed = false
}

app.template.html

<p-panel header="Panel 4" [toggleable]="true" [collapsed]="collapsed" [style]="{'margin-bottom':'20px'}">
Some text
</p-panel>
<!-- To toggle the state, set the button like this -->
<button (click)="collapsed = !collapsed">Toggle</button>
<!-- to have separate buttons, do this -->
<button (click)="collapsed = true">Collapse</button>
<button (click)="collapsed = false">Expand</button>

Simply bind this property to the variable the button controls on any panel you want to globally collapse.

If you also want to hide the individual collapse buttons for each pane, you can set the following style:

.ui-panel-titlebar-toggler { display:none; }

Working example forked from your plnkr.

Following @match answer, you must indicate the collapsed property with banana in a box, meaning it has to be '[()]', otherwise it won't work after the first trigger.

So it would look like this:

<p-panel header="Panel 4" [toggleable]="true" [(collapsed)]="collapsed" [style]="{'margin-bottom':'20px'}">
    Some text
</p-panel>

Maybe a bit late, but just so you know.

Here is where they introduced those changes.

您可以像这样使用两种方式绑定 -

<p-panel [(collapsed)]="collapsed">

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