In this post, I’ll show how change the row color in an XPages Dojo Data Grid based on whether the row is hovered over, selected, or is an odd-numbered row.
Dojo Data Grid Series
Dojo Grids in XPages — All Blog Posts
Row Styling
By default, the grid will provide alternate row coloring and different styles when you select or hover over a row. This image shows all three of those styles in effect.
However, as soon as you add any code into the event handler, you lose the hover, selected, and alternate row styles.
The client-side onStyleRow allows us to re-apply this styling or change it. The event executes as each row is drawn in the grid, which is the timing we need in order to dynamically set the row styling. See my previous post on the onStyleRow event for more information.
Restoring the Default Styles
To restore the default styles, you can use the same class names that the grid uses to apply the styles.
- Alternate Rows – dojoxGridRowOdd
- Row Hover – dojoxGridRowOver
- Row Selection – dojoxGridRowSelected
// This event automatically receives an object with information about the current row. It's not named, so we refer to it by arguments[0] var row = arguments[0]; if (row.over) { row.customClasses = 'dojoxGridRowOver'; } else if (row.selected) { row.customClasses = 'dojoxGridRowSelected'; } else if (row.odd) { row.customClasses = 'dojoxGridRowOdd'; }
Customizing the Styles
Now that you understand how this works, it is just as easy to change the styles completely in these three cases.
Just define classes in CSS and include the style sheet on the page and you use the same technique to append classes to rows as needed.
In this hideous example, alternate rows are set to blue, selected rows are set to yellow, and the hovered row is set to red.
var row = arguments[0]; if (row.over) { row.customClasses = 'redRow'; } else if (row.selected) { row.customClasses = 'yellowRow'; } else if (row.odd) { row.customClasses = 'blueRow'; }
