Search code examples
angularprimeng-datatable

Primeng data-table row does't show cell values


I used Primeng data-table for display selected values from angular select.

Whenever select the option in dropdown that selected values need to be display in primeng data-table.

Here problem I got empty row instead selected value:

enter image description here

The Code:

 <mat-form-field fxFlex="50%">
   <mat-label>Select The NAICS Name Or NAICS Code.</mat-label>
   <mat-select multiple name="cnae"(selectionChange)="cnaeChangeValue($event)">
    <mat-option *ngFor="let cnae of listOfcnae.default.cnaeData" 
       [value]="cnae">
    {{cnae.TextField }}
     </mat-option>
   </mat-select>
</mat-form-field>
<p-table [columns]="cnaeCols" [value]="selectedCnaeValues" [paginator]="true"[rows]="10">
  <ng-template pTemplate="header" let-columns>
     <tr>
         <th *ngFor="let col of columns">
          {{col.header}}
         </th>
     </tr>
  </ng-template>
  <ng-template pTemplate="body" let-rowData let-columns="columns">
    <tr>
        <td *ngFor="let col of columns">
        {{rowData[col.filed]}}
      </td>
    </tr>
  </ng-template>
</p-table> 

ts code:
ngOnInit() {
this.cnaeCols = [
      { field:"RowNumber", header: "RowNumber"},
      { filed:"TextField", header: "TextField"},
      { field:"ValueField", header: "ValueField"},
    ]
}
 cnaeChangeValue(eventVal) {
    this.selectedCnaeValues.push(eventVal.value);
  }

And json value:
[
{
 "RowNumber": "1",
 "TextField": "1011201 - FRIGORÍFICO - ABATE DE BOVINOS",
  "ValueField": "168"
}
]

Solution

  • I think the material dropdown (selectionChange)="cnaeChangeValue($event) on selection return all selected items in the array and that you are trying to push in the array that's why the table is not showing data.

    cnaeChangeValue(eventVal) {
        this.selectedCnaeValues = eventVal.value; // Will work
      }
    

    Solution Created on stackblitz

    Hope this will help!