Ordering table columns after loading data

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

There is much you are not telling us.

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

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:

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:

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.

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

Try using an OrderedDict().

1 Like

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 (9.3 KB)

image

Script on button:

	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
2 Likes