Select row in Perspective table and change background

I have went through many examples on the forum and around the web on how to format a row on a table based on a button click in Perspective. Basically, I want the operator to input a value into a text field (think scanning a barcode) and compare it to value in a cell in a specific column. If those values are equal, the row turns a different background.

Using the code example for the manual does not result in any change?!

Does anyone have a working example or knowledge of how this can be accomplished?!?! Can it even be done?

Can you add a link to the exact example you tried?

# This list will be used to create a JSON like structure that will insert rows for our styles
output_json = []
 
# Here we can define what styling on our rows will be.
style_orange = {"backgroundColor": "#F7901D"}
style_green = {"backgroundColor": "#00AA00"}
 
# You could change more than just the background color, for example:
# style_another_example {"backgroundColor": "#00AA00", "font-weight": "bold"}
 
 
for row in range(value.getRowCount()):
    row_object = {}
    row_value = {}
    row_style = {}
    for col in range(value.getColumnCount()):   
        row_value[value.getColumnName(col)] = value.getValueAt(row, col)
        row_object['value'] = row_value
         
        # Here we're checking the name of the column that we want to base our styling on.
        if value.getColumnName(col) == 'Column Name off table':
         
            # Here we're checking for individual values within the column, and applying styling
            if value.getValueAt(row, col) == 'Text Field where user inputs value to match':
                row_style = style_orange
            elif value.getValueAt(row, col) == 'no match = no change':
                row_style = style_green
                 
        row_object['style'] = row_style
    output_json.append(row_object)
return output_json

Perspective - Table - Ignition User Manual 8.1 - Ignition Documentation (inductiveautomation.com)

Example 3.

I have also tried the other 2…I apologize, but I am still very green with Ignition!

Thanks!

Here is the code that I am currently using:

    style_orange = {"backgroundColor": "#F7901D"}
	dataset = system.tag.read("[default]Heartland/DBTag").value
	componentName = self.getSibling("SetupTable").props.selection.data[0].Component
	
	for row in range(dataset.getRowCount()):
	    row_object = {}
	    row_value = {}
	    row_style = {}
	    for col in range(dataset.getColumnCount()):   
	        row_value[dataset.getColumnName(col)] = dataset.getValueAt(row, col)
	        row_object['value'] = row_value
	# Here we're checking the name of the column that we want to base our styling on.
	if dataset.getColumnName(col) == 'Component':
	      
		# Here we're checking for individual values within the column, and applying styling
		if value.getValueAt(row, col) == system.tag.readBlocking("[default]Heartland/ScannedPart"):
			row_style = style_orange

It looks like your if statements are not nested in the for loops. I would bind the dataset to the tag value, then add a transform on that binding like below

	style_orange = {"backgroundColor": "#F7901D"}
	# Bind the tag to your data prop of the table and remove the line below
	# dataset = system.tag.readBlocking("[default]Heartland/DBTag").value
	scanned_part = system.tag.readBlocking(["[default]Heartland/ScannedPart"])[0].value
	componentName = self.getSibling("SetupTable").props.selection.data[0].Component

	for row in range(value.getRowCount()):
	    row_object = {}
	    row_value = {}
	    row_style = {}
	    for col in range(value.getColumnCount()):   
	        row_value[vale.getColumnName(col)] = value.getValueAt(row, col)
	        row_object['value'] = row_value
	        
			# Here we're checking the name of the column that we want to base our styling on.
			if value.getColumnName(col) == 'Component':
			      
				# Here we're checking for individual values within the column, and applying styling
				if value.getValueAt(row, col) == scanned_part:
					row_style = style_orange
1 Like

you can also use dataset.getColumnNames()
to make it a bit more readable :slight_smile:

for col in dataset.getColumnNames():   
	        row_value[col] = value.getValueAt(row, col)
	        row_object['value'] = row_value
	        
	        # Here we're checking the name of the column that we want to base our styling on.
	        if col == 'Component':

1 Like

The formatting was a cut/paste issue with notepad it appears. Thanks for reply!

1 Like