PowerTable dropdown dynamic

I have a power table

if colName=='DefectType':	
 query="select  Code,Name  from RubberDefect"
 DefectType=system.db.runQuery(query,'MaluProduction')
 return {'options': [(row[0], row[1]) for row in DefectType]}

In my database I have two column
image
I current can use the upside logic show the Chinese word in the dropdown list.

when I selected, the English word will exists
image

Now I want to drop down list according to another column data
image

	if  colName=='DefectType':
		root = self
		if root.data.getValueAt(row, 'Cmode')== 'R':
			#while the ColName=Cmode and value =R, use the query 
			query="select  Code,Name  from RubberDefect"
			DefectType=system.db.runQuery(query,'MaluProduction')
			return {'options':[(row[0], row[1]) for row in DefectType]}
		else:
			query2="select  Code,Name  from RubberDefectB"
			#while the ColName=Cmode and value <>R, use the query2
			DefectType=system.db.runQuery(query2,'MaluProduction')
			return {'options':[(row[0], row[1]) for row in DefectType]}

I have tried code, but not success, can you help to modify the code?

	if  colName=='DefectType':
		root = self
		from javax.swing import JComboBox, DefaultCellEditor, DefaultComboBoxModel
		class CustomCellEditor(DefaultCellEditor):
			def getTableCellEditorComponent(self, table, value, isSelected, row, column):
				comp = DefaultCellEditor.getTableCellEditorComponent(self, table, value, isSelected, row, column)
				data = []
				if root.data.getValueAt(row, 'Cmode')== 'R':
					#while the ColName=Cmode and value =R, use the query 
					query="select  Code,Name  from RubberDefect"
					DefectType=system.db.runQuery(query,'MaluProduction')
					data=[(row[0], row[1]) for row in DefectType]
				else:
					query2="select  Code,Name  from RubberDefectB"
					#while the ColName=Cmode and value <>R, use the query2
					DefectType=system.db.runQuery(query2,'MaluProduction')
					data=[(row[0], row[1]) for row in DefectType]
				comp.setModel(DefaultComboBoxModel(data))
				return comp
		return {'editor': CustomCellEditor(JComboBox())}

I have try another code ,also not work

I think you need to deliver an editor for editing and a renderer for display. The renderer can be the same component you use for editing, though.

Performance note: you are running queries inside these methods. Don’t do that. Create custom properties on the component and use some flavor of query binding to load those datasets. Reference the appropriate custom property in the extension method. That keeps the gateway round trip out of your component’s fast path.

Also consider defining your custom editor class in a project script and only instantiating it in your extension method. (You will need an __init__ method as you won’t be able to rely on closures, but the end result should be faster.)

2 Likes

Reviving this thread for as I am doing this too for the first time in a while and am not sure what I am doing wrong here. I have a table and it has a custom property of a dataset, two columns, with the choices I care to display. Here are all my extension scripts in my scripting library.

from javax.swing import JComboBox, DefaultCellEditor, DefaultComboBoxModel

EDITABLE_COLUMNS = ['LIN', 'LOX', 'PLAR','CLAR', 'weightSource']

class CustomCellEditor(DefaultCellEditor):
	def __init__(self, choices):
		DefaultCellEditor.__init__(self, JComboBox())
		self.choices = choices

	def getTableCellEditorComponent(self, table, value, isSelected, row, column):

		comp = DefaultCellEditor.getTableCellEditorComponent(self, table, value, isSelected, row, column)
		data = []
		for (idx, name) in system.dataset.toPyDataSet(self.choices):
			data.append(name)
		comp.setModel(DefaultComboBoxModel(data))
		return comp


def isCellEditable(self, rowIndex, colIndex, colName, value):
	"""
	Only the product and weight source columns are editable, any row.
	"""
	return colName in EDITABLE_COLUMNS and self.parent.editable


def onCellEdited(self, rowIndex, colIndex, colName, oldValue, newValue):
	self.data = system.dataset.setValue(self.data, rowIndex, colIndex, newValue)

def configureEditor(self, colIndex, colName):
	"""
	This is where we create our dropdown selection for weightSources.
	"""
	if colName == 'weightSource':
		return {'editor': CustomCellEditor(self.weightSources)}

However double clicking on my weight sources column just lets me edit the text instead of showing me the dropdown.

I have this working on another project though there I do it all in the extension function directly with the class / instantiation (eww I know) but now I am trying to do it this way and 1) its not working and 2) it seems like I am still instantation on each call anyways here.

What am I doing wrong? In the actual configureEditor extension function I am doing a return PlantSetup.configureEditor(self, colIndex, colName)