Power Table row select Color then change row color?

In Power Table ,I want to double click row , then show chooseColor, users can choose one color ,Finally the table row can change color.

def onDoubleClick(self, rowIndex, colIndex, colName, value, event):
 
	# 获取双击行的索引
	row_index = rowIndex
	col_index = colIndex
	
	# 可以根据需要获取当前行的背景颜色作为初始颜色
	# 这里简单假设当前行背景颜色属性可直接获取
	color_background=self.background

#    current_row_color = self.rowIndex.getProperty(color_background)
	print(color_background)

	# 弹出颜色选择对话框,使用当前行背景颜色作为初始颜色,并设置自定义标题
	selected_color = system.gui.chooseColor(color_background, u"请选择行背景颜色")
	
	if selected_color is not None:
		# 设置指定行的背景颜色为用户选择的颜色
#		self.selectionBackground=selected_color
		self.setProperty('rows[' + str(row_index) + '].background', selected_color)


But self.setProperty('rows[' + str(row_index) + '].background', selected_color), ERROR

 Vision.Components.AdvancedTable - Error invoking extension method.
org.python.core.PyException: TypeError: 'java.awt.Color' object is not callable

How do I correct it?

There is no such method.

You cannot trust large language models/AI with Ignition scripting, because the domain is too small for them to have useful context. They will readily hallucinate functions and attributes that simply don't exist in Ignition.

I would recommend adding a dataset type custom property to the table. Put the row index as the first column, and a color value as the second column.

Then in configureCell, you will need to look up the current row index in that dataset and retrieve the value from it to use as a row background (if present).

How is the table being populated? If possible, simply add a hidden string column to store the color information.

Example:
Add a column to the table called 'rowColors' and use the table customizer to hide the column:
Then, program the onDoubleClick extension funcion in this way:

#def onDoubleClick(self, [...], event):
	# 弹出颜色选择对话框,使用当前行背景颜色作为初始颜色,并设置自定义标题
	selected_color = system.gui.chooseColor(self.background, u"请选择行背景颜色")

	if selected_color is not None:
	
		# Convert the integer parameters of the selected color to a comma delimited string
		rgbaString = '{},{},{},{}'.format(selected_color.red, selected_color.green, selected_color.blue, selected_color.alpha)
		
		# Store the string value in the hidden 'colors' column
		self.data = system.dataset.setValue(self.data, rowIndex, 'rowColors', rgbaString)

Finally, program the configureCell extension function to retrieve and utilize the colors:

def configureCell(self, value, textValue, selected, rowIndex, colIndex, colName, rowView, colView):

	# Get the double click use selected color for the row from the hidden column
	storedColor = self.data.getValueAt(rowIndex, 'rowColors')
	
	# Optional:
	# Ignore the stored color parameter if it's the selected row
	# ...and simply apply the selected row color
#	if selected:
#		return {'background': self.selectionBackground}
	
	# If a user selected color has been stored in the table
	if storedColor:
		# unpack the red, green, blue, alpha integer values from the stored string
		r, g, b, a = [int(value) for value in storedColor.split(',')]
		
		# Paint the cell with the user selected color
		return {'background': system.gui.color(r, g, b, a)}
	
	# Otherwise return the default value for the table
	else:
		return {'background': self.background}

Result:
Demo

1 Like

If you're going this route, just store a java.awt.Color directly - they're safe to serialize in datasets and you won't pay the parsing penalty every time you interact with the column.

3 Likes