【问题标题】:Transpose data in a primeng datatable/typescript在primeng数据表/打字稿中转置数据
【发布时间】:2017-07-12 15:18:41
【问题描述】:

我有一个包含这些字段的地方数组。(大陆和国家)

 places = [
           { "continent":"Europe", "country": "UK" },
           { "continent":"North America", "country": "US" },
           { "continent":"Asia", "country": "Philippines" },
           { "continent":"Africa", "country": "Egypt" }
          ];

如果我将它绘制在一个primeng数据表中,它就像这样。

我想要的是转置数据。我希望数据表显示在数据表中,这样大陆行将成为标题,国家将成为字段。我想要的是这样的东西。

我为此创建了一个 plunkr。 http://plnkr.co/edit/KX1pCw?p=preview

【问题讨论】:

标签: angular typescript primeng


【解决方案1】:

修改你的 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);
  }
}

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2018-03-16
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多