Deleting row/s from table

Newbie question, please bear with me. I have a table with data in it, the last column has delete and edit buttons against each row of table, which are embedded from another view.
What is the most simple/easiest way to delete a row (obviously after pressing the delete button) here and in the DB. The ID is the key value and I have assigned them randomly. Thank You

You can bind the viewParams to this.props.selection. when you click the button, you select the cell, and the embedded view get the params. After that, you get a target to operate on.

  • By default, the embedded view will receive a parameter rowData which contains all the data in the row. In your case you are only interested in the ÌD value.
  • On the embedded view you need to manually add params.custom.rowData.
  • Right-click on the delete button, select Configure Events, *onActionPerformed`, add a script and add the code below (indenting one tab).
def runAction(self, event):
    # Delete the record.
    system.db.runPrepUpdate('DELETE FROM config WHERE id = ?', [self.view.params.rowData['ID']])
    # Send a message to anything that's listening.
    system.perspective.sendMessage("dataTableChanged", payload = {}, scope = "session")
  • Right-click on the table to be updated, select Configure Scripts, add a message handler, call it `dataTableChanged" (exactly the same as in the sendMessage routine), set the listen scopes to "Session" and enter the following code.
def onMessageReceived(self, payload):
    self.refreshBinding("props.data")
3 Likes

Thank You, I made that work after couple of tries.

Just for making things easier for yourself in the future.

I would not let the delete be handled on the button action.
instead also send a message (similar to "dataTableChanged" )
"deleteRow" or something with the id as payload

And handle the delete on the message handler

This way you can use the same delete/edit button subview for other tables. you'll just have to put another message handler on it

2 Likes

I'll suggest another way of doing this:
Don't put a delete button on each row, instead add a remove selected row outside of the table.
Then use the table's props.selection.selectedRow to delete that row when the button is clicked.
You can use the selection.data property to update the database at the same time.
Or add another update database button to do batch updates.

This removes the need for subviews, message handlers, etc.
You can even allow multiple selection to remove multiple rows at once.
And you can reuse the mechanism for any other table, all you have to do is copy/paste the button and update the path to the table component.

The button script is as simple as this:

row_id = self.getSibling("Table").props.selection.selectedRow
row = self.getSibling("Table").props.data.pop(row_id)
system.db.runNamedQuery("remove_row", parameters=row)
4 Likes

In the "row = self.getSibling("Table").props.data.pop(row_id)" what is the pop representing?

pop is a function which removes the row from the table.props.data with row_id and returns it.

1 Like

pop() with a "Try it yourself".