# Power table color grouping based on 1 cell value

**URL:** <https://forum.inductiveautomation.com/t/power-table-color-grouping-based-on-1-cell-value/44037>\
**Category:** Ignition\
**Tags:** ignition79, ignition80\
**Created:** [March 3, 2021, 1:12pm UTC](https://forum.inductiveautomation.com/t/power-table-color-grouping-based-on-1-cell-value/44037 "2021-03-03T13:12:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaushik.shah](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kaushik.shah](https://forum.inductiveautomation.com/u/kaushik.shah)\
**Post date:** [March 3, 2021, 1:12pm UTC](https://forum.inductiveautomation.com/t/power-table-color-grouping-based-on-1-cell-value/44037/1 "2021-03-03T13:12:14Z")

</div>

Hello There,  
I am trying to format background color of the power table based on the value of previous column.

Here’s an example:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/6/76d8f86c8104b595b51e83df6aef7e1f7178fc27.png)

In the picture below, there are 6 rows with same column value of batch\_number hence, I want to have those rows grouped in same color and so on.

Here’s what I got but I think I am missing something:

```auto
data = self.data		
	returnValue = {}		
	
	if rowView == 0:
		returnValue["background"] = '#DDDDDD'
		previous = '#DDDDDD'
	elif colName == 'batch_number':
		if int(value) == int(data.getValueAt(rowIndex-1, colIndex)):
			returnValue["background"] = previous
		else:
			if previous == 'white':
				returnValue["background"] = '#DDDDDD'
				previous = '#DDDDDD'
			else:
				returnValue["background"] = 'white'
				previous = 'white'
	return returnValue

```

Any leads are appreciated.

Thanks,  
Kaushik

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [March 3, 2021, 11:48pm UTC](https://forum.inductiveautomation.com/t/power-table-color-grouping-based-on-1-cell-value/44037/2 "2021-03-03T23:48:19Z")

</div>

Here’s something I haven’t tested but should help. If your batch numbers skip around quite a bit you might end up having more than 1 consecutive group with the same color. If so then this script would need to be further refined.

```auto
rowColors = ["#EEEEEE", "#AAAAAA", "#777777"]
batch_number = self.data.getValueAt(rowIndex, "batch_number ")
return {"background": rowColors[batch_number % len(rowColors)] }

```

You can put as many colors in the rowColors list and the script will choose a color based on the modulus

---

<div class="post-metadata">

**Author:** ![kaushik.shah](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kaushik.shah](https://forum.inductiveautomation.com/u/kaushik.shah)\
**Post date:** [March 4, 2021, 11:03am UTC](https://forum.inductiveautomation.com/t/power-table-color-grouping-based-on-1-cell-value/44037/3 "2021-03-04T11:03:36Z")

</div>

@JGJohnson Thanks so much. That got me going.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [April 22, 2022, 7:50pm UTC](https://forum.inductiveautomation.com/t/power-table-color-grouping-based-on-1-cell-value/44037/4 "2022-04-22T19:50:59Z")

</div>

I’m looking at doing this as well. My following script works, but it sometimes gets messed up when I select a row

```auto
	rowColors = ['#CCFFCC', 'white']
					
	if colName == 'status':
		from javax.swing import JComboBox
		elements = [self.host,'Retrived','Not Found']
		dropdown = JComboBox(elements)
		dropdown.setSelectedItem(value)
		return {'renderer':dropdown}
	
	else:
		if colName == 'serial':
			serial = self.data.getValueAt(rowIndex,'serial')
			if rowIndex:
				prevSerial = self.data.getValueAt(rowIndex-1,'serial')
			else:
				self.toggle = 0
				prevSerial = serial
			
			if serial != prevSerial:
				self.toggle = not self.toggle
		
		if selected:
			return {'background': self.selectionBackground}
		else:
			return {'background': rowColors[self.toggle]}

```

Correct Rendering:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/9/09755a805afece226435b3a5c0865b083f3015c3.png)

Incorrect after selection:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/f/efcab8fa53a306de150ee28bfa4a1530388ad4ce.png)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [April 23, 2022, 6:05pm UTC](https://forum.inductiveautomation.com/t/power-table-color-grouping-based-on-1-cell-value/44037/5 "2022-04-23T18:05:13Z")

</div>

I found a way to make it faster and therefore the row selection does not mess up any more 🙂

First, I made a custom method on the table called `mapColors()`, it creates a list of unique values, serial numbers in my case, and keeps them in order according to how they appear in the table. I multiply my color list by the length of the set in order to make sure I have enough alternations to cover every serial number. The color mapping dictionary is then put in a client property.

```auto
	from collections import OrderedDict
	
	serials = self.data.getColumnAsList(self.data.getColumnIndex('serial'))
	set = []
	for serial in serials:
		if serial not in set:
			set.append(serial)

	colors = ['CCFFCC','white'] * len(set)
	mapping = {set[i]: colors[i] for i in range(len(set))}
	#print mapping
	self.putClientProperty('colorMapping',mapping)

```

Next, I call `mapColors()` anytime the data changes

```auto
if event.propertyName == 'data':
	event.source.mapColors()

```

Finally, `configureCell` call the color mapping client property and applies the correct color. I only call the colorMapping once when I’m in the ‘serial’ column and put it in another client property so I don’t have to get the serial number for every single cell in the row.

```auto
	if colName == 'status':
		from javax.swing import JComboBox
		elements = [self.host,'Retrived','Not Found']
		dropdown = JComboBox(elements)
		dropdown.setSelectedItem(value)
		return {'renderer':dropdown}
	elif selected:
		return {'background': self.selectionBackground}
	else:
		if colName == 'serial':
			color = self.getClientProperty('colorMapping')[self.data.getValueAt(rowIndex,'serial')]
			self.putClientProperty('cellColor',color)

		return {'background': self.getClientProperty('cellColor')}

```
