Search code examples
angularangular-template

Angular 2: collapse multiple element


I have problem when I will collapse for content elements. When I press the button show all content are showing but no just one element of this content.

I following by this example Plunker

This is part of my html:

         <div *ngFor="#elem of apps">
               <div  class="col-md-5">
                  <div class="panel-heading">
                     <strong> {{elem.name}}</strong> on {{elem.host}} 
                  </div
                  <button type="button" (click)="isCollapsedContent= !isCollapsedContent">
               </div>
           </div>


           <div [collapse]="isCollapsedContent">
              <table class="table table-hover">
                   <thead>
                     <tr class="header">
                        <td>Property</td>
                        <td>Value</td>
                     </tr>
                   </thead>
                   <tbody>
                     <tr>
                        <td>app</td>
                         <td>{{elem.app}}</td>

                        <td>name</td>
                         <td>{{elem.name}}</td>

                        <td>host</td>
                         <td>{{elem.host}}</td>
                     </tr>
                   </tbody>
              </table>
           </div

And I add extra boolean property to my JSON from API called show default set false.

[
{"app":"database_1",
 "host":"my_host1",
 "name":"name1",
 "show": false
},
{"app":"database_2",
 "host":"my_host2",
 "name":"name2",
 "show": false
},
{"app":"database_3",
 "host":"my_host3",
 "name":"name3",
 "show": false
},
]

How can I show/hide only one element? Can I add property to isCollapsedContent for example:

<div [collapse]="isCollapsedContent(elem.show)">

Solution

  • Use ngIf to test whether elem.show is true.

      <div *ngFor="#elem of apps">
        <div  class="col-md-5">
          <div class="panel-heading">
              <strong> {{elem.name}}</strong> on {{elem.host}} 
          </div>
          <button type="button" (click)="elem.show = !elem.show"></button>
        </div>
    
        <table class="table table-hover" *ngIf="elem.show">
          <thead>
            <td>app</td>
            <td>Name</td>
            <td>Host</td>
          </thead>
          <tbody>
            <tr>
              <td >{{elem.app}}</td>
              <td >{{elem.name}}</td>
              <td >{{elem.host}}</td>
            </tr>
          </tbody>
        </table>
      </div>