【问题标题】:How to push custom array object in ng2 smart table如何在 ng2 智能表中推送自定义数组对象
【发布时间】:2017-11-10 02:22:06
【问题描述】:

https://github.com/akveo/ng2-smart-table 在设置对象中,我们定义结构来显示名称、标题等字段。我想直接将对象分配给列。 对象包含字段

only.settings = {
  editable: false,
  mode: 'inline',
  add: {
    confirmCreate: true
  },
  edit: {
    confirmSave: true,
  },
  actions: {
    delete: false
  },
  columns: {
    food: {
      title: 'Food',
      filter: false,
    },
    quantity: {
      title: 'Quantity',
      filter: false,
    },
    unit: {
      title: 'Unit',
      filter: false,
      editor: {
        type: 'list',
        config: {
          list: [
            { value: 'gm', title: 'gm' },
            { value: 'slice', title: 'slice' },
            { value: 'cup', title: 'cup' },
            { value: 'glass', title: 'glass' },
            { value: 'pcs', title: 'pcs' },
            { value: 'ml', title: 'ml' },
            { value: 'bowl', title: 'bowl' },
            { value: 'tbspn', title: 'tbspn' }
          ]
        }
      }
    },

我必须创造

array =>units[]= { value: 'bowl', title: 'bowl' },{ value: 'tbspn', title: 'tbspn' } 

想要分配 =>
列表:this.units

但它不起作用。 以防万一我通过网络服务调用获取数组。

【问题讨论】:

    标签: angular ng2-smart-table


    【解决方案1】:
    1. 构建一个包含 { value: unit, title: unit } 对象的数组
    2. 重新分配设置对象作为新对象

    代码:

    const options = [];
    for (const unit of units) {
        options.push({ value: unit.val, title: unit.name });
    }
    this.settings.columns.unit.editor.config.list = options;
    this.settings = Object.assign({}, this.settings);
    

    【讨论】:

      【解决方案2】:

      从 web 服务收到数组后,映射元素以转换为 smart-table 中使用的结构。

      地图参考:Array.map

      应该是这样的:

      config.list = unitsFromWebservice.map(function(unit) {
         return { value: unit, title: unit };
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-17
        • 1970-01-01
        • 2016-09-28
        • 2021-01-26
        • 1970-01-01
        • 2018-10-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多