# Changing colour of row based off value in cell in perspective table

**URL:** <https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [February 18, 2021, 7:51pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587 "2021-02-18T19:51:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanbuckley2698](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@seanbuckley2698](https://forum.inductiveautomation.com/u/seanbuckley2698)\
**Post date:** [February 18, 2021, 7:51pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/1 "2021-02-18T19:51:52Z")

</div>

Hi,

I just want to change the colour of the rows in my table based off the values on one of the cells. I am unsure on how to go about this in perspective, any help is welcome.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [February 18, 2021, 8:52pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/2 "2021-02-18T20:52:54Z")

</div>

If you want to style individual cells or even a row based off of a value within a row, then you need to provide that as part of the data. The default data in the Table has a simple application of this which you can see for the city of Folsom.

You will want to manipulate your data so that each cell contains a style object, which in turn contains a property key of `backgroundColor`, where this property has the color you want the cell to have. If you want a cell to not have any additional background, you can leave the cell as a simple value.

In this example, you can see that I’ve kept the original orange background for my `city` cell, but Ive added a green background to my `country` cell by providing the cell’s data as an object with `value` as a key (which is where the displayed value should go) and a `style` object with an internal `backgroundColor` property.

 ![Screen Shot 2021-02-18 at 12.39.08PM](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/7/27290015c22405189e41cf705b6f29afeaf0c2c5.png)  
I usually include a transform on whatever binding I’m using for my Table data:

```auto
	returned_rows = []
	for row in value:
		row_dict = {}
		row_dict['country'] = row.country
		row_dict['city'] = row.city
		population = row.population
		if int(population) < 3000000:
			population = {'value': population, 'style': {'backgroundColor': '#00FF00'}}
		row_dict['population']= population
		returned_rows.append(row_dict)
	return returned_rows

```

This allows for easy highlighting of values which match a condition:

 ![Screen Shot 2021-02-18 at 12.52.40PM](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/a/2a0ce5955721431e8028ce246bc996c1ccf59e6d.png)

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [February 18, 2021, 9:18pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/3 "2021-02-18T21:18:14Z")

</div>

Is this method only usable if the table data is given as an array of objects? If the original source is a dataset does it need to be transformed?

That’s sort of implied by your example transform, I’m just double checking.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [February 18, 2021, 11:00pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/4 "2021-02-18T23:00:54Z")

</div>

If the original data is a dataset, then yes, it will need to be transformed before reaching the code I supplied.

---

<div class="post-metadata">

**Author:** ![seanbuckley2698](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@seanbuckley2698](https://forum.inductiveautomation.com/u/seanbuckley2698)\
**Post date:** [February 19, 2021, 2:42pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/5 "2021-02-19T14:42:24Z")

</div>

> [@cmallonee](#):
>
> If you want to style individual cells or even a row based off of a value within a row, then you need to provide that as part of the data. The default data in the Table has a simple application of this which you can see for the city of Folsom.
> 
> You will want to manipulate your data so that each cell contains a style object, which in turn contains a property key of `backgroundColor` , where this property has the color you want the cell to have. If you want a cell to not have any additional background, you can leave the cell as a simple value.
> 
> In this example, you can see that I’ve kept the original orange background for my `city` cell, but Ive added a green background to my `country` cell by providing the cell’s data as an object with `value` as a key (which is where the displayed value should go) and a `style` object with an internal `backgroundColor` property.

Is there an example using a dataset because I am pulling my info in from a binding with a query and if so is it different?

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [February 19, 2021, 3:38pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/6 "2021-02-19T15:38:54Z")

</div>

If your incoming `value` was a dataset, the example above would be adjusted like this:

```auto
	pydset = system.dataset.toPyDataSet(value)
	returned_rows = []
	for row in pydset:
		row_dict = {}
		row_dict['country'] = row['country']
		row_dict['city'] = row['city']
		population = row['population']
		if int(population) < 3000000:
			population = {'value': population, 'style': {'backgroundColor': '#00FF00'}}
		row_dict['population'] = population
		returned_rows.append(row_dict)
	return returned_rows

```

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [February 19, 2021, 4:02pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/7 "2021-02-19T16:02:51Z")

</div>

And to update a bit since I failed to provide an example which would change the color of an entire _row_:

```auto
	pydset = system.dataset.toPyDataSet(value)
    backgroundColor = '#00FF00' # if condition met
	returned_rows = []
	for row in pydset:
		row_dict = {}
		if int(row['population']) < 3000000:
			row_dict['population'] = {'value': row['population'], 'style': {'backgroundColor': backgroundColor}}
		    row_dict['country'] = {'value': row['country'], 'style': {'backgroundColor': backgroundColor}}
		    row_dict['city'] = {'value': row['city'], 'style': {'backgroundColor': backgroundColor}}
        else:
            row_dict['population'] = row['population']
            row_dict['country'] = row['country']
            row_dict['city'] = row['city']
		returned_rows.append(row_dict)
	return returned_rows

```

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [February 19, 2021, 4:18pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/8 "2021-02-19T16:18:20Z")

</div>

Sorry I had tabs in my sample. You, um … missed some indent issues in your version. 🙂

---

<div class="post-metadata">

**Author:** ![seanbuckley2698](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@seanbuckley2698](https://forum.inductiveautomation.com/u/seanbuckley2698)\
**Post date:** [February 19, 2021, 5:15pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/9 "2021-02-19T17:15:17Z")

</div>

This worked but then it prevented a feature I had where I would select a row and the values in the row would appear in textboxs to be edited. Is there any way of having both?

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [February 19, 2021, 5:17pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/10 "2021-02-19T17:17:40Z")

</div>

You might need to include that each cell is editable as part of the object.

```auto
...
row_dict['population'] = {'value': row['population'], 'style': {'backgroundColor': backgroundColor}, 'editable': True}
...

```

---

<div class="post-metadata">

**Author:** ![seanbuckley2698](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@seanbuckley2698](https://forum.inductiveautomation.com/u/seanbuckley2698)\
**Post date:** [February 19, 2021, 5:31pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/11 "2021-02-19T17:31:05Z")

</div>

I tried that and it did not work, I get the values into the textbox through selectedRow so I am unsure whether it matters if the cells are editable or not as the textboxes execute an update query and change the value that way.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [February 19, 2021, 5:44pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/12 "2021-02-19T17:44:24Z")

</div>

Oh, I misunderstood what you were asking here:

> [@seanbuckley2698](#):
>
> a feature I had where I would select a row and the values in the row would appear in textboxs to be edited

How are you getting the selected row in order to feed the TextFields their data?

---

<div class="post-metadata">

**Author:** ![seanbuckley2698](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@seanbuckley2698](https://forum.inductiveautomation.com/u/seanbuckley2698)\
**Post date:** [February 19, 2021, 5:52pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/13 "2021-02-19T17:52:36Z")

</div>

```auto
try({../tblRequestView.props.data}[{../tblRequestView.props.selection.selectedRow}, "userID"], "")

```

I have this binding expression on each textbox to grab the values from the selected row.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [February 19, 2021, 6:04pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/14 "2021-02-19T18:04:02Z")

</div>

That binding expects the original structure (dataset), which we’ve altered with this script to be an array/list.  
You’ll need to update the binding references to use the new structure of the data of the table. You’ll want to update the code below and replace “city” with “userID”.

 ![Screen Shot 2021-02-19 at 10.02.31AM](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/0/40136166565c992e8de9c71918f28a7f4168a386.png)

---

<div class="post-metadata">

**Author:** ![seanbuckley2698](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@seanbuckley2698](https://forum.inductiveautomation.com/u/seanbuckley2698)\
**Post date:** [February 22, 2021, 12:39pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/15 "2021-02-22T12:39:22Z")

</div>

Hi, I tried this and I am getting 2 errors…  
#1: text; null found, string expected  
#2: Data Quality: Error\_ScriptEval(“traceback(most recent call last); file “”, line 3, in transform AttributeError: 'unicode@ object has no attribute ‘value’”)

---

<div class="post-metadata">

**Author:** ![seanbuckley2698](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@seanbuckley2698](https://forum.inductiveautomation.com/u/seanbuckley2698)\
**Post date:** [February 22, 2021, 1:32pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/16 "2021-02-22T13:32:33Z")

</div>

It is now working but it will only pull in the values of rows that have the “In progress” value which is what dictates whether the row is green or not. So only the green rows are returning values the rest return null.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [February 22, 2021, 4:06pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/17 "2021-02-22T16:06:10Z")

</div>

It sounds like you’re not making the row data homogeneous.

Early examples in this thread suggested you only need modify the piece of data you wanted to color. Later examples moved toward setting the background color of the row. based on some condition.

To use the binding/transform combo I supplied, each row will need to have the same structure whether it meets the condition or not. so you’ll want to adjust the logic so that each row has the background color set, but the color is different based on your condition:

```auto
	pydset = system.dataset.toPyDataSet(value)
    backgroundColor = '#00FF00' # if condition met
	returned_rows = []
    default_background_color = ''
	for row in pydset:
		row_dict = {}
		if int(row['population']) < 3000000:
			row_dict['population'] = {'value': row['population'], 'style': {'backgroundColor': backgroundColor}}
		    row_dict['country'] = {'value': row['country'], 'style': {'backgroundColor': backgroundColor}}
		    row_dict['city'] = {'value': row['city'], 'style': {'backgroundColor': backgroundColor}}
        else:
            row_dict['population'] = {'value': row['population'], 'style': {'backgroundColor': default_background_color}}
            row_dict['country'] = {'value': row['country'], 'style': {'backgroundColor': default_background_color}}
            row_dict['city'] = {'value': row['city'], 'style': {'backgroundColor': default_background_color}}
		returned_rows.append(row_dict)
	return returned_rows

```

---

<div class="post-metadata">

**Author:** ![seanbuckley2698](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@seanbuckley2698](https://forum.inductiveautomation.com/u/seanbuckley2698)\
**Post date:** [February 22, 2021, 4:18pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/18 "2021-02-22T16:18:33Z")

</div>

I have it working now, thanks for your help with everything, it gave me a much better understanding.

---

<div class="post-metadata">

**Author:** ![Applieddesignandcont](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Applieddesignandcont](https://forum.inductiveautomation.com/u/Applieddesignandcont)\
**Post date:** [March 9, 2021, 5:35pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/19 "2021-03-09T17:35:10Z")

</div>

Hi,

I am trying to change the colors based off one cells value also. In trying to follow the examples shown I see that the data provided is the default data. I then want to add the transform to the sample data. Where exactly do I put that transform? If I bind it to the data property I then need to provide it data from a query or something. I made a small table in Sql to mimic the example provided and added a query as well as a transform with the code provided. The transform is not executing in my code. I have it enabled and a poll rate of 2 Seconds. I know the code in the transform is not executing because I added a label and referenced the label in my transform to give the label a value. The label does not get that value.

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![damean.thomas](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/damean.thomas/32/11367_2.png) [@damean.thomas](https://forum.inductiveautomation.com/u/damean.thomas)\
**Post date:** [March 29, 2021, 7:40pm UTC](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587/20 "2021-03-29T19:40:51Z")

</div>

Is this the only way to change the row color for large tables or is there a simpler way? Don’t mind but want to check before i begin typing.

[Next page](https://forum.inductiveautomation.com/t/changing-colour-of-row-based-off-value-in-cell-in-perspective-table/43587.md?page=2)
