Ng2智能表,从一个对象获取多列

时间:2020-10-12 13:19:18

标签: angular typescript2.0 ng2-smart-table

我正在使用ng2智能列来显示包含用户对象的交易。 我想在不同的列中显示id用户,名称和电子邮件。 我该怎么办?

settings = {
    add: {
      addButtonContent: '<i class="nb-plus"></i>',
      createButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
      confirmCreate : true
    },
    edit: {
      editButtonContent: '<i class="nb-edit"></i>',
      saveButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
      confirmSave : true
    },
    delete: {
      deleteButtonContent: '<i class="nb-trash"></i>',
      confirmDelete: true,
    },
    columns: {
      id: {
        title: 'ID',
        type: 'number',
      },
   
      user: { 
        title : 'name',
      valuePrepareFunction: (data) => {
                                   return data.email;
                               },
       },

      amount: {
        title: 'Valeur',
        type: 'number',
       
      },
      items: {
        name: { title: 'Nom' },
    },

      state: {
        title: 'Etat',
        type: 'string',
      },
      delivery_date: {
        title: 'Date de livraison',
        type: 'string',
      },
      expedition_date: {
        title: 'Date d\'expedition',
        type: 'string',
      },
    },
  };

在此链接上检查 https://stackblitz.com/edit/example-ng2-smart-table-ytzrns?file=src/app/app.component.ts

2 个答案:

答案 0 :(得分:0)

您可以引用同一对象的不同属性,而只需在valuePrepareFunction中获取整行。我将通过以下示例向您展示

...

property1: {
    title: 'Colum 1 title',
    type: 'string',
    valuePrepareFunction: (cell, row) => {
      return row.object.property1;
    },
},
property2: {
    title: 'Cell 2 title',
    type: 'string',
    valuePrepareFunction: (cell, row) => {
      return row.object.property2;
    },
},
...

我希望我的答案足够清楚。询问是否需要。

欢呼

答案 1 :(得分:0)

这是解决方案

  
   'user.firstName': {
    title: 'Name',
    valuePrepareFunction: (cell, row) => { return row.user.firstName }
  },

  
    'user.lastName': {
    title: 'Last name',
    valuePrepareFunction: (cell, row) => { return row.user.lastName }
  },
    'user.address': {
    title: 'address',
    valuePrepareFunction: (cell, row) => { return row.user.address }
  },
    'user.email': {
    title: 'email',
    valuePrepareF