【问题标题】:Adding a dynamic column primeNG Angular 2. Make column for each object in the array添加动态列 primeNG Angular 2. 为数组中的每个对象创建列
【发布时间】:2017-11-20 07:39:41
【问题描述】:

我有一个要在primeng 数据表中显示的人员列表。 person 对象具有这些字段(firstName、lastName、continentsVisited)。 continentVisited 字段是人访问过的大陆数组。访问的这个大洲是动态的。我想要的是除了 firstName 和 lastName 之外,为访问的每个大陆都有一个单独的列。

export class AppComponent {
    persons: any [] = [
        {"firstName": "paolo","lastName":"revira","continentsVisited": [
               { "continent":"Europe", "name": "UK" },
               { "continent":"Asia", "name": "China" },
               { "continent":"North America", "name": "US" }
          ]},
        {"firstName": "kenneth","lastName":"santos"},"continentsVisited": [
               { "continent":"Europe", "name": "France" },
               { "continent":"Asia", "name": "Japan" },
               { "continent":"North America", "name": "Canada" }
          ]},
        {"firstName": "chris","lastName":"kenndy"},,"continentsVisited": [
               { "continent":"Europe", "name": "Germany" },
               { "continent":"Asia", "name": "Philippines" },
               { "continent":"North America", "name": "Mexico" }
          ]},
        ];

    ngOnInit() {

    }
}


<p-dataTable [value]="persons" [editable]="true"  resizableColumns="true" reorderableColumns="true"> 
    <p-column field="firstName" header="First Name" [editable]="true"></p-column>
    <p-column field="lastName" header="Last Name"></p-column>
</p-dataTable>

我为此创建了一个 plunkr。这是链接https://plnkr.co/edit/gS1wsI?p=preview

【问题讨论】:

    标签: angular typescript primeng


    【解决方案1】:

    你可以使用下面的代码,

    <p-dataTable [value]="persons">
        <p-column *ngFor="let col of cols" [field]="col.field" [header]="col.header"></p-column>
    </p-dataTable>
    

    打字稿代码

    export class App {
      name:string;
      value:any;
       persons: any [] = [
            {"firstName": "paolo","lastName":"revira","continentsVisited": [
                   { "continent":"Europe", "name": "UK" },
                   { "continent":"Asia", "name": "China" },
                   { "continent":"North America", "name": "US" }
              ]},
            {"firstName": "kenneth","lastName":"santos","continentsVisited": [
                   { "continent":"Europe", "name": "France" },
                   { "continent":"Asia", "name": "Japan" },
                   { "continent":"North America", "name": "Canada" }
              ]},
            {"firstName": "chris","lastName":"kenndy","continentsVisited": [
                   { "continent":"Europe", "name": "Germany" },
                   { "continent":"Asia", "name": "Philippines" },
                   { "continent":"North America", "name": "Mexico" }
              ]}
            ];
    cols:any[]=[];
      constructor() {
        Object.keys(this.persons[0]).forEach(item=>{
          console.log(item)
          this.cols.push({field: item, header: item});
        })
      console.log(this.cols    );
        this.name = `Angular Prime Data table Dynamic columns`
      }
    

    更新1:基于截图

    HTML 看起来像

    <p-dataTable [value]="newPersons">
        <p-column *ngFor="let col of cols" [field]="col.field" 
                   [header]="col.header"></p-column>
    </p-dataTable>
    

    打字稿代码:

    for(let i =0 ; i <this.persons.length;i++){
      let temp={
        firstName : this.persons[i].firstName,
        lastName : this.persons[i].lastName
      }
      this.persons[i].continentsVisited.forEach(item=>{
      let keyValue = Object.values(item);
      console.log(keyValue)
       temp[keyValue[0].toString()] = keyValue[1]
    
    }
     this.newPersons.push(temp);
    }
    console.log(this.newPersons)
    Object.keys(this.newPersons[0]).forEach(item=>{
      this.cols.push({field: item, header: item});
    })
    

    注意:在下面的演示中更新

    LIVE DEMO

    【讨论】:

    • 我想要的是每个访问过的大陆在每个列中分开。我该怎么做
    • 你能用你预期的布局更新帖子吗?这可以帮助我。
    • 非常感谢@Aravind。我有另一个问题/问题。我想在prime ng中实现collum切换器,我想在其中切换每个列visitedContinents和姓氏的列。所以可见的列将只有 2 列。 1 是名字,另一个是visitedContinents 和lastName 的切换列。我 [pdatedthe question 并附上示例布局
    • @ilovejavaAJ 将此标记为答案并点赞。下一个问题你能在fb联系我吗?需要更多信息
    猜你喜欢
    • 2019-04-03
    • 2016-10-17
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多