Home > Software engineering >  ag-grid valueGetter function doesnt realize its part of a class
ag-grid valueGetter function doesnt realize its part of a class

Time:10-12

I am trying to wrap the ColDef definitions so I can do better than copy/paste the code for every grid column in my application...

My current issue is that I can not get the valueGetter to work

//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;

};

The idea is to have a set of classes so i can add a column for a {date, number, Listvalue, etc }and all the hard work is done, tested, etc....

Is there a way I can pass in the valueSetter function so it recognizes it is part of an object?

CodePudding user response:

You can not use this keyword inside a regular function (function), since it has its own context, you need to use arrow (=>)function.

myCD.valueGetter =  (params) => { // arrow function

         return params.data[this.fieldName]; // now this works 
     });
  • Related