Delete data from dataset vision

hello, I need delete my data from table.
Want I want is, when I select a row, and I click into DELETE button, row into table must be deleted

code of delete button:

tableData = event.source.parent.getComponent('Table').data
row = event.source.parent.row
if row != -1: 
   system.dataset.deleteRow(tableData, row)
else:
	system.gui.messageBox("select row!")

Event into table:

event.source.parent.row = event.source.selectedRow

But when I click on the DELETE nothing is happening. Am I forgetting something about the syntax to delete row from table?

The system function you are calling creates a new dataset without the deleted row. The new dataset has to be assigned back to the table's data property.

newData = system.dataset.deleteRow(tableData, row)
event.source.parent.getComponent('Table').data = newData
3 Likes

There are a few errors here:

  1. row = event.source.parent.row
    the event is the action performed, its source is the button, and the button's parent is NOT the table.
    I'm not sure how you got that path... try row = event.source.parent.getComponent('Table').selectedRow
  2. datasets manipulation functions return NEW datasets. So you're not really deleting a row from your dataset, only creating a new one with that row removed, and then doing nothing with it.
    try event.source.parent.getComponent('Table').data = system.dataset.deleteRow(data, row)
  3. What's that cellEdited event supposed to do ? You don't need that.
4 Likes

Along, with what @justinedwards.jle and @pascal.fragnoud have said, it should be noted that if the source of the data in your table is coming from a binding, then you need to also remove the row from source data, otherwise the binding will just overwrite any change that you have made.

In that instance I would say that you should actually remove the row from the source and then refresh the binding.

4 Likes

an event to tell me that I click on a row in the table.

Afterwards I don't really know if it's the right event that I chose

I would use a property change script that looks at any change of the Selected Row property.

if event.propertyName == 'selectedRow':

cellEdited is for when you want to allow the user to edit the data on a cell, it would fire when the user finishes editing.

3 Likes

Yes, normally the information on the table will come from an SQL query, but for now, I just want to retrieve the value of the row ID I clicked.

I did it:

if event.propertyName == 'selectedRow':
	event.source.parent.row = event.source.selectedRow + 1

it work, I can get value of ID column but it is incorrect in case of image below:
image

The selected row will be zero indexed just like the table. I don't normally see developers adding a 1 to it. For deleting rows, this step isn't needed, but I'm imagining that you are doing other things with this property.

For deleting selected rows from a button, the whole script should look like this:

# Get the table, its data property, and the selected row
table = event.source.parent.getComponent('Table')
tableData = table.data
row = table.selectedRow

# if a row is selected, create a dataset without the row
# ...and assign it back to the table's data property.
if row != -1:
	table.data = system.dataset.deleteRow(tableData, row)

I've excluded the message box else clause from your original post. Personally, I don't like them because they are gui locking. I would go with a label or some other indicator for that purpose.

3 Likes

yes,I can delete data using this script into delete button. but I also read ID value of column ID when I clique in row

This is all you have to add:

rowID = tableData.getValueAt(row, 'ID')
2 Likes

I dit it:

if event.propertyName == 'selectedRow':
	#event.source.parent.row = event.source.selectedRow + 1
	table = event.source.parent.getComponent('Table')
	tableData = table.data
	row = table.selectedRow
	event.source.parent.row = tableData.getValueAt(row, 'ID')

Andd I get it :smiley:
image

it is Okay now

1 Like

Excellent. One other thing: on your table's propertyChange event handler, this line:

table = event.source.parent.getComponent('Table')

could be simplified to this:

table = event.source
1 Like

Or remove the script entirely and put that binding on the label:

try(
	{Root Container.Table.data}[{Root Container.Table.selectedRow}, 'ID'],
	-1
)
3 Likes

ID space

I see a space after 4 ('4 ') in the ID column. It suggests that your ID is a string where maybe the column should be an Int to prevent a problem that may bite you in the future.

I think he just clicked on that cell, which makes it look like this.
The right alignment suggests it is indeed an int.

image

image

image

3 Likes