How to provide a background color for an entire row in ag grid based on a certain value in a column?

家住魔仙堡 提交于 2019-12-01 00:01:01

问题


I need to provide a background color for an entire row in ag grid based on a condition in a column. I found no such examples where entire row is colored based on a certain value in a column..


回答1:


The previous answer is somewhat outdated (although still correct and working) and now we have some more control over the styling of the grid. You could use getRowStyle(params) for this job, just like this:

gridOptions.getRowStyle(params) {
    if (params.data.myColumnToCheck === myValueToCheck) {
        return {'background-color': 'yellow'}
    }
    return null;
}

Obviously, myColumnToCheck would be the column you're checking your value against (the same name you input in the id/field property of the colDef object), and myValueToCheck would be the value you want said column to have to make the row all yellow.




回答2:


Answer 2 is correct, but the syntax used is wrong, and caused me several problems trying to sort it out. Trying to minify the answer 2 code barfed, for example. It did work, but it's not proper syntax as far as I can see.

Note, this can be done inline, or with an external function, 2 different ways:

SEPARATE FUNCTION:

vm.gridOptions = {
    columnDefs: columnDefs,
    getRowStyle: getRowStyleScheduled
}

function getRowStyleScheduled(params) {
    if (params.selected && params.data.status === 'SCHEDULED') {
        return {
            'background-color': '#455A64',
            'color': '#9AA3A8'
    }
    } else if (params.data.status === 'SCHEDULED') {
        return {
            'background-color': '#4CAF50',
            'color': '#F4F8F5'
        };
    }
    return null;
};

INLINE:

vm.gridOptions = {
    columnDefs: columnDefs,
    getRowStyle: function(params) {
        if (params.selected && params.data.status === 'SCHEDULED') {
            return {
                'background-color': '#455A64',
                'color': '#9AA3A8'
            };
        } else if (params.data.status === 'SCHEDULED') {
            return {
                'background-color': '#4CAF50',
                'color': '#F4F8F5'
            };
        }
        return null;
    }
}



回答3:


I set different color for even and odd rows you can do it in any way..

    $scope.gridOptions.getRowStyle = function getRowStyleScheduled(params){      
       if(parseInt(params.node.id)%2==0) {
          return {'background-color': 'rgb(87, 90, 90)'}
       }else {
          return {'background-color': 'rgb(74, 72, 72)'}
       }
    };



回答4:


You can't change the background color of an entire row in one command. You need to do it through the cellStyle callback setup in the columnDefs. This callback will be called per each cell in the row. You need to change the color of the row by changing the color of all the cells.

See the following column definition

{
   headerName: "Street Address", field: "StreetAddress", cellStyle: changeRowColor
}

You need to do this for all your columns.

Here is your changeRowColor function.

function changeRowColor(params) {

   if(params.node.data[4] === 100){
      return {'background-color': 'yellow'};    
   } 

}

It changes the color of a row if the value of the third cell is 100.




回答5:


I hope this helps others. A very common use case in any table or grid including AG Grid is going to be to set the even/odd background color of the whole row of the entire table in a performant way. ALSO, this needs to still work when SORTING.

ALL OF THESE WAYS OF DOING THIS IN AG-GRID ARE WRONG. Even though they WILL work without sort, they will not update properly when you go to use sorting. This is due to something the ag-grid team refers to in this issue https://github.com/ag-grid/ag-grid-react/issues/77 as initialization time properties.

// Initialization problem
getRowClass = (params) => {
    if (params.node.rowIndex % 2 === 0) {
        return this.props.classes.rowEven;
    }
};
<AgGridReact
    getRowClass={this.getRowClass}
>

// Initialization problem
getRowStyle = (params) => {
    if (params.node.rowIndex % 2 === 0) {
        return this.props.classes.rowEven;
    }
};
<AgGridReact
    getRowStyle={this.getRowStyle}
>

// Initialization problem
rowClassRules = {
    rowEven: 'node.rowIndex % 2 === 0',
}
rowClassRules = {
    rowEven: (params) => params.node.rowIndex % 2 === 0,
}
<AgGridReact
    rowClassRules={this.rowClassRules}
>

// Trying to change the key so a rerender happens
// Grid also listens to this so an infinite loop is likely
sortChanged = (data) => {
    this.setState({ sort: Math.random()})
}
<AgGridReact
    key={this.state.sort}
    onSortChanged={this.sortChanged}
>

Basically, most stuff in grid is just read once and not again, probably for performance reasons to save rerenders.

You end up with this problem when sorting when doing any of the above:

THE FOLLOWIUNG IS THE RIGHT WAY TO ACHIEVE EVEN ODD COLORING: The correct way to add even/odd functionality in ag-grid is to apply custom css styles as follows:

You will need to overwrite/use ag variables as mentioned in the docs here:https://www.ag-grid.com/javascript-grid-styling/#customizing-sass-variables

The names of the variables in our case are .ag-grid-even class name, or the .ag-grid-odd class name. You of course only need one if you just want an alternating color to help with visibility. For our purposes we only needed one.

Here is how this process looked in our repo: 1. Make a custom css file that overwrites/uses some of these ag- class variable names. We call it ag-theme-custom.css (I believe it needs to be a css file).

Note: We also have sass variables so this file just has a comment that this color I am adding in css is the value for our variable $GREY_100 so you don't need that part

You now will get the same result but it will still work when sorting.



来源:https://stackoverflow.com/questions/34200358/how-to-provide-a-background-color-for-an-entire-row-in-ag-grid-based-on-a-certai

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!