使用ag-grid在未定义字段上编辑列

时间:2018-04-03 09:01:56

标签: javascript angular ag-grid ag-grid-ng2

我已经为列定义配置了ag-grid,如下所示

    {
        headerName: 'Model Level',
        field: 'ModelLevelTimeSeries.Id.Value',
        editable: false,
        cellRendererFramework: RenderEntityComponent,
        cellStyle: { textAlign: "center" },
        suppressSizeToFit: true,
    }

基本上后端的字段可以返回null

"ModelLevelTimeSeries": {
        "Id": null
    }

"ModelLevelTimeSeries": {
    "Id": {
        "Value": "dec70622-3ab3-4b47-a324-f69a14a53beb"
    }
}

当我编辑单元格值时,它会在null方法中为params.value返回agInit
我正在使用“ag-grid”:“^ 17.0。 0" ,     “ag-grid-angular”:“^ 17.0.0”。

3 个答案:

答案 0 :(得分:0)

在你的列defincation中添加,cellRnderer函数

cellRenderer: params => params.value ? params.value : ''

现在就像这样

   {
        headerName: 'Model Level',
        field: 'ModelLevelTimeSeries.Id.Value',
        editable: false,
        //add below line
        cellRenderer: params => params.value ? params.value : ''
        cellRendererFramework: RenderEntityComponent,
        cellStyle: { textAlign: "center" },
        suppressSizeToFit: true,
    }

答案 1 :(得分:0)

您可以为coldefs使用值设置器

{
  headerName: 'Model Level',
  field: 'ModelLevelTimeSeries.Id.Value',
  editable: false,
  cellRendererFramework: RenderEntityComponent,
  cellStyle: { textAlign: "center" },
  suppressSizeToFit: true,

  valueSetter: function(params: any){
     if (params.oldValue !== params.newValue) {
         params.data[params.colDef.field] = (params.newValue) !== "" ? params.newValue.toUpperCase() : "";
         return true;
     } 
     else {
         return false;
     }
  }
}

答案 2 :(得分:0)

根据我的经验,如果该字段是.nested.key,则在valueSetter中使用params.data [params.colDef.field]将不起作用,因为params.colDef.field只会返回类似“ ModelLevelTimeSeries.Id”的字符串.Value”

目前,我依靠lodash的set()在valueSetter中进行操作:

import * as set from 'lodash/set'

changeDetectValueSetter (params) {
  if (params.oldValue !== params.newValue) {
    set(params.data, params.colDef.field, params.newValue) // had to use lodash set, because params.colDef.field is a string representation of a nested object key
    // params.data.instruction.billingrecord.invoice_number = params.newValue // could have done this, but it's fixed for that field.
    // params.data[params.colDef.field] = params.newValue // this does not work at all if the field is.a.nested.key
    return true
  } else {
    return false
  }
},