# Ordering table columns after loading data

**URL:** <https://forum.inductiveautomation.com/t/ordering-table-columns-after-loading-data/91692>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [June 11, 2024, 2:26pm UTC](https://forum.inductiveautomation.com/t/ordering-table-columns-after-loading-data/91692 "2024-06-11T14:26:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [June 11, 2024, 2:26pm UTC](https://forum.inductiveautomation.com/t/ordering-table-columns-after-loading-data/91692/1 "2024-06-11T14:26:10Z")

</div>

I have a table column with data from a named queries, after a series of transform script to map and style different aspects of the table I got the columns in a peculiar order.  
I would like to order them chronologically after all the necesary transform scripts, if I try this before doesn´t work.

If anibody has an idea of how to do it let me know.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/8/48aa322be2f84cb195eeb45b0c4c8d434b3c69b7.png)

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

---

<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:** [June 11, 2024, 2:29pm UTC](https://forum.inductiveautomation.com/t/ordering-table-columns-after-loading-data/91692/2 "2024-06-11T14:29:56Z")

</div>

There is much you are not telling us.

1. Your datasets have "5 cols". Your table has more than 8.
2. Show your code.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Alaco](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/alejandro_alaco/32/59849_2.png) [@Alejandro\_Alaco](https://forum.inductiveautomation.com/u/Alejandro_Alaco)\
**Post date:** [June 11, 2024, 2:39pm UTC](https://forum.inductiveautomation.com/t/ordering-table-columns-after-loading-data/91692/3 "2024-06-11T14:39:57Z")

</div>

I had to create the columns with script transfrom because the data from the query comes in just 5 columns (I don´t have access to this query) so I aplied this code:

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

        
    if quality.isGood() and value is not None:
  
        py_data = system.dataset.toPyDataSet(value)
        
     
        month_year_set = set(row['LOC'] for row in py_data)
        
    
        def parse_month_year(month_year):
            month, year = month_year.split('^')
            return int(year), int(month)
        
        month_year_list = sorted(month_year_set, key=parse_month_year)

     
        columns = ['Sede', 'WORKORDERNAME',] + month_year_list
        data = []

       WORKORDERNAME pair
        data_dict = {}
        for row in py_data:
            sede_workorder = (row['BU'], row['WORKORDERNAME'])
            if sede_workorder not in data_dict:
                data_dict[sede_workorder] = {month: None for month in month_year_list}
            data_dict[sede_workorder][row['LOC']] = row['STATES']
        
 
        for (sede, workorder), month_data in data_dict.items():
            new_row = [sede, workorder] + [month_data.get(month) for month in month_year_list]
            data.append(new_row)
        
     
        new_dataset = system.dataset.toDataSet(columns, data)
        return new_dataset
    else:
    
        return value

```

but after that I aplied the styling code:

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

    if quality.isGood() and value is not None:
        output = []

    
        py_data = system.dataset.toPyDataSet(value)

   
        for row in py_data:
            newRow = {}
       
            for col in py_data.getColumnNames():
                cell_value = row[col]

             
                if cell_value == 1:
                    bgColor = "#FF555588"
                    fontColor = "#FFFFFF" 
                    display_value = cell_value 
                elif cell_value == 0:
                    bgColor = "#50BE8788"
                    fontColor = "#FFFFFF"
                    display_value = cell_value 
                else:
                    bgColor = ""
                    fontColor = ""
                    display_value = cell_value 

               
                newRow[col] = {
                    "value": display_value,
                    "style": {
                        "backgroundColor": bgColor if bgColor else "transparent",
                        "color": fontColor,
                        "fontWeight": "bold" if bgColor else "normal",
                        "textAlign": "center",
                        "borderLeft": "1px solid #FFFFFF" if bgColor else "none",
                        "borderRight": "1px solid #FFFFFF" if bgColor else "none"
                    }
                }

       
            output.append(newRow)

        return output
    else:
        return value

```

and this is the one disorder everithing, but as you told me last time  
_"Ignition uses Jython which is based on Python 2.7. Dictionaries are unordered"_

But I don´t know wich other way I could aply the styling change script without disordering the columns.

I also use fixed props.column.field names as the columns change each time I aply a dateInput.

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

Maybe there is no solution for this issue as is not very common, maybe I should contact the database guys to change the query.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [June 11, 2024, 2:48pm UTC](https://forum.inductiveautomation.com/t/ordering-table-columns-after-loading-data/91692/4 "2024-06-11T14:48:11Z")

</div>

Try using an [OrderedDict()](https://www.geeksforgeeks.org/ordereddict-in-python/).

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [June 11, 2024, 7:46pm UTC](https://forum.inductiveautomation.com/t/ordering-table-columns-after-loading-data/91692/5 "2024-06-11T19:46:08Z")

</div>

I got a chance to play with this a bit. OrderedDict gets reverted to an unordered object when you write it to the data property (even though I was sure it worked before-- ah well), so no joy there.

But, as you should be modifying the props.columns property anyway (as I believe has also been mentioned before), this is a short example of what works.

[testTable\_2024-06-11\_1534.zip](https://forum.inductiveautomation.com/uploads/short-url/tqSo9HlEx2JvKD0eX8jIAcua95Y.zip) (9.3 KB)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/c/7c4c486d2f14c115f7f60addabd0cf17ef26981d.png)

Script on button:

```python
	sampleHeaders = ['col1', 'col2', 'status']
	sampleData = [['a', 'b', 0],
	              ['c', 'd', 1],
	              ['e', 'f', 2]
	             ]
	sampleDataset = system.dataset.toDataSet(sampleHeaders, sampleData)
	
	#------------------------------------------------------------------
	
	from copy import deepcopy
	from collections import OrderedDict
	
	bgColors = {0 : '#00FF00', # Green
	            1 : '#FFFF00', # Yellow
	            2 : '#FF0000', # Red
	           }
	           
	blankColSetting = {
	  "field": "",
	  "visible": True,
	  "editable": False,
	  "render": "auto",
	  "justify": "auto",
	  "align": "center",
	  "resizable": True,
	  "sortable": True,
	  "sort": "none",
	  "filter": {
	    "enabled": False,
	    "visible": "on-hover",
	    "string": {
	      "condition": "",
	      "value": ""
	    },
	    "number": {
	      "condition": "",
	      "value": ""
	    },
	    "boolean": {
	      "condition": ""
	    },
	    "date": {
	      "condition": "",
	      "value": ""
	    }
	  },
	  "viewPath": "",
	  "viewParams": {},
	  "boolean": "checkbox",
	  "number": "value",
	  "progressBar": {
	    "max": 100,
	    "min": 0,
	    "bar": {
	      "color": "",
	      "style": {
	        "classes": ""
	      }
	    },
	    "track": {
	      "color": "",
	      "style": {
	        "classes": ""
	      }
	    },
	    "value": {
	      "enabled": True,
	      "format": "0,0.##",
	      "justify": "center",
	      "style": {
	        "classes": ""
	      }
	    }
	  },
	  "toggleSwitch": {
	    "color": {
	      "selected": "",
	      "unselected": ""
	    }
	  },
	  "nullFormat": {
	    "includeNullStrings": False,
	    "strict": False,
	    "nullFormatValue": ""
	  },
	  "numberFormat": "0,0.##",
	  "dateFormat": "MM/DD/YYYY",
	  "width": "",
	  "strictWidth": False,
	  "style": {
	    "classes": ""
	  },
	  "header": {
	    "title": "",
	    "justify": "left",
	    "align": "center",
	    "style": {
	      "classes": ""
	    }
	  },
	  "footer": {
	    "title": "",
	    "justify": "left",
	    "align": "center",
	    "style": {
	      "classes": ""
	    }
	  }
	}
	
	dataIn = system.dataset.toPyDataSet(sampleDataset)
	
	colNames = dataIn.columnNames
	blankRow = {col : {'value': None, 'style' : {}} for col in colNames}
	
	dataOut = []
	colSettings = []
	
	for i, row in enumerate(dataIn):
		newRow = deepcopy(blankRow)
		for col in colNames:
			newRow[col]['value'] = row[col]
			newRow[col]['style'] = {'backgroundColor' : bgColors[row['status']]}
		dataOut.append(newRow)
		
	self.getSibling("Table").props.data = dataOut
	
	for col in colNames:
		colSetting = deepcopy(blankColSetting)
		colSetting['field'] = col
		colSettings.append(colSetting)
	self.getSibling("Table").props.columns = colSettings

```
