Search code examples
ag-gridag-grid-angular

How to compare two data in the same row in Ag Grid?


I'm using Angular and Ag Grid.

I have a contract_start column and and contract_end column (date type) in an Ag Grid table. I want that the contract_end's cell's value can only be changed when the contract_end cell's date is bigger than the contract_start cell's date (in the same row).

I've searched for solutions but unfortunately I've only find information on filtering by date.


Solution

  • In your column definition for contract_end, you can specify an "editable" property, which can be either a boolean or a function that returns a boolean.

    You would use the function version. The parameter to the function is an EditableCallbackParams object, documented at https://www.ag-grid.com/javascript-data-grid/column-properties/#reference-editing

    In the function, you can access the row data, and compare the two column values.

    For instance:

    editable: function(params: EditableCallbackParams): boolean {
       return params.data.contract_end.getTime() > params.data.contract_start.getTime();
       
    }
    

    Note that I typed this from memory - I haven't tried to compile it - YMMV