簡體   English   中英

如何在ng2智能表中推送自定義數組對象

[英]How to push custom array object in ng2 smart table

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

但它不起作用。 以防我通過Web服務調用獲取數組。

  1. 建立{值:單位,標題:單位}對象的數組
  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);

從Web服務接收數組后,請映射元素以轉換為智能表中使用的結構。

對於地圖參考: Array.map

就像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM