2016-08-08 33 views
4

我想從用戶的列表中進行選擇:AG-cellEditor的電網與對象選擇值

user.ts

export class User 
{ 
    constructor (public id: number, public userName : string){} 
} 

列定義是這樣的:

this.columns = [ 
       {headerName: "Assigned", field:"user.userName", 
       editable: true ,cellEditor: "select", 
       cellEditorParams: {values : this.users.map(u=> u.userName)}, 
] 

我希望能夠從列表中選擇一個用戶並獲取cellValueChanged對象。

有沒有一個選項,user將是字段,而不是字符串值,仍然user.username將顯示在單元格中?

回答

2

最後我找到了一個解決辦法,得到'select'keyvalue一起使用。

var colorsNames = []; 
colors.forEach(color=> { 
    colorsNames.push(color.name); 
}) 
... 

{ 
    headerName: "Color", 
    field: "colorId", 
    width: 150, 
    editable: true, 
    cellEditor: 'select', 
    cellRenderer: function (data: any) { 
    var color = colors.find(color => color.id == data.value || color.name == data.value); 
    // here I've added the check for 'color.id' and 'color.name' because initailly from DB will com the id and afterwards form selectparams will come the name 
    return color.name; 
    }, 
    onCellValueChanged: function (data: any) { 
    /** 
    * because 'select' does not offer us the possibility to use 'key-value' as traditional, 
    * we will use only values in 'select' and changed to 'id' when will be saved. 
    */ 
    var serviceTypeName = data.data.serviceTypeId; 
    data.data.serviceTypeId = serviceTypes.find(serviceType => serviceType.name == serviceTypeName).id; 
    }, 
    cellEditorParams: { 
    values: colorsNames 
    } 
}, 

的想法是,裏面select params我們只會給字符串,我們會盡量找對象的基礎上,nameid。重要的是我們一致認爲name是獨一無二的領域。

經過努力讓它工作,我意識到確實select是一個非常糟糕的解決方案。工作不正常,我不會建議使用它。

@Yonatan Lilling對於任何問題,請讓我知道。

0

我發現JavaScript的一個解決方案https://www.ag-grid.com/javascript-grid-reference-data/?framework=angular#gsc.tab=0

創建我的數組:

var Etat_acces = {"1": "Annulée", "2": "Validée", "3": "A valider CEX", "4": "Demandée", "5":"Initialisée"}; 

,並在我的columnDefs:

{ 
headerName: "Etat Ni", field: "etat_acces", editable: true, cellEditor:'select', 

      cellEditorParams: { 
       values: extractValues(Etat_acces) 
      }, 
      valueFormatter: function (params) { 
       return lookupValue(Etat_acces, params.value); 
      }, 
      valueParser: function (params) { 
       return lookupKey(Etat_acces, params.newValue); 
      } 

} 

和三個功能:

function extractValues(mappings) { 
return Object.keys(mappings); 
} 

function lookupValue(mappings, key) { 
    return mappings[key]; 
} 

function lookupKey(mappings, name) { 
    for (var key in mappings) { 
     if (mappings.hasOwnProperty(key)) { 
      if (name === mappings[key]) { 
       return key; 
      } 
     } 
    } 
} 

我希望這可以有用;)