Power table row and cell coloring

Can I color the rows alternating as well as highlight a cell based on the value in it?

I was able to alternate the rows by writing the code in the initialize function or i can color the cell by writing something in the configureCell but I am not able to do both. Can anyone shed some light on how I can achieve this?

1 Like

If the colour needs to change during runtime, you will have to do everything in the configureCell function.

You can set a default colour based on the row number, and make it alternating that way (different colour for even and odd rows). And then override the colour for the cells that have the “selected” flag set.

1 Like

I tried that but it does not seem to have the effect i like.

By default i want alternating rows colored. I am able to do this by have the code in initialize function.

I also want the individual cell colored based on the value so I have the code to check the value in configureCell function; as soon as I do that the alternating row coloring is lost. Looks like I can either have the rows colored or the cell colored but not both.

Any ideas?

1 Like

Can you share the code of the configureCell function?

1 Like
if selected:
	return {'background': self.selectionBackground}
elif colName == "otanks" and value > 2:
	return {'background' : 'red'}
2 Likes

Well, there’s no alternating colours in that code. If you want alternating colours, you should do something like this:

ret = {}
if rowIndex == self.selectedRow:
	ret["background"] = "#A0A0FF" 
elif rowIndex % 2 == 0:
	ret["background"] = "#F0F0F0"
else:
	ret["background"] = "#FFFFFF"
return ret
1 Like

If i did like what you suggested then I end up with only alternate row coloring. But i would like to add cell coloring in addition to that something like the attached picture

1 Like

You need to cascade additional if/else statements. The following is pseudo code to illustrate the point

if rowIndex is odd:
    rowColor =  "Grey"
    if cellHeader == "Header 2" and selectedIndex == rowIndex:
        cellColor = "Red"
    else:
        cellColor = "Grey"
else:
   rowColor =  "White"
    if cellHeader == "Header 2" and selectedIndex == rowIndex:
        cellColor = "Red"
    else:
        cellColor = "White"
1 Like

The way I do it is to build a dictionary of attributes, starting with the formatting of least precedence. Then I add or ‘override’ attributes with additional logic as needed.

attributes = {"background": "#FFFFFF", "foreground": "#000000"}
if rowIndex % 2 == 0:
	attributes["background"] = "#EEECE8"
if colName == "val":
	if self.data.getValueAt(rowIndex, "writeable") == True:
		attributes["background"] = "#FFFF47"
if colName == "alarm" and value is not None:
	attributes["text"] = None
elif colName == "unit" and value is None:
	attributes["text"] = None
	if self.data.getValueAt(rowIndex, "alarm") is not None:
		attributes["iconPath"] = "CustomIcons/Indicator_Red.png"
		if value < 1:
			attributes["iconPath"] = "CustomIcons/Indicator_Green.png"

return attributes
7 Likes

This is great !! Thanks a bunch. I was able to modify to get the desired result.

I am trying to highlight a complete row white, yellow or green based on a value in a column(10) in the cell. It is a number 0 to 100 where 0 would be white, 100 would be green and yellow is 0 thru 100.

Thought I could use something like this(self.data.getValueAt(rowIndex ,10):slight_smile: but the variations of code I could only get the column 10 to work. I don’t know how to apply the color for the whole row based on 1 column’s value of that row.

It sounds like you are only having the script run for that column. Take out any conditional that filters on that column.

The example I posted is built to format differently for cells in each column. I you want every cell in each row to be highlighted then you don;t need to check the colName variable.

My understanding is that each cell is examined for its cell formatting. I am not sure how to apply code for each cell in a row to look at another cells value and then apply the color. Any help would be appreciated.

That is correct; the extension function runs for each cell in the table.

My example posted above filters down to specific columns.

if colName == "val":
		if self.data.getValueAt(rowIndex, "writeable") == True:
			attributes["background"] = "#FFFF47"

If the cell being checked is not in that column (or one of the others in the other conditionals) then no script will be run.

If you want to run the script for all cells in a given row, simply take out the “if columnName = …” check.

Your script would be similar to this:

columnTenValue = self.data.getValueAt(rowIndex, 10) 
if columnTenValue == 0:
    return {"foreground": "#FFFFFF"}
elif columnTenValue < 100:
    return {"foreground": "#00FF00"}
return {"foreground": "#FFFF00"}
1 Like

This post may help as well.

Hi,
I’ve errors when I use rowIndex:
…expecting INDENT…
Afer so much time searching for an typping error I tried the code only changing:
rowIndex for rowView.
Then it works for me.

That suggests a Python indentation error. Pay attention to consistent indentation and use tabs.
If pasting code here then use the </> code formatting button to format your code correctly and apply syntax highlighting.

1 Like

Hey snoopyflyingace,

Can you Please share your code ? , I have exact requirement
Thank you

@srinivas_anupurapu - You're asking in a thread that has been more than a year since the last post so it's unlikely that the original posters will notice this. I happened to stumble on to this, so I will show you an example I have done.

This configures a power table with alternating light yellow and light green rows by default. If a cell contains a NULL value, then that cell is gray (without changing the rest of the row). If a cell contains a date too far in the future, the cell is light blue. If the cell contains a date whose hour is set to 12 then that cell is red.

def configureCell(self, value, textValue, selected, rowIndex, colIndex, colName, rowView, colView):
	import datetime
	if colIndex > 0 and value == None:
		return {'background': '#D5D5D5'}
	try:
		if colIndex > 0 and  value > datetime.datetime(2099,12,31):
			return {'foreground': '#3CCAE7',
					'background': '#3CCAE7'}
		elif colIndex > 0 and value.hours == 12:
			return {'background': '#FF6858'}
		elif selected:
			return {'background': self.selectionBackground}
		elif rowView % 2 == 0:
			return {'background': '#CCFFCC'}
		else:
			return {'background': '#FAFFCC'}
	except:
		if selected:
			return {'background': self.selectionBackground}
		elif rowView % 2 == 0:
			return {'background': '#CCFFCC'}
		else:
			return {'background': '#FAFFCC'}
		

Here's what it looks like...

2 Likes

Thank you so much for your support, modified the code as per my requirement.