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.)

1 Like