Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Angular Mat-Table with Dynamic Columns generate and Data should be populated in horizontal way

displayedColumns: string[] = ['SAMPLERULENAME','DISPLAYNAME1','OPERATOR1','COUNT1','DISPLAYNAME2','OPERATOR2','COUNT2','DISPLAYNAME3','OPERATOR3','COUNT3','DISPLAYNAME4','OPERATOR4','COUNT4'];
  dataSource = ELEMENT_DATA;

  getValue(element, colname){
   console.log(element);
   var lastChar = colname.substr(colname.length - 1);
   console.log(lastChar);
   colname = colname.slice(0, -1); 
   console.log(colname);
   
   if(element["SAMPLERULECONDITIONS" + lastChar]){
    return (element["SAMPLERULECONDITIONS" + lastChar][colname]);
   }
  }

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

<!-- Position Column -->

<ng-container *ngFor="let col of displayedColumns; let i = index" [matColumnDef]="col">

<ng-container  *ngIf="col === 'SAMPLERULENAME'" matColumnDef="SAMPLERULENAME">
<th mat-header-cell *matHeaderCellDef> </th>
<td mat-cell *matCellDef="let element"> {{element.SAMPLERULENAME}} </td>
</ng-container>

<ng-container *ngIf="col !== 'SAMPLERULENAME'">
    <th mat-header-cell *matHeaderCellDef>  </th>
    <td mat-cell *matCellDef="let element">
    {{getValue(element, col)}}
    </td>      
  </ng-container>  
  
</ng-container>

<!-- Header row first group -->
<ng-container matColumnDef="header-row-first-group">
<th mat-header-cell *matHeaderCellDef 
    [style.text-align]="center"
    [attr.colspan]="1"> 
  Name
</th>
</ng-container>

<!-- Header row second group -->
<ng-container matColumnDef="header-row-second-group">
<th mat-header-cell *matHeaderCellDef [attr.colspan]="3"> Second group </th>
</ng-container>

<!-- Header row 3 group -->
<ng-container matColumnDef="header-row-3-group">
<th mat-header-cell *matHeaderCellDef [attr.colspan]="3"> Condition1 </th>
</ng-container>

<!-- Header row 4 group -->
<ng-container matColumnDef="header-row-4-group">
<th mat-header-cell *matHeaderCellDef [attr.colspan]="3">Condition2 </th>
</ng-container>

<!-- Header row 4 group -->
<ng-container matColumnDef="header-row-5-group">
<th mat-header-cell *matHeaderCellDef [attr.colspan]="3"> Condition3 </th>
</ng-container>



<tr mat-header-row *matHeaderRowDef="['header-row-first-group', 'header-row-second-group','header-row-3-group','header-row-4-group','header-row-5-group']"></tr>


<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
 
PREVIOUS NEXT
Tagged: #Angular #Dynamic #Columns #generate #Data #populated #horizontal
ADD COMMENT
Topic
Name
2+2 =