I have an array of objects (called devices) which I get from an API during a POST method. When I try to fill it in a simple html table, it works:
<table class="table table-striped">
<thead>
<tr>
<th *ngFor="let property of devices[0] | keys">{{property.key}}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let device of devices">
<td *ngFor="let property of device | keys">{{property.value == null ? '---' : property.value.toString()}}</td>
</tr>
</tbody>
</table>
I also use custom pipe to display key and value of each property of an object:
@Pipe({ name: 'keys' })
export class KeysPipe implements PipeTransform {
transform(value, args: string[]): any {
let keys = [];
for (let key in value) {
if (value.hasOwnProperty(key)) {
keys.push({ key: key, value: value[key] });
}
}
return keys;
}
}
But when I try to do the same with PrimeNG DataTable, it doesn't display anything:
<p-dataTable var="device" [value]="devices" sortMode="multiple">
<p-column *ngFor="let property of device | keys" field="{{property.value == null ? '---' : property.value.toString()}}" header="{{property.key}}" [sortable]="true"></p-column>
</p-dataTable>
What am I missing/doing wrong?
Looks like the problem was that I didn't fill in the headers, so PrimeNG didn't know what to do with the data.
<p-dataTable [value]="devices">
<p-column *ngFor="let header of headers" [field]="header.field" [header]="header.header"></p-column>
</p-dataTable>
And here's the filling of headers taken from the first object of array (all objects in my array have the same properties, just with different values):
public headers = [];
FillHeaders(devices) {
for (let property in devices[0]) {
if (devices[0].hasOwnProperty(property)) {
this.headers.push({ field: property, header: property });
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With