7
votes

I have a table that loads from dynamically changing data. It refreshes every 5 secs. I'm using ag-grid for it using this example: https://www.ag-grid.com/javascript-grid-sorting/index.php

Is it possible to change color of the cells whose values have changes, like suppose a cell value is 100 and it becomes (less than this i.e. <100) so make the cell red color, id it becomes greater, make it green color. I'm trying to do it using this example: https://www.ag-grid.com/javascript-grid-cell-styling/index.php

But I can't understand how to do this.

UPDATE: I'm doing it this way, but it's not changing the color:

var columnDefs = [
    {headerName: "Arr Px Slippage", field: "total_wt_arr_slp", width: 100, newValueHandler: compareValues},
    {headerName: "IVWAP Slippage", field: "total_wt_ivwap_slp", width: 100}


];

function compareValues(params) {
    if (params.oldValue > params.newValue){ 
    return {color: 'green', backgroundColor: 'black'};
    console.log(params.newValue);

    }
    if (params.oldValue < params.newValue){ 
    return {color: 'red', backgroundColor: 'black'};
    }
}
4
If you see, the ag-grid documents have a page for Refresh (ag-grid.com/javascript-grid-refresh) feature. The second example is something you can have a look. The color of the value in column Total changes if the value is greater than 20 depending upon the sum of other columns in the row. The problem I am not able to solve is how the classes are applied dynamically.jsmtslch

4 Answers

3
votes

Actually I just got mine working. You need "cellClassRules" attribute on each column where you want to change the color. Something like:

{headerName: "header", field:"field",suppressMenu: true, volatile: true, suppressMovable: true, cellClassRules: {'bold-and-red': 'x>1'} }

Here 'x' in the rule is the value of the column. Also, you need to mark your column as voaltile: true.
For volatile fields to dynamically change, you need api.softRefreshView() while you are refreshing the data.
The css class 'bold-and-red'can be defined in your html file like: .bold-and-red { color: darkred; font-weight: bold; }

3
votes

Here is the snippet of code where you can change the color of the ag grid cell text and background color.

 var colDef = {
            name: 'Dynamic Styles',
            field' 'dummyfield',
            cellStyle: function(params) {
                if (params.node.value=='Police') {
        //Here you can check the value and based on that you can change the color
                    //mark police cells as red
                    return {color: 'red', backgroundColor: 'green'};
                } else {
                    return null;
                }
            }
        }
2
votes

What you have written is mostly correct:

function compareValues(params) {
if (params.oldValue > params.newValue){ 
return {color: 'green', backgroundColor: 'black'};
console.log(params.newValue);

}
if (params.oldValue < params.newValue){ 
return {color: 'red', backgroundColor: 'black'};
}

And the info Jarod Moser gave you about the params object for this callback is spot on and important.

The issue you are having is that you are trying to return {style} but you can't do that. You need to have access to the html element (the containing <div>) and set a class on it (having the class defined with the style you desire). I have done this in ag-grid where I have access to params.eGridCell but within this particular callback eGridCell is not available. If I find a good way to get to the <div> I'll edit this post with what I found.

EDIT - Additional Information

I don't think I would use the newValueHandler for what you are trying to do.

You don't say how you are updating the data, but you do say it is possibly updated every 5 seconds.

However you are deciding to update a cell, you could add to the rowData object a property 'origValue', and before updating the cell value, set the current value to 'origValue' and then set the new value to value. THEN... you could use the cellClass column property, using a callback function, and compare the new value to the 'origValue' and return the desired style.

Examples from the Documentation:

// return class based on function
var colDef3 = {
    name: 'Function Returns String',
    field' 'field3',
    cellClass: function(params) { return (params.value==='something'?'my-class-1':'my-class-2'); }
}


// return array of classes based on function
var colDef4 = {
    name: 'Function Returns Array',
    field' 'field4',
    cellClass: function(params) { return ['my-class-1','my-class-2']; }
}

The params object for cellClass has access to the row data, and will be able to compare the new and orig values.

There are many options once you get digging. Choose what you think is best.

1
votes

Looks like you should be able to use the newValueHandler which is an attribute of each column.

From the docs:

If you want to use the simple text editing, but want to format the result in some way before inserting into the row, then you can provide a newValueHandler to the column. This will allow you to add additional validation or conversation to the value.

newValueHandler is provided a params object with attributes:

  • node: The grid node in question.
  • data: The row data in question.
  • oldValue: If 'field' is in the column definition, contains the value in the data before the edit.
  • newValue: The string value entered into the default editor.
  • rowIndex: The index of the virtualised row.
  • colDef: The column definition.
  • context: The context as set in the gridOptions. api: A reference to the ag-Grid API.

So something along the lines of:

var colDefs = [{
    header: 'comparing to previous val'
    newValueHandler: compareValues
}]

function compareValues(params){
    if (params.oldValue > params.newValue){ //make it red}
    if (params.oldValue < params.newValue){ //make it green}
}