Search code examples
angularngforangular-ng-if

How to toggle two divs when clicked while in a loop in angular4


It is a reference to the question asked how to show/hide dynamic id of div's in angular2.

I have prepared a plunker which will display the problem which i am facing? here, I want to toggle btn1 and btn2 separately in the 4 boxes. The requirement is when in box1, the btn1 is clicked , 'subRegion' should show & 'productLine' should hide; when btn2 is clicked,'productLine' should show and 'subRegion' should hide. Similarly, among box2,box3,box4. Here, clicking btn2 from box1(one among 4 boxes)is displaying the content in every boxes. How to fix that?

Here is plunker link, https://plnkr.co/edit/E3ykbvXIPtQwVqe9cnQA?p=preview

code:-

@Component({
  selector: 'my-app',
  template: `
 <div class="col-xs-3 rmpm" *ngFor="let kpi of kpiName;let index=index;">
 <div class="col-xs-12 rmpm" style="height:80px;border:1px solid;width:70px;padding:0px;margin:0px;">A</div>
 <div class="col-xs-12 rmpm" style="border:1px solid;width:70px;margin-top:10px;padding:0px;margin:0px;">

  <div class="col-xs-12 rmpm" style="border:1px solid;padding:0px;margin:0px;">

   <!--btn 1 should be by default active and have blue color -->

  <div class="col-xs-6 rmpm" id="btn1{{index}}"  (click)="showsubid(index,'btn1')" style="padding:0px;margin:0px;border-right:1px solid;">btn1</div>
   <div class="col-xs-6 rmpm" id="btn2{{index}}" (click)="showproid(index,'btn2')"  style="padding:0px;margin:0px;">btn2</div>
  </div>

 <!--By default will be displayed when btn1 is active,this will show when btn1 is clicked and hide when btn2 is clicked -->

  <div class="col-xs-12" id="subid{{index}}" style="padding:0px;margin:0px;" >
     <div class="col-xs-12 rmpm filteredDataRow" *ngFor="let subDta of subRegion;let k = index;" style="padding:0px;margin:0px;">
     <div class=" col-xs-12 rmpm filteredDataName " (click)='loadSubRegionData()' style="padding:0px;margin:0px;border-top:1px solid">{{subRegion[k]}}</div>
 </div>
 </div>

 <!--this will show when btn2 is clicked and hide when btn1 is clicked -->

    <div class="col-xs-12"  id="proid{{index}}"   style="padding:0px;margin:0px;" *ngIf="pro == 0" >
     <div class="col-xs-12 rmpm filteredDataRow"  *ngFor="let subDta of productLine;let k = index;" style="padding:0px;margin:0px;" >
     <div class=" col-xs-12 rmpm filteredDataName " (click)='loadSubRegionData()' style="padding:0px;margin:0px;border-top:1px solid">{{productLine[k]}}</div>
 </div>
  </div>
</div>
</div>
  `,
})
export class App {
    kpiName = ['s', 'TO ','d','e'];
  subRegion = ['Ch', 'Ia', 'Ja', 'Ke', 'SE'];
   productLine = ['6A', '7T', '9T', 'UV', 'BA'];
pro: any;
  constructor() {

  }

showsubid(index,btnid){
  alert(index)
   alert(btnid+index)
}
showproid(index){
   alert(index)
    // alert(id+index)
    this.pro = index;
}
}

Solution

  • As per your request. On clicking the button the specific content of that section should be displayed. I have updated the code: https://plnkr.co/edit/TEbI45K2TZkRYd84jYft?p=preview

        //our root app component
    import {Component, NgModule} from '@angular/core'
    import {BrowserModule} from '@angular/platform-browser'
    
    @Component({
      selector: 'my-app',
      template: `
     <div class="col-xs-3 rmpm" *ngFor="let kpi of kpiName;let index=index;">
     <div class="col-xs-12 rmpm" style="height:80px;border:1px solid;width:70px;padding:0px;margin:0px;">A</div>
     <div class="col-xs-12 rmpm" style="border:1px solid;width:70px;margin-top:10px;padding:0px;margin:0px;">
    
      <div class="col-xs-12 rmpm" style="border:1px solid;padding:0px;margin:0px;">
    
       <!--btn 1 should be by default active and have blue color -->
    
      <div class="col-xs-6 rmpm" id="btn1{{index}}"  (click)="showsubid(index,'btn1')" style="padding:0px;margin:0px;border-right:1px solid;">btn1</div>
       <div class="col-xs-6 rmpm" id="btn2{{index}}" (click)="showproid(index,'btn2')"  style="padding:0px;margin:0px;">btn2</div>
      </div>
    
     <!--By default will be displayed when btn1 is active,this will show when btn1 is clicked and hide when btn2 is clicked -->
    
      <div class="col-xs-12" id="subid{{index}}" style="padding:0px;margin:0px;" >
         <div class="col-xs-12 rmpm filteredDataRow" *ngFor="let subDta of subRegion;let k = index;" style="padding:0px;margin:0px;">
         <div class=" col-xs-12 rmpm filteredDataName " (click)='loadSubRegionData(index ,kpiName[index],subRegion[k])' style="padding:0px;margin:0px;border-top:1px solid">{{subRegion[k]}}</div>
     </div>
     </div>
    
     <!--this will show when btn2 is clicked and hide when btn1 is clicked -->
    
        <div class="col-xs-12"  id="proid{{index}}"   style="padding:0px;margin:0px;" *ngIf="pro === index" >
         <div class="col-xs-12 rmpm filteredDataRow"  *ngFor="let subDta of productLine;let k = index;" style="padding:0px;margin:0px;" >
         <div class=" col-xs-12 rmpm filteredDataName " (click)='loadSubRegionData()' style="padding:0px;margin:0px;border-top:1px solid">{{productLine[k]}}</div>
     </div>
      </div>
    </div>
    </div>
      `,
    })
    export class App {
        kpiName = ['s', 'TO ','d','e'];
      subRegion = ['Ch', 'Ia', 'Ja', 'Ke', 'SE'];
       productLine = ['6A', '7T', '9T', 'UV', 'BA'];
    pro: number;
      constructor() {
    this.pro= [];
      }
    loadSubRegionData(index,kpi,subreg){
      alert(index);
      alert(subreg);
    }
    showsubid(index,btnid){
      alert(index)
       alert(btnid+index)
    }
    showproid(index){
       alert(index)
        // alert(id+index)
        this.pro = index;
    }
    }
    
    @NgModule({
      imports: [ BrowserModule ],
      declarations: [ App ],
      bootstrap: [ App ]
    })
    export class AppModule {}
    

    There was a small mistake of considering variable pro as a boolean. If you consider it as a number and compare it with the index then it will do what you need.