# Writing table data

**URL:** <https://forum.inductiveautomation.com/t/writing-table-data/85873>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [February 17, 2024, 4:29am UTC](https://forum.inductiveautomation.com/t/writing-table-data/85873 "2024-02-17T04:29:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [February 17, 2024, 4:29am UTC](https://forum.inductiveautomation.com/t/writing-table-data/85873/1 "2024-02-17T04:29:08Z")

</div>

Hi  
I'm trying to something that should be simple... but maybe it is me that is simple?  
I would like to display a text file in a table so that I can modify the style of a row based on it's content. The table will have only one column, for the text line, and one row per line in the file.  
The script that I currently have in the binding transform is

```auto
def transform(self, value, quality, timestamp):
	rows = []

	for line in value.splitlines():
		row = {
	        # I need code for correct formatting of the row data 
			# here (including style (eg background color) here
                        # in json format?

			}
		rows.append(row)
	
	return rows

```

Thanks

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [February 17, 2024, 4:53am UTC](https://forum.inductiveautomation.com/t/writing-table-data/85873/2 "2024-02-17T04:53:29Z")

</div>

This should give you a good starting point. Try working it out and then post back with what didn't work.

> [@Perspective - color table row based on rowData value](https://forum.inductiveautomation.com/t/perspective-color-table-row-based-on-rowdata-value/84314):
>
> Hello guys, I am new to Perspective. I have a table component binded to a Named Query, I configured columns and one is a view ("Linked IDCoil"). I have also a transform script on the binding to add other columns and show a checkbox. I want to change the background color of rows that containt IDCoil != self.view.params.IDCoil. I tried to add style to columns object but doesn't work or view is no more displayed This is the code: columnData = [self.view.params.IDCoil] \* value.getRowCount() valu…

NOTE: There is at least one link to a old manual page, which will not work. You'll need to find it in the new manual.

---

<div class="post-metadata">

**Author:** ![Usha\_Vilash](https://avatars.discourse-cdn.com/v4/letter/u/b5a626/32.png) [@Usha\_Vilash](https://forum.inductiveautomation.com/u/Usha_Vilash)\
**Post date:** [February 17, 2024, 5:06pm UTC](https://forum.inductiveautomation.com/t/writing-table-data/85873/3 "2024-02-17T17:06:50Z")

</div>

Hey try this.

```auto
row = {
            "text": line,
            "style": {
                #add color
            }

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 17, 2024, 5:23pm UTC](https://forum.inductiveautomation.com/t/writing-table-data/85873/4 "2024-02-17T17:23:22Z")

</div>

You can get a good flavour of how this should work by analysing the first row of the default data in the Table component. This has "Folsom" on an orange background. The JSON looks like this:

```auto
{
  "city": {
    "align": "center",
    "editable": true,
    "justify": "left",
    "style": {
      "backgroundColor": "#F7901D",
      "classes": "some-class"
    },
    "value": "Folsom"
  },
  "country": "United States",
  "population": 77271
}

```

If you want to do this well, then define a style in Project Browser | Perspective | Styles and use that in the `style.classes` property in the JSON above and remove the `backgroundColor` property.

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [February 18, 2024, 8:01am UTC](https://forum.inductiveautomation.com/t/writing-table-data/85873/5 "2024-02-18T08:01:09Z")

</div>

Thank you all for response. I still haven't got it sorted  
I trimmed out everything leaving hopefully just one column for the text data. My code is as follows:

```auto
def transform(self, value, quality, timestamp):

	rows={}
	ctr=0
	for line in value.splitlines():	
		row = {
		  "data": {
		    "value": line
		  		}
			}		
		rows[ctr] = row		
		ctr+=1
	return rows

```

The data seems to get to the table:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/2/42a689adffabab3b50226833c9f6c8d7cfb0572a.png)  
The "value" correctly displays the lines of text. The 24 error messages say "Invalid key"  
The order of the lines is odd, but thats another issue

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 18, 2024, 9:19am UTC](https://forum.inductiveautomation.com/t/writing-table-data/85873/6 "2024-02-18T09:19:03Z")

</div>

That's returning a **dictionary of dictionaries**. You need a **list** (array) **of dictionaries**. Note that the key word "data" shouldn't appear in the list. The whole list is assigned to `props.data` instead.

> **The JSON should look like this.**
>
> ```auto
> [
> {
> "city": {
> "align": "center",
> "editable": true,
> "justify": "left",
> "style": {
> "backgroundColor": "#F7901D",
> "classes": "some-class"
> },
> "value": "Folsom"
> },
> "country": "United States",
> "population": 77271
> },
> {
> "city": "Helsinki",
> "country": "Finland",
> "population": 635591
> },
> {
> "city": "Jakarta",
> "country": "Indonesia",
> "population": 10187595
> }
> ]
> 
> ```

> **The code should look something like this.**
>
> ```auto
> def transform(self, value, quality, timestamp):
> rows = [] # An empty LIST!
> for line in value.splitlines():	
> if row['col1'] > 15:
> rows.append({"col1": {
> "value": value['col1'],
> "style": {"classes": "myStyleClass"}
> }})
> else:
> rows.append({"col1": value['col1']})
> return rows
> 
> ```

I haven't tested this. I've used spaces for indentation on the forum. You might want to convert to tabs.

> The order of the lines is odd, but that's another issue.

That's because you returned a dictionary and dictionaries in Jython are unordered. Returning as a list solves this problem, maintains the order and is the format the Table component expects.

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [February 18, 2024, 9:49pm UTC](https://forum.inductiveautomation.com/t/writing-table-data/85873/7 "2024-02-18T21:49:15Z")

</div>

That's it  
Thank you for amplifying

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 18, 2024, 11:27pm UTC](https://forum.inductiveautomation.com/t/writing-table-data/85873/8 "2024-02-18T23:27:59Z")

</div>

I'm glad to help.

I forgot to repeat my suggestion that you define a style for the highlighting rather than hard code it in your script. That way it's reusable throughout your application and easy to modify, if required. Modify "myStyleClass" to suit your style name.
