Angular 2 collapsible table not collapsing in full


I’m trying to create a table with many different addresses and many different values for each address and the data is coming from a mock database. Image here enter image description here

The problem I’m facing now is how to make each button to expand each row with individual values. At the moment when you click on the button it expand and collapse only the first value of the first row.

How do we achieve this functionality in Angular2. Please help?

This is the relevant part of the code. Will be happy to provide more info if need!

<!-- collapseble trigger -->
 <a class="btn btn-sm btnCollapse2" data-toggle="collapse" href="#collapse2" role="button" aria-expanded="false"

<div class="main-content">
 <div class="row">
<div class="col-md-12 accordion" id="accordionExample">
  <div class="card">
    <div class="card-header card-header-grey" id="headingOne">
      <h3 class="card-title">Property List</h3>
      <span class="card-category">Current readings</span>

      <!-- collapseble trigger 1-->
      <button class="btn btn-sm float-right btnCollapse" type="button" data-toggle="collapse" data-target="#collapseOne"
        aria-expanded="false" aria-controls="collapseOne"><i class="material-icons list">list</i>
        <strong>Collapse List
        </strong> </button>


    <div class="card-body">
      <div class="table-responsive">

        <table class="table table-hover">
          <thead class="text-primary">
              <th *ngIf="showBuildingName || sensorType=='S2'">Temp</th>
              <th *ngIf="showBuildingName || sensorType=='S2'">Humid</th>
              <th *ngIf="showBuildingName || sensorType=='S2'">Light</th>
              <th *ngIf="showBuildingName || sensorType=='S2'">Motion</th>
              <th *ngIf="showBuildingName || sensorType=='S1'">Audio</th>

          <!-- COLLAPSE 1-->
            <ng-template let-building ngFor [ngForOf]="buildingsList" >

              <div *ngIf="showBuildingName">
                <h3 *ngIf=" != -1">
                  <a class="building-row" routerLink="/building/{{}}">{{}}</a>

                <h2 *ngIf=" == -1">{{}}</h2>

              <!-- COLLAPSE 2-->
              <tr *ngFor="let prop of; let propertyIndex=index" id="collapseOne"  class="collapse show multi-collapse" aria-labelledby="headingOne" data-parent="#accordionExample">

                <a class="btn btn-sm btnCollapse2" data-toggle="collapse"
                  [href]="'#collapse' + propertyIndex" role="button" aria-expanded="false" aria-controls="collapse2"></a>

                  <a routerLink="/property/{{}}" class="collapse show multi-collapse propName" [id]="'collapse' + propertyIndex">{{}}</a>
                <!-- For now, only temp, humid, light have alerts, so save some time but not getting contextual class for others -->
                <td [title]="cellTtip(prop.messages.temp?.message)" [class]="cellClass(prop.messages.temp?.rag)"><a
                    class="building-row"></a>{{prop.sensorTelemetry?.temp ? prop.sensorTelemetry?.temp : '-'}}</td>
                <td class="fine-cell" [title]="cellTtip(prop.messages.humid?.message)" [class]="cellClass(prop.messages.humid?.rag)">{{prop.sensorTelemetry?.humid
                  ? prop.sensorTelemetry?.humid : '-'}}</td>
                <td *ngIf="showBuildingName || sensorType=='S2'" class="fine-cell" [title]="cellTtip(prop.messages.light?.message)"
                  [class]="cellClass(prop.messages.light?.rag)">{{prop.sensorTelemetry?.light ?
                  prop.sensorTelemetry?.light : '-'}}</td>
                <td *ngIf="showBuildingName || sensorType=='S2'" class="fine-cell">{{prop.sensorTelemetry?.motion
                  >= 0 ? prop.sensorTelemetry?.motion : '-'}}</td>
                <td *ngIf="showBuildingName || sensorType=='S1'" class="fine-cell">{{prop.sensorTelemetry?.audio
                  0 ? prop.sensorTelemetry?.audio : '-'}}</td>



here is my coomponent.ts

export class BuildingListComponent implements OnInit {

buildingsList: Building[];
sensorType: string;
@Input() showBuildingName: boolean;

set buildings(buildings: Building[]) {

this.buildingsList =, (building) => {
  const properties =, (property) => {

    return {,
      name: _.startCase(
  // properties is array of {property: property, name:}

  // in the mode where we are showing only 1 building,
  // restrict columns shown by sensor type
  if (!this.showBuildingName) {
    this.sensorType = properties[0].sensorType;
  return {


Change collapse element id dynamic value at the time click element href. Click element href same value the open collapse id That only open collapse correctly.
for one example:

<div *ngFor="let prop of; let propertyIndex=index">
<a class="btn btn-sm btnCollapse2" data-toggle="collapse" [href]="'#collapse' + propertyIndex" role="button" aria-expanded="false" aria-controls="collapse2"></a>

<div class="collapse multi-collapse" [id]="'collapse' + propertyIndex"><p>{{}}</p></div>

Answered By – Jai Kumaresh

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.