In my last past, I showed how you can dynamically set row colors in views and repeats based on data in each entry. In this post, I’ll show how you can take it a step further and use that technique along with providing default alternating row colors.
In my task list, I want to alternate the rows between white and light gray to provide visual separation of the data. But I want to override those defaults and display the row with a yellow background if the task is expiring and a red background if the task is late.
Here’s an example of a repeat control using this combined technique:
Here’s the updated css:
.repeatRow { background-color: #EEEEEE; } .repeatRowAlt { background-color: #FFFFFF; } .yellowRow { background-color: #FFFF00; } .redRow { background-color: #FF0000; }
NOTE: It is important to define the red and yellow classes after the default row classes. Whichever ones are defined later will take precedence when multiple classes are assigned to a row.
Here’s the computed style class code to make it work:
var status = varTask.getColumnValue('Status'); var cssClass; if (rowIndex % 2 == 0) { cssClass = 'repeatRow'; } else { cssClass = 'repeatRowAlt'; } if (status == 'Expiring') { cssClass += ' yellowRow'; } else if (status == 'Late') { cssClass += ' redRow'; } return cssClass;
The code checks the row number and starts with a class of repeatRow or repeatRowAlt. If the status is Expiring or Late, it adds another class to the list. Note that there’s a space before the class name in lines 11 and 13. This is because I’m retaining the first class name and adding a second class to it.
I’m doing it this way because I tend to define font and spacing settings in the repeatRow and repeatRowAlt classes. By assigning two classes to rows as needed, I can retain those settings while overriding the background color when required.
