Color row table

Hello I'am using results = system.alarm.queryStatus(source=["*"]) to get status alarms.
I put all data on a power table.

image

I would like color row red if State = 1 and Blue if State = 0

Coloring a row in a table has been discussed many times around here.
In fact, I needed to do it yesterday and couldn't remember the exact format expected by the table, and I found the answer here in 3 minutes...
Also, you're posting a screenshot of a binding that calls your own custom functions, without showing those functions. It tells us nothing.

Short story short: You'll need to use the list of dicts format instead of a dataset, and it should look something like this:

[
	{
		'value': {
			'column1': value,
			'column2': value,
			'column3': value,
			...
		},
		'style': {
			'backgroundColor': 'red' if state == 1 else 'blue' if state == 0 else None
		}
	},
	...
]

where each dict is a row.

That said, any reason you're not using the built-in alarm status table ?

salut @pascal.fragnoud merci.

voici ce que j'ai fait:

	""
	
	pydset = system.dataset.toPyDataSet(value.getDataset())
	
	returned_rows = []
	for row in pydset:
		row_dict = {}
		if int(row['State']) ==2:
			row_dict['State'] = {'value': row['State'], 'style': {'backgroundColor': "blue"}}
			row_dict['EventTime'] = {'value': row['EventTime'], 'style': {'backgroundColor': "blue"}}
			returned_rows.append(row_dict)
		elif int(row['State']) ==0:
			row_dict['State'] = {'value': row['State'], 'style': {'backgroundColor': "green"}}
			row_dict['EventTime'] = {'value': row['EventTime'], 'style': {'backgroundColor': "green"}}
			returned_rows.append(row_dict)
			
		else:
			row_dict['State'] = row['State']
			row_dict['EventTime'] = row['EventTime']

I found some lines of code on forum.

#recuperer les alarmes en temps réels
def getAlarmesStatus():
	
	results = system.alarm.queryStatus(source=["*"])
	return results

Here's a prettified version:

returned_rows = []
for row in pydset:
	row_dict = {
		'value': {
			'State': row['State'],
			'EventTime': row['EventTime']
		}
	}
	if row['State'] == 2:
		row_dict['style'] = {'backgroundColor': "blue"}
	elif row['State'] == 0:
		row_dict['style'] = {'backgroundColor': "green"}
	returned_rows.append(row_dict)

Though I'd probably try to skip all the conversions to dataset then to py dataset.

1 Like