Insert and update row using button click in power table

Hi Guys,

I have tried to update and insert the row using a button click. When I write any value in the cell and go to the next cell or button click then automatically deleted newValue. and insert new row Null and does update newValue.

Any idea for how to set a new value and insert, and update a row using a button click?

  1. Below script in button action performed for insert row:
table = event.source.parent.getComponent('tbl')
> row = table.selectedRow   
> if system.gui.confirm("insert?"): 
> 	Nummer = event.source.parent.parent.Nummer
> 	col1 = table.data.getValueAt(row, "col1")
> 	col2 = table.data.getValueAt(row, "col2")
> 	col3 = table.data.getValueAt(row, "col3")
> 	
> 	args = [Nummer, col1,col2,col3]
> 	
> 	insertRecord = system.db.runPrepUpdate("insert into Test (Nummer, col1,col2,col3) values (?,?,?,?)", args) 
> 	if insertRecord > 0:
> 		system.db.refresh(table, "data")
  1. Below script to update a row in the button action performed:
table = event.source.parent.getComponent('tbl')
data = event.source.parent.getComponent('tbl').data   
row = event.source.parent.getComponent('tbl').selectedRow  

ValueOne = data.getValueAt(row, "col1")
ValueTwo = data.getValueAt(row, "col2")
ValueThree = data.getValueAt(row, "col3")
ValueFour = data.getValueAt(row, "ID") 
args = [ValueOne,ValueTwo,ValueThree,ValueFour]

if system.gui.confirm("are you sure?"): 
	UpdateRecord = system.db.runPrepUpdate("UPDATE Test SET col1 = ?, col2=?,col3=? where ID = ? ", args) 
	system.db.refresh(table, "data")
	if UpdateRecord > 0:
		system.gui.messageBox("updated.")

Thanks in advance
Priyanka

It sounds like you are not refreshing the power table binding after updating/inserting into the database. The code you are looking for is system.db.refresh(powerTable, 'data')
https://docs.inductiveautomation.com/display/DOC81/system.db.refresh

Pssst!

system.db.refresh(powerTable, 'data')

2 Likes

@justinedwards.jle @pturmel Thanks for the quick response.

But I have already added a refresh function after the runPrepUpdate function.

Please edit your original post to properly mark your scripts as "preformatted text" so they are readable. Please always do this for posted scripts and error reports.

The issue I am facing right now is whenever I enter a new value into a particular cell the cell is not getting reflected with that value, it shows a blank cell.

Can you please help with this?

Use this in your onCellEdited script to change your table's dataset:
https://docs.inductiveautomation.com/display/DOC81/system.dataset.setValue

It is possible that you will have to remove your binding, and script the dataset population to do what you are doing.