0

Am binding data to angular material table by preparing columns dynamically.

please find below code 

component.ts

export class DepComponent {
    tableSource :any= [
              {
                "name":"ssit",
                "departments":[{"depName":"cse","noOfStudents":30},
                                {"depName":"civil","noOfStudents":60}]
              },
                   {
                "name":"vbit",
                "departments":[{"depName":"cse","noOfStudents":90},
                                {"depName":"civil","noOfStudents":40}]
              }
        ];

    displayedColumns: string[] = [];
   constructor() {
        this.displayedColumns= this.tableSource[0].departments.map(x=>x.depName)
    }
}


  <table mat-table [dataSource]="tableSource" class="mat-elevation-z8">
        <ng-container matColumnDef="{{column}}" *ngFor="let column of displayedColumns">
            <th mat-header-cell *matHeaderCellDef>
                {{column}}
            </th>
            <td mat-cell *matCellDef="let element">
                <span *ngFor="let s of element.departments">
                    {{s.noOfStudents}}
                </span>
            </td>
        </ng-container>

        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>

am getting output like this, duplicate data is printing in each row like below with my code

cse     civil
30 60   30 60
90 40   90 40

i want output like below

Cse Civil
30  60
90  40
2
  • Can you provide stackblitz where I can reproduce an issue! Commented Oct 3, 2019 at 9:59
  • You have to manage both rows and column as separate array Commented Oct 3, 2019 at 10:12

1 Answer 1

2

To get the desired result First you will need to transform data in desired format like

dataSource=this.tableSource.map(i=> {
      var result = {};
         i.departments.forEach(d=>{
        result[d['depName']]=d.noOfStudents
        })
       return result;
      })

then change html to

 <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
         <ng-container matColumnDef="cse">
                <th mat-header-cell *matHeaderCellDef> Cse </th>
                <td mat-cell *matCellDef="let element"> {{element.cse}} </td>
          </ng-container>
          <ng-container matColumnDef="civil">
                <th mat-header-cell *matHeaderCellDef> Civil </th>
                <td mat-cell *matCellDef="let element"> {{element.civil}} </td>
          </ng-container>
        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>

demo

Sign up to request clarification or add additional context in comments.

4 Comments

it's working for sample JSON what I provided, when i fetch result from database result returning as {"30":"30","60":"60"} value is retunrning instead of key
@madhuGoud check your api response if that is same as sample json
correct @jitender invalid response from my api, one thing is it possible to push object here result[d['depName']]={d.noOfStudents,d.noOfTeachers }
@madhuGoud I don't think so bcs it need to be string and your are trying to assign object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.