【问题标题】:ag-grid valueGetter function doesnt realize its part of a classag-grid valueGetter 函数没有实现它的类的一部分
【发布时间】:2021-11-30 10:38:54
【问题描述】:

我正在尝试包装 ColDef 定义,这样我可以比复制/粘贴应用程序中每个网格列的代码做得更好...

我当前的问题是我无法让 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;

};

这个想法是有一组类,这样我就可以为{日期、数字、列表值等}添加一列,所有的艰苦工作都已经完成、测试等......

有没有办法可以传入 valueSetter 函数,以便它识别它是对象的一部分?

【问题讨论】:

    标签: typescript ag-grid-angular


    【解决方案1】:

    你不能在常规函数(function)中使用this关键字,因为它有自己的上下文,你需要使用箭头(=>)函数。

    myCD.valueGetter =  (params) => { // arrow function
    
             return params.data[this.fieldName]; // now this works 
         });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 2021-10-14
      • 2016-06-15
      • 2018-10-04
      • 2016-09-07
      • 2020-06-19
      • 2023-03-03
      相关资源
      最近更新 更多