This is a good use for CSS and styles rather than assigning the color in a script. The browser would manage alternating the background color and sorting wouldn't be an issue.
The default Perspective table seems to use css classes ia_table__body__row--even and ia_table__body__row--odd.
If you don't mind a sledgehammer approach, you could add the following to Styles/stylesheet.css
Any HTML color designation would work instead of blue and yellow.
The default perspective theme uses variables (var(--neutral-90)), so a more elegant approach would be to set the variable instead of hardcoding the values.
However, it brings up a slightly different problem. Sometimes I need the rows to be a different shade of colors based off of the population for example. If the population is greater than 100,000 use these colors, otherwise use these other colors.
It is possible, even preferred.
Conceptually you wouldshould add style names to the styles folder:
Then assign the style name to the element in your load script Instead of "light" and "dark" you would use one of the style names (e.g., "high-high", "high", "normal-range", "low", "low-low")
You can add multiple styles to an element and the last one wins.
If you want alternating high-high (high-high-light and high-high-dark) then perhaps a different way to use color might be considered. Implementing that will be tricky - you will be fighting the tools - and the result might not be as readable and usable as it could be.
It seems like you should be able to add a map transform, but you may need to do it in your load script. As this data isn't changing on Sort you will not have your original problem.
Something like
population = item["population"]
if population > 1000000:
populationClass = "Test/Metropolis"
elif population > 100000:
populationClass = "Test/City"
elif population > 10000:
populationClass = "Test/Town"
elif population > 1000:
populationClass = "Test/Village"
else:
populationClass = "Test/Community"
I am not saying this is a good thing to do, but if you want to assign background color for the row based on population size as discussed above and still have alternation between even and odd rows you could adjust the brightness for even numbered rows in the stylesheet.css
Hmmmmmmm, I haven't been able to make any method work so far. When you sort a table the data displayed seems to be different than the data in the data property.
I think I have to mark this as unresolved for now.
I thought about my original problem some more and I think I uncovered a bug with the sorting feature on a table or at least a discrepancy between how the table sorts things and how SQL sorts things.
Because to solve my problem I could sort by whatever criteria I wanted in the named query itself and then just color the rows as needed. It wouldn't necessarily matter if I sorted ASCENDING or DESCENDING.
So we can see the named query sorts alphabetically but the Ignition table sorts all capital letters first and then sorts all lower case letters.
So I think I have to turn off the table sorting ability and add some user friendly buttons to call a different query to sort how I want to sort things or setup my own coding to manipulate the dataset, color it, and then display it.