how to show/hide dynamic id of div's in angula

2019-02-21 00:22发布

问题:

Here a loop of kpiName is executed and inside loops of subRegion also executed.
As a result 4 divs of class="col-xs-2" are created and inside it two div(clickable divs inside filter class) are created having dynamic Id as id="filteredTabSubRegion{{index}}",id="filteredTabProductLine{{index}}" which onclick calls some function.

The requirement is when corresponding id="filteredTabSubRegion{{index}}",id="filteredTabProductLine{{index}}" is clicked show and hide id="filteredDataSubRegion{{index}}" and id="filteredDataProductLine{{index}}" of corresponding 4 divs(a,b,c,d).

app.component.ts

kpi = [a, b, c, d];
subRegion = ['China', 'India', 'Japan', 'Korea', 'SEATH'];
productLine = ['6A', '7T', '9T', 'UV', 'BA'];

    loadFilterData(index,type){
    console.log(index);
//---------------------angular2 implementation???------------------
     if (type === 'subregion') {     
              // $('#filteredDataSubRegion' + index).show();  
              // $('#filteredDataProductLine' + index).hide();
          } else {
              // $('#filteredDataSubRegion' + index).hide();
              // $('#ffilteredDataProductLine' + index).show();
          }
    }

app.component.html

<div class="col-xs-2 " *ngFor="let kpi of kpiName;let index=index;">
  <div class="col-xs-12 rmpm bottomSectionKpis">
    <div class="col-xs-12 rmpm filter">
      <div class="col-xs-6 rmpm" id="filteredTabSubRegion{{index}}" (click)="loadFilterData(index,'subregion')" [ngClass]="{'activex': act}">Sub Region</div>
      <div class="col-xs-6 rmpm" id="filteredTabProductLine{{index}}" (click)="loadFilterData(index,'productline')" [ngClass]="{'activex': activ}">Product Line</div>
    </div>
    <div class="col-xs-12 rmpm filterTable">
      <div class="col-xs-12 rmpm " id="filteredDataSubRegion{{index}}" style="display:block">
        <div class="col-xs-12 rmpm filteredDataRow" *ngFor="let subDta of subRegion;let k = index;">
          <div class="col-xs-2 rmpm filteredDataIcon">
            <i class="fa fa-circle" aria-hidden="true"></i>
          </div>
          <div class=" col-xs-7 rmpm filteredDataName">{{subRegion[k]}}</div>
          <div class="col-xs-3 rmpm filteredDataShift text-center">Shift</div>
        </div>
      </div>
      <div class="col-xs-12 rmpm " id="filteredDataProductLine{{index}}" style="display:none">
        <div class="col-xs-12 rmpm filteredDataRow" *ngFor="let subDta of subRegion;let k = index;">
          <div class="col-xs-2 rmpm filteredDataIcon">
            <i class="fa fa-circle" aria-hidden="true"></i>
          </div>
          <div class=" col-xs-7 rmpm filteredDataName">{{productLine[k]}}</div>
          <div class="col-xs-3 rmpm filteredDataShift text-center">Shift</div>
        </div>
      </div>
    </div>
  </div>
</div>

回答1:

Ok, so as smnbbrv proposes, something like :

Component file :

displayDiv: boolean = false;
loadFilterData(index: number, div: string){
   this.displayDiv = !this.displayDiv;
}

HTML file :

<div *ngIf="displayDiv" class="col-xs-12 rmpm " id="filteredDataSubRegion{{index}}">
...
</div>
<div *ngIf="!displayDiv" class="col-xs-12 rmpm " id="filteredDataProductLine{{index}}">
...
/<div>

Does this help you ?

Re. If I understand you well, you have 4 divs :

A
  A1
    A11 (click)-> show only A21 
    A12 (click)-> show only A22
  A2
    A21
    A22
B (...)
C (...)
D (...)

Is that right ?

If so, you have several possibilities to manage your displays. By example, you can use 2 variables : displayBlock, displaySubBlock.

displayBlock: boolean = false;
displaySubBlock: boolean = false;

loadFilterData(index: number, div: string){
   this.displayBlock = index;
   this.displaySubBlock = !this.displaySubBlock;
}

<div *ngIf="displayBlock === i && displaySubBlock" class="col-xs-12 rmpm "id="filteredDataSubRegion{{index}}">
...
</div>
<div *ngIf="displayBlock === i && !displaySubBlock" class="col-xs-12 rmpm " id="filteredDataProductLine{{index}}">
...
</div>

Code not tested, just for the idea.