How to change styles in ag-grid on event?

Viewed 953

I have looked in the ag-grid docs, but haven't found a solution for my problem.

I want for row to change style on change event, or in other words mark it dirty.

Currently my code is like this:

component.html:

<ag-grid-angular
    class="ag-theme-material table"
    [gridOptions]="gridOptions"
    (gridReady)="onGridReady($event)"
    (cellValueChanged)="cellValueChanged($event)">
</ag-grid-angular>

component.ts

onGridReady(params) {
    this.gridApi = params.api;
}

cellValueChanged(e) {
    const { data, oldValue, newValue } = e;

    if (newValue != oldValue) {
      this.gridOptions.rowStyle = { background: "#E4FFF9" };
    }
}

Problem is that on first change event the style isn't applied and when I am re-applying data, all of the rows style's get changed even though the code doesn't even go in to the "IF" condition.

Re-applying data with gridApi.setRowData([]) then gridApi.setRowData(data).

private dataChange() {
    this.buyerGridApi.setRowData([]);
    let gridData = [];

    gridData = [...some code]

    this.gridApi.setRowData(gridData);
}
1 Answers

You can use getRowStyle to dynamically get the row style after a data change. See https://www.ag-grid.com/javascript-grid-row-styles/#refresh-of-styles

In your situation, I would set a dirty property on the data, using rowNode.setData. This will trigger a re-render of the row, and will re-run the getRowStyle function. See https://www.ag-grid.com/javascript-grid-data-update/#updating-rownodes-data

Here is a quick example, where clicking on a row will set a dirty property and change the style: https://stackblitz.com/edit/angular-ag-grid-dynamic-style?embed=1&file=src/app/app.component.ts

Related