我有一系列有这些领域的地方。(大陆和国家)
places = [
{ "continent":"Europe", "country": "UK" },
{ "continent":"North America", "country": "US" },
{ "continent":"Asia", "country": "Philippines" },
{ "continent":"Africa", "country": "Egypt" }
];
我想要的是转置数据。我希望数据表显示在数据表中,以便大陆行将成为标题,国家将成为字段。我希望是这样的。
我为此创建了一个plunkr。 http://plnkr.co/edit/KX1pCw?p=preview
答案 0 :(得分:0)
修改了你的plunkr工作here。
html -
<p-dataTable [value]="places" sortMode="single" reorderableColumns="true" [globalFilter]="gb" scrollable="true" scrollHeight="350px">
<p-column *ngFor="let col of cols" [field]="col.field" [header]="col.header" [sortable]="true" [style]="{'width': '200px'}">
</p-column>
</p-dataTable>
app.component.ts -
export class AppComponent {
places_rawdata =
[
{ "continent":"Europe", "country": "UK" },
{ "continent":"North America", "country": "US" },
{ "continent":"Asia", "country": "Philippines" },
{ "continent":"Africa", "country": "Egypt" }
];
places: any [] = [];
cols:any[]=[];
ngOnInit() {
let temp: any = {};
this.places=[];
this.places_rawdata.forEach(
(place,index) => {
let s : string = 'country' + index;
temp[s]= place.country
this.cols.push({field: s, header: place.continent})
});
this.places = [...this.places,temp];
console.log(this.places);
}
}