2017-06-08 85 views
0

https://github.com/akveo/ng2-smart-table 在設置對象中,我們定義結構來顯示字段如名稱,標題等。我想直接將對象分配給列。 對象包含字段如何在ng2智能表中推送自定義數組對象

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服務調用獲取數組的情況下。

回答

0

從Web服務接收到數組後,映射要轉換爲智能表中使用的結構的元素。

在地圖參考:Array.map

這將是這樣的:

config.list = unitsFromWebservice.map(function(unit) { 
    return { value: unit, title: unit }; 
}); 
1
  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); 
相關問題