Perspective Table - Adding new rows to existing data

Hi,
I’ve been using the table in perspective such that, when a button is clicked, this will add a new, empty row to the table. What I have been doing so far is trying to get the data from the table, append a row of zeroes to the array, then make the table data this new data set as so:

    data = self.getSibling("Table").props.data
	newRow = ['empty','empty','empty','empty']
	data.append(newRow)
	self.getSibling("Table").props.data = data

This script is producing an error in the gateway, ArrayIndexOutOfBoundsException. This seems unlikely since I’m only appending the data. Does anybody know what’s going on here, or is this a bug? If so is there a workaround I can use?

The problem here is that your data variable is not a list, its a “PyArray”. Try:

    data = self.getSibling("Table").props.data.tolist()
	newRow = ['empty','empty','empty','empty']
	data.append(newRow)
	self.getSibling("Table").props.data = data

We’re working on ways to make the values supplied to scripting more intuitive, but it’s a slow process.

1 Like

Hi @cmallonee,
Thanks, I’ll give it a go.

Hi @cmallonee, just wondering how do i use a button to delete a selected row?
Thanks.

You don’t delete a selected row from the table, you use the info from the row to determine what data should be removed from the data source.

For example:
I have a database table which is represented in my Perspective Table. Let’s say the data I’ve pulled from my database five rows and four columns. When I do something like this, I also include the id column of the database table, but I don’t display that column in the Perspective Table.

Now, if you have row (single) selection enabled for the Perspective Table, you should be able to do something like execute a query when the button is clicked:

selected_row_object = self.getSibling("Table").props.selection.data[0]
id_to_delete = selected_row_object.id
query = 'delete from my table where id={0}'.format(id_to_delete)
system.db.runUpdateQuery(query, database)

Thank you @cmallonee.

I am having an error with the .tolist, it says that object has no attribute ‘tolist’

What it could be?, Thanks

I am having an error with the .tolist, it says that object has no attribute ‘tolist’

What it could be?, Thanks

This is the crux of what you're encountering; we have indeed made improvements in this area since that post was written back in July of 2020. The list-like data property can now be used as a list, and so you don't need to list-ify the value. The proposed method of appending the new row has also changed just a bit (or was wrong in that example)

	data = self.getSibling("Table").props.data
	newRow = {'city': 'Birnin Zana', 'country': 'Wakanda', 'population': 246689}
	data.append(newRow)
	self.getSibling("Table").props.data = data
1 Like

This was a guide for me for another project. Thank you!!!!

However, in another project, I am trying to do the same thing, but my table's data is bound to a query tag dataset.
I used the same script but I got this error:

AttributeError: 'com.inductiveautomation.ignition.common.JsonDatase' object has no attribute 'append'

When the table data is not bound to query tag, the code running perfectly, however the problem is with the binding. I am not sure how to fix it, I am assuming the dataset is not going to be updated with this action that is why it does not accept new values.

My code only works when the property is a list/array value. The dataset isn’t accepting new values because you’re trying to treat it like a list, but it’s a Dataset. Please review the documentation on how to properly add rows to datasets. Also, this script does nothing in regard to updating a database in any way. What you should probably be doing is writing the results you need to a table in the database, and then refreshing the binding of the Table.

1 Like

Thank you very much for your response!

1 Like