Search code examples
angularag-grid

Ag grid get field value inside custom cell renderer Angular


I'm trying to get the column field value inside my cell renderer. I can do that through params.data.code inside agInit hookup method but that would make the cell renderer non reusable for other components, as field 'code' is not generic.

In the docs it says it will use valueGetter(with or wout valueFormatter) instead of field, if provided; but in the renderer params.value it's always undefined.

Column definition attemtps:

      {
        headerName: "test",
        field: 'code', 
        cellRenderer: CustomCellRendererComponent,
        cellRendererParams: {
          title: "title-for-custom-component",
          parent: this,
        }
      }

      {
        headerName: "test",
        field: 'code', 
        cellRenderer: CustomCellRendererComponent,
        cellRendererParams: {
          title: "title-for-custom-component",
          parent: this,
        },
        valueGetter: (params) => return params.data.code,
      }

     {
        headerName: "test",
        field: 'code', 
        cellRenderer: CustomCellRendererComponent,
        cellRendererParams: {
          title: "title-for-custom-component",
          parent: this,
          value: (params) => {params.data.code}
        },
      }

Cell renderer:

export class CustomCellRendererComponent implements ICellRendererAngularComp {

 refresh(params: ICellRendererParams): boolean {
    return false;
  }

  agInit(params: ICellRendererParams): void {
    this.params = params;
    //1st try will print undefined
    //2nd try will print undefined
    //3rd try will print (params) => { params.data.code; }
    console.log("valuegetter:"+this.params.value)

    //this one works, but here I'm accessing the 'code' field from the row, so it's not reusable
    console.log("valuegetter:"+this.params.data.code)
    //I am able to access title and parent params as well
   
  }

 }

Is it possible to get the field value without accessing row data from within the renderer? Or must I go through params.data to achieve so?


Solution

  • After debugging and following the data source, params.value it is indeed working.

    The value for that specific field was explicitly being set to undefined somewhere else in the code.