0

I am working in angular app where I am working on COVID 19 app.

Here I have 2 components where component A list of all the states and component B list all the district of the particular state.

Here is my stack blitz link stack blitz link

I want my output like this expected output

I got reference here from stack overflow stack overflow reference

Now I have displayed all the data of component in table format and when I click on that state it should load all the data of clicked state and when I click on that state again it should get closed. Also if I click on other state lists of all district fr that state should get displayed

But I don't know where to put my <app-componentB></app-componentB> because if put it inside for loop and if I try to display list for one state it displays the same list of district under all states

Here is a piece of my code

componentA.html

  <tbody *ngFor="let data of statewisedata;let i=index">
                <span class="dropdown rotateDownRight"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg></span>

                <tr class="state">
                    <td (click)="OngetState(data.state)" style="font-weight: 600;">{{data.state}}</td>



                    <td style="color: inherit;">{{data.confirmed}}

                        <span *ngIf='DailystateStatus[i]?.confirmed !==0 || DailystateStatus[i]?.confirmed < 0 ;' class="deltas" style="color: rgb(255, 7, 58);"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline>
                                </svg>    {{DailystateStatus[i]?.confirmed}}</span>

                    </td>


                    <td style="color: inherit;">{{data.active}}</td>
                    <td style="color: inherit;">{{data.recovered}}</td>
                    <td style="color: inherit;">{{data.deaths}}</td>

                </tr>


 <app-district *ngIf="!showDistrict"></app-district>

        </tbody>

componentA.ts

 showDistrict=true
  OngetState(state) {
    this.cs.getState(state)
    this.cs.getDataDistrictWise(state)
    this.showDistrict=!this.showDistrict
  }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tyler
  • 418
  • 4
  • 14

2 Answers2

0
you need to do a few changes and your code is

**componentA.html**

            <tbody *ngFor="let data of statewisedata;let i=index">
                <span class="dropdown rotateDownRight"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg></span>

                <tr class="state">
                    <td (click)="OngetState(data.state); showHideData(data)" style="font-weight: 600;">{{data.state}}</td>



                    <td style="color: inherit;">{{data.confirmed}}

                        <span *ngIf='DailystateStatus[i]?.confirmed !==0 || DailystateStatus[i]?.confirmed < 0 ;' class="deltas" style="color: rgb(255, 7, 58);"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline>
                                </svg>    {{DailystateStatus[i]?.confirmed}}</span>

                    </td>


                    <td style="color: inherit;">{{data.active}}</td>
                    <td style="color: inherit;">{{data.recovered}}</td>
                    <td style="color: inherit;">{{data.deaths}}</td>

                </tr>
                <app-district *ngIf="data['show']"></app-district>
            </tbody>



**componentA.ts**

  OngetState(state) {

    this.cs.getState(state)
    this.cs.getDataDistrictWise(state)
    // this.showDistrict=!this.showDistrict
  }

 showHideData(data) {
    if(data && data['show'] == true) {
      data['show'] = false;
    } else {
      data['show'] = true;
    }
  }
0

I hope you can try something from the reference you have mentioned


    <tr>

     <td (click)="OngetState(data.state,i)" style="font-weight: 600;">{{data.state}}</td>

    <td>...</td>
    <td>...</td>
    <td>...</td>
    </tr>

<app-district *ngIf="selectedIndex == i && showDistrict==true"></app-district>

component.ts

selectedIndex = -1;
  showDistrict:boolean=false

OngetState(state,i) {
    console.log(this.showDistrict)
    this.cs.getState(state)
    this.cs.getDataDistrictWise(state)
    this.selectedIndex = i;   
    this.showDistrict=!this.showDistrict
    console.log(this.showDistrict)
  }
Mehul Kothari
  • 386
  • 1
  • 12
  • Your answer is workin fine.but there is one bug when I will click on state and district list will open but then when i click on other state list currently opened list will get closed..Still thanks – tyler Apr 02 '20 at 12:38