1
votes

I have this accordion, so in order to expand/collapse an accordion tab I click anywhere on the tab, but I want to be able to expand/collapse when clicking ONLY on the "Click me" button and not on the accordion tab. Can anyone point me in the right direction? Thanks a lot in advance!

Here's my code:

LIVE DEMO

<p-accordionTab header="Accordion Tab 1" [selected]="true">
   <p-header>
      Accordion Tab 1
      <span>
         <button (click)="myFunction($event)">Click me</button>
      </span>
  </p-header>
   <ul>
     <li>Colors</li>
   </ul>
</p-accordionTab>
2

2 Answers

0
votes

Based on the docs for the accordion component that you're using, you can use the Programmatic Control to implement what you need.

Here is the DEMO, where I modified your code. All tabs are disabled and they are opened/closed programmatically only by clicking on the "Click me" buttons in their headers.


UPD new DEMO with:

  • multiple="true"
  • has all tabs open by default
  • custom styling that overwrites the disabled property
0
votes

css File:-

.zippy{
 border: 1px solid #ccc;
 border-radius:2px;
}
.zippy-heading{
 font-weight: bold;
 padding: 20px;
 cursor: pointer;
 background: #f09b9b;
}
.zippy-body{
 padding: 20px;
}
.expanded{
 background: #b3b0b0;
}

Html view:-

<div class="zippy" style="padding:40px;">
  <div class="zippy-heading"
    [class.expanded]="isExpandedBikes"
    (click)="onBikeClick()">
    {{bike}}
    <span
      class="fa"
      [class.fa-angle-down] ="!isExpandedBikes"
      [class.fa-angle-up]="isExpandedBikes"
      (click)="onBikeClick()" style="font-size: 20px; position: absolute;right: 50px;"></span>
  </div>
  <div *ngIf="isExpandedBikes" class="zippy-body">
    <li *ngFor ="let data of bikeContent">
      {{data}}
    </li>
  </div>
</div>

Component File:-

constructor() { 
 this.bike = "Bikes";
 this.bikeContent=["My First Bike","My Second Bike","My Third Bike"];
}

for button Fxn in component File:-

 onBikeClick(){
  this.isExpandedBikes =! this.isExpandedBikes;
 }