I am sure that there have been other posts about this, but I just can’t seem to find anything that has worked. I have a table which displays the data from a database. I want to be able to edit cells and then save the changes to the database.
I have the following code:
row = event.row
col = event.column
id = self.data.getValueAt(row, col)
query = "UPDATE ReportTracker SET %s = ? WHERE id = ?" %(col)
system.db.runPrepUpdate(query,id)
system.db.refresh(self,"data")
But I am getting an error saying:
File “function:runAction”, line 5, in runAction AttributeError: ‘com.inductiveautomation.perspective.gateway.script’ object has no attribute ‘data’
You’re missing the reference to props. Also, to refresh a binding in perspective you call the refreshBinding() function on the component.
row = event.row
col = event.column
#assuming that self.props.data is a dataset and not an array of objects.
id = self.props.data.getValueAt(row,col)
query = "UPDATE ReportTracker SET %s = ? WHERE id = ?" % (col)
system.db.runPrepUpdate(query, [id])
self.refreshBinding('props.data')
I updated my code and now am getting the following error:
File “function:runAction”, line 6, in runAction AttributeError: ‘com.inductiveautomation.perspective.gateway.script’ object has no attribute ‘getValueAt’
row = event.row
col = event.column
#assuming that self.props.data is a dataset and not an array of objects.
id = event.value
query = "UPDATE ReportTracker SET %s = ? WHERE id = ?" % (col)
system.db.runPrepUpdate(query, [id])
self.refreshBinding('props.data')
You need two parameters. Something like this, although it didn’t seem to work earlier.
row = event.row
col = event.column
#assuming that self.props.data is a dataset and not an array of objects.
ds = self.props.data
id = ds.getValueAt(row,'id')
query = "UPDATE ReportTracker SET %s = ? WHERE id = ?" % (col)
system.db.runPrepUpdate(query, [event.value,id])
self.refreshBinding('props.data')
Your parameter list must have a value for each ‘?’ in the query.
For my understanding, the first parameter is the new value entered in the cell and the second parameter is the selected cell in the table being edited?
I made an assumption that there is a column in the table (hidden or otherwise) which is named 'id' where the id is stored. You’ll need to get that value from somewhere.
If you were to count the '?''s in the query string starting from 0, then the index in the parameter list would coincide with the '?' that is being replaced. So the first parameter is the value that you are setting, and the second parameter is the id that you are filtering by.
Right, that’s what I thought. I got it figured out, turns out you can’t have a space in the column header or else it throws everything off. Thank you for your help
Is it possible to do this if my table is an array of objects and not a dataset? I added a transform to prop.data to do some conditional cell background colors based on the cells values. In order to do so, I have to return the data as json and not a dataset. In doing so, I am now not able to edit cells.