ag-grid valueGetter 函数没有实现它的一部分 class
ag-grid valueGetter function doesnt realize its part of a class
我正在尝试包装 ColDef 定义,这样我就可以比 copy/paste 我的应用程序中每个网格列的代码做得更好...
我目前的问题是我无法让 valueGetter 工作
//this is called from my angular COMPONENT to define the grid column
this.columnDefs.push(new AgColDef( 'Description').mycolDef());
mycolDef() : ColDef{
let myCD : ColDef {field : this.fieldName};
myCD.field = this.fieldName;
myCD.headerText = 'header ' + this.fieldName;
myCD.editable = true;
myCD.valueGetter = (function (params) {
return params.data.RevisionNumber; //this works. i can hard code the field
//return params.data[this.fieldName]; ///this does not work, the function does not know it belongs to an object?
});
return myCD;
};
我的想法是拥有一组 类 这样我就可以为 {date, number, Listvalue, etc } 添加一个列,所有的艰苦工作都已完成、测试等....
有没有一种方法可以传入 valueSetter 函数,以便它识别它是对象的一部分?
您不能在常规函数 (function
) 中使用 this
关键字,因为它有自己的上下文,您需要使用箭头 (=>
) 函数。
myCD.valueGetter = (params) => { // arrow function
return params.data[this.fieldName]; // now this works
});
我正在尝试包装 ColDef 定义,这样我就可以比 copy/paste 我的应用程序中每个网格列的代码做得更好...
我目前的问题是我无法让 valueGetter 工作
//this is called from my angular COMPONENT to define the grid column
this.columnDefs.push(new AgColDef( 'Description').mycolDef());
mycolDef() : ColDef{
let myCD : ColDef {field : this.fieldName};
myCD.field = this.fieldName;
myCD.headerText = 'header ' + this.fieldName;
myCD.editable = true;
myCD.valueGetter = (function (params) {
return params.data.RevisionNumber; //this works. i can hard code the field
//return params.data[this.fieldName]; ///this does not work, the function does not know it belongs to an object?
});
return myCD;
};
我的想法是拥有一组 类 这样我就可以为 {date, number, Listvalue, etc } 添加一个列,所有的艰苦工作都已完成、测试等....
有没有一种方法可以传入 valueSetter 函数,以便它识别它是对象的一部分?
您不能在常规函数 (function
) 中使用 this
关键字,因为它有自己的上下文,您需要使用箭头 (=>
) 函数。
myCD.valueGetter = (params) => { // arrow function
return params.data[this.fieldName]; // now this works
});