我正在根据通过web服务接收的数据动态地构建ag-Grid列(下面是我的代码片段,其中我正在构建我的列定义;有关整个示例,请参阅柱塞):
for (let i = 0; i < cr.attributes.length; i++) {
let displayAlignment = '';
switch (cr.attributes[i].dispAlign) {
case 'C':
displayAlignment = 'center';
break;
case 'L':
displayAlignment = 'left';
break;
case 'R':
displayAlignment = 'right';
break;
default:
throw new Error('this will never happen, dispAlign for database must have value');
}
const displayMask = cr.attributes[i].dispMask;
// console.log('displayMask: ', displayMask);
const dataType = cr.attributes[i].dataType;
// console.log('dataType: ', dataType);
// console.log('cr attributes: ' , cr.attributes );
childColDefsString = childColDefsString +
'{"field": "' + cr.attributes[i].label + '"' +
', "cellStyle": {"textAlign": "' + displayAlignment + '"}' +
', "valueFormatter": "this.customValueFormatter(datatype, displayMask)"' +
', "width": 175, ' + '"hide": ' + cr.attributes[i].hide + '},';
}
在构建我的列时,我根据从web服务接收到的列类型(正如预期的那样工作),为列对齐添加了一个cellStyle。我还试图添加一个试图调用自定义格式化程序的valueFormatter。不幸的是,当rowData被应用到网格(单击"Apply Row Data“按钮)时,我遇到了以下ag-Grid引发的异常(通过Chrome ->检查->控制台可以看到):
Processing of the expression failed ag-grid.js:10595
Expression = customValueFormatter(datatype, displayMask) ag-grid.js:10596
Exception = ReferenceError: customValueFormatter is not defined
下面是我创建的柱塞示例的url:https://plnkr.co/edit/LcA5dRU9g8huLUWv3syZ?p=preview
我尝试了几次ag-Grid示例https://www.ag-grid.com/javascript-grid-value-setters/的迭代,但都没有效果。
发布于 2019-02-28 07:45:11
不太清楚为什么在buildAvailableFields
中使用buildAvailableFields
进行操作。
parentColDefsString = parentColDefsString +
'{"headerName": "' + cr.label + '", ' +
'"children": [';
不管怎么说,这不是你所面临的问题。
正是在您的情况下,您在这里有一个错误:
"valueFormatter": "this.customValueFormatter(datatype, displayMask)"'
在这里,您已经混合了输入属性,并且忘记了将params
值放入其中。如果要向valueFormatter
函数添加额外的输入参数,则应将其包装到inner function
中。
valueFormatter: (params) => this.customValueFormatter(params, datatype, displayMask);
--或纯JS
valueFormatter: function(params) {
this.customValueFormatter(params, datatype, displayMask);
}
但是,如果出现“string”连接,则不会对此函数进行计算。
下一个问题是:
为什么不用standart结构操作,就容易多了。
columnRequest.forEach(columnData=>{
// your parent
let column: ColDef={
headerName: columnData.label,
children:[]
}
// handle children
column.attributes.forEach(childrenColumnData=>{
let childrenColumn:ColDef={
headerName: childrenColumnData.label,
field: childrenColumnData.label
}
// condition for valueFormatter if it needed
if(true){
childrenColumn.valueFormatter = (params)=>this.customValueFormatter(params, yourAdditionalValue1, yourAdditionalValue2);
}
})
this.colums.push(column);
}
https://stackoverflow.com/questions/54915562
复制相似问题