Ag-grid RowStyle with selected row - ag-grid

I am using ag-grid 5.1.2
I've configured a getRowStyle function to set the background-color for certain items.
I've noticed that has overridden the 'selected row' color now, so when a row is selected the background will not change to indicate as such.What is the correct way to still let highlighted row color work with the custom getRowStyle. Here is the typescript for the rowStyle function:
self.customRowStyle = function (params: AgParams) {
if (!params.data.isShaded) {
return {
"background-color": "#CEDDDD",
};
}
return null;
};

In your CSSyou can choose not to target the selected row.
ag-Grid adds the class onto the row that is returned from your getRowStyle callback
It also adds .ag-row-selected for rows that get selected.
You can simply use CSS to only target not selected rows or even apply a different style to the rows that are both selected and have your custom class.
Here is an example:
CSS
.bg-red.ag-row:not(.ag-row-selected) {
background-color: red ;
}
OR different style for both selected and bg-red
.bg-red.ag-row {
background-color: red;
}
.bg-red.ag-row.ag-row-selected {
background-color: pink;
}
JS
rowClassRules: {
'bg-red': () => {
return true; // applies class bg-red to all rows
},
},
Here is a live running code example of this in action.
Also here is another live example that overrides row styles on click of a button, but this doesn't involve styling callbacks:
Is this what you are looking for?

Use the getRowClass gridOption instead of getRowStyle. Then in CSS set the appropriate styles for your background row and the brackground row when highlighted.

Related

Change pinned row color while selecting ag-grid row

I am currently working on Ag Grid.initial load i have changed the pinned row color with the help of below code.
<ag-grid-angular[getRowStyle]="getRowStyle">
</ag-grid-angular>
getRowStyle(params: any): any {
if (params.node.rowPinned === 'top') {
return {'font-weight': '400',
'background-color': 'blue' };
}
}
Do we have any other way to change the background color of pinned row on selectionChanged event.
expecting a better suggestion
Thanks in advance.

ag-grid apply style to Total footer

I am using groupIncludeTotalFooter to display footer. I was able to change footer text using footerValueGetter.
However i am trying to find a better way to apply some styles to the Total footer row.
Currently I am doing some CSS selection as follows. Wanted to know if this is the only way or there is better way.
.my-component .ag-row-footer {
background-color:#FFFFFF;
}
You can achieve this programatically by leveraging the Grid Callback method getRowStyle:
gridOptions = {
getRowStyle: (params) => {
if (params.node.footer) {
return { fontWeight: 'bold' };
}
},
}
This will make the footer bold, as shown in the following example
There are other ways of achieving this programatically, if you need more flexibility, in the documentation: https://www.ag-grid.com/javascript-grid-row-styles/

How can I change the styling of a column based on whether the column is pinned to the left?

Is it possible to change the style of a column based on whether it's pinned or not?
I'm able to change the style based on the value while the table is rendered for the first time. What I'm trying to do is change the style when the column is pinned using the mouse (drag and pin).
I'm able to figure out which column has been pinned by firing the ColumnPinnedEvent in gridOptions. I tried modifying the cellClass of the column obtained from 'event.column' but it does not get reflected on the table.
onColumnPinned(event: ColumnPinnedEvent) {
const column = event.column;
if (column) {
const columnDef = column.getColDef();
const userProvidedColDef = columnDef;
userProvidedColDef.cellStyle = event.pinned ? { color: 'white', backgroundColor: 'black' } : {};
column.setColDef(columnDef, userProvidedColDef);
}
}
You can achieve the same by just with the CSS.
Have a look at the plunk I've created: Column Pinning and styling. Add or remove any column to see the styles updated for it.
.ag-body-viewport .ag-pinned-left-cols-container .ag-row {
background-color: yellow;
}
Here .ag-body-viewport .ag-pinned-left-cols-container hierarchy is important. Just using .ag-pinned-left-cols-container .ag-row will not work as some row levels' styling of ag-grid will overwrite it.
So far this information is enough to solve your challenge, let me know in addition to this, you have some more details to provide.

Ag-grid master/detail: custom row style when detail expanded

for my work I use angular 7 and ag-grid enterprise latest version. I have an ag-grid table configured with master / detail feature and I need to customize the style of the row when the detail is expanded. For example, I would like to change the background color of the row only when its detail is expanded. I tried with the CSS rule:
.ag-theme-material .ag-row-focus {
background-color: $dark-grey-blue !important;
}
but I don't get the correct behavior, because the row changes color clicking on it even without the detail being expanded, while I want it to change color only if the detail is expanded.
I tried to look at the official documentation of ag-grid, but I didn't find indications for this specific case.
Can you help me please?
Thank you
You can accomplish this with the grid callback getRowStyle and detect if the node is expanded or not:
gridOptions.getRowStyle = (params) => {
if (params.node.expanded) {
return { background: 'red' };
} else {
return { background: 'green' };
}
}

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

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..
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.
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.
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. For example an external 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;
};
You can add CSS classes to each row in the following ways:
rowClass: Property to set CSS class for all rows. Provide either a string (class name) or array of strings (array of class names).
getRowClass: Callback to set class for each row individually.
<ag-grid-angular
[rowClass]="rowClass"
[getRowClass]="getRowClass"
/* other grid options ... */>
</ag-grid-angular>
// all rows assigned CSS class 'my-green-class'
this.rowClass = 'my-green-class';
// all even rows assigned 'my-shaded-effect'
this.getRowClass = params => {
if (params.node.rowIndex % 2 === 0) {
return 'my-shaded-effect';
}
};
You can define rules which can be applied to include certain CSS classes via the grid option rowClassRules.
The following snippet shows rowClassRules that use functions and the value from the year column:
<ag-grid-angular
[rowClassRules]="rowClassRules"
/* other grid options ... */>
</ag-grid-angular>
this.rowClassRules = {
// apply green to 2008
'rag-green-outer': function(params) { return params.data.year === 2008; },
// apply amber 2004
'rag-amber-outer': function(params) { return params.data.year === 2004; },
// apply red to 2000
'rag-red-outer': function(params) { return params.data.year === 2000; }
};
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.
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)'}
}
};
If you don't need to set the background color conditionally(based on the row data), it is not recommended to use rowStyle, as written on the row style documentation page:
// set background color on even rows
// again, this looks bad, should be using CSS classes
gridOptions.getRowStyle = function(params) {
if (params.node.rowIndex % 2 === 0) {
return { background: 'red' };
}
}
Instead, you can change the row colors using css:
#import "~ag-grid-community/dist/styles/ag-grid.css";
#import "~ag-grid-community/dist/styles/ag-theme-alpine.css";
#import "~ag-grid-community/dist/styles/ag-theme-balham.css";
#import "~ag-grid-community/src/styles/ag-theme-balham/sass/ag-theme-balham-mixin";
.ag-theme-balham {
#include ag-theme-balham((
// use theme parameters where possible
odd-row-background-color: red
));
}
If you are using AdapTable then the simplest way is to use a Conditional Style and apply it to a whole row.
The advantage of this is that it can be at run-time easily by users also.
https://demo.adaptabletools.com/style/aggridconditionalstyledemo