ag-grid cellEditor选择对象值

Yon*_*ing 5 typescript ag-grid

我想从用户列表中进行选择:

user.ts

export class User
{
    constructor (public id: number, public userName : string){}
}
Run Code Online (Sandbox Code Playgroud)

列定义如下所示:

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

我希望能够从列表中选择一个用户并获取cellValueChanged对象.

是否有一个选项,user将是字段而不是字符串值,仍然user.username会在单元格中显示?

Lin*_*adu 3

最后我找到了一个解决方法来'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
  }
},
Run Code Online (Sandbox Code Playgroud)

这个想法是,在内部select params我们将只给出字符串,并且我们将尝试根据 找到id对象的name。重要的是我们一致认为这name是一个独特的领域。

在设法让它工作后,我意识到这确实select是一个非常糟糕的解决方案。无法正常工作,我不建议使用它。

@Yonatan Lilling 如有任何问题,请告诉我。