Stefan Ivovic
Stefan Ivovic

Reputation: 105

Angular ag grid valueGetter

i am trying to make ag grid table. Basically i have about 8 columns. 3 of them are user inputs and the rest are just mathematical calculations based on some equations.

i figured valueGetter will be a good place to put them (calculations), but for one cell i need to get data from different row and trouble starts there.

this is my column definition, and valueGetter for field x is working fine because it's using data from the same row, but, for field xixi i have to get data from the previous row. i have tried with gridApi but ofc its undefined. pls help

  columnDefs: ColDef[]=[
    {field: 'r', width:150, cellEditor: 'agRichSelectCellEditor'},
    {field: 'y', width:150, cellEditor: 'agRichSelectCellEditor'},
    {field: 'x', width:150, valueGetter: function (params) {
      return params.data.r * params.data.y;
    }},
    {field: 'xixi', width:150, valueGetter: function (params){
      if(params.node.rowIndex == 0) {return 0;}
      let indexBefore = params.node.rowIndex
      if(this.gridApi == undefined){return;}
      let dataBefore = this.gridApi.getDisplayedRowAtIndex(indexBefore-1);
      let data = params.data.x - dataBefore.x;
      return data;}},
    {field: 'koef', width:150, cellEditor: 'agRichSelectCellEditor'},
    {field: 'proizvod', width:150},
    {field: 'deltaY', width:190},
    {field: 'delta4y', width:150},
    {field: 'proizvodSum', width:150},
  ]

Upvotes: 1

Views: 7433

Answers (1)

ViqMontana
ViqMontana

Reputation: 5688

There are a few solutions to your problem, easiest solution would be to use the grid API that is available in the params parameter within your valueGetter.

Use the following code for your 'xixi' columnDef:

{
      field: 'xixi',
      width: 150,
      valueGetter: function (params) {
        if (params.node.rowIndex == 0) {
          return 0;
        }
        let indexBefore = params.node.rowIndex;
        if (params.api == undefined) {
          console.log('her');
          return;
        }
        let dataBefore = params.api.getDisplayedRowAtIndex(indexBefore - 1);
        let data = params.data.x - dataBefore.data.x;
        return data;
      },

Demo.

Upvotes: 1

Related Questions