Search code examples
angularangular7

Nested Table issue in angular?


enter image description here

data = {
SolutionsDetail: [
  {
    SolutionId: 658,
    name: "dk",
    id: 1568377327000,
    requestDetails: [
          {
            ReqId: 2331
          },
          {
            ReqId: 1234
          },
          {
            ReqId: 5678
          }
        ],
    groups: [
      {
        GroupId: 1,
        requestDetails: [
          {
            ReqId: 2331
          },
          {
            ReqId: 1234
          },
          {
            ReqId: 5678
          }
        ]
      },
      {
        GroupId: 2,
        requestDetails: [
          {
            ReqId: 2331
          }
          ,
          {
            ReqId: 2212
          }
        ]
      },
      {
        GroupId: 3,
        requestDetails: [
          {
            ReqId: 2331
          },
          {
            ReqId: 4444
          },
          {
            ReqId: 1111
          },
          {
            ReqId: 2222
          }
        ]
      }
    ]
  }
] }

My working structure is displaying group and request properly.now i have requestDetails outside the group as well. i am not able to display properly as required.(Two place request details i have in json which needs to display as per snapshot.)

below mentioned code is displaying group and number of request along side.

<ng-container *ngFor="let groupRowData of data.SolutionsDetail[0].groups;"> 
  <ng-container *ngFor="let requestDetailData of groupRowData.requestDetails; let $index = index">
    <tr>
      <td *ngIf="$index===0;" [attr.rowspan]="groupRowData.requestDetails.length">Group {{ groupRowData.GroupId }}</td>
      <td>
        {{ requestDetailData.ReqId}}
      </td>
    </tr>
  </ng-container>
</ng-container>


Solution

  • This is how i solved

    <table border=1>
    <ng-container *ngFor="let groupRowData of data.SolutionsDetail[0].groups;"> 
    
      <ng-container *ngFor="let requestDetailData of groupRowData.requestDetails; let $index = index">
        <tr>
          <td *ngIf="$index===0;" [attr.rowspan]="groupRowData.requestDetails.length">Group {{ groupRowData.GroupId }}</td>
          <td>
            {{ requestDetailData.ReqId}}
          </td>
        </tr>
      </ng-container>
    </ng-container>
    <ng-container  *ngFor="let RowData of data.SolutionsDetail[0].requestDetails; let $index = index">
    <tr>
          <td *ngIf="$index===0;" [attr.rowspan]="data.SolutionsDetail[0].requestDetails.length">NA</td>
          <td>
            {{ RowData.ReqId}}
          </td>
        </tr>
    </ng-container>
    </table>