Extracting whole row of data from selected row in table

Is there an easy way to get the entire row of data from a table into a custom property? I feel like I’m missing something!

First, I tried binding a dataset type property as:
{Root Container.Power Table.data}[{Root Container.Power Table.selectedRow}]

Secondly:
I tried making a script to do this for me but I get the error:
TypeError: toPyDataSet(): 1st arg can’t be coerced to com.inductiveautomation.ignition.common.Dataset

data = project.getData.getData()

headers = data.getColumnNames()
a = []

row = 30

for col in range(len(headers)):
colName = data.getColumnName(col)
a.append([data.getValueAt(row, colName)])

#ds = system.dataset.toDataSet(headers, a)

ds = system.dataset.toPyDataSet(a)

Ugg, I really am missing something. Just want to be able to pass selected data from a table without creating a new SQL table…

I don’t really like the idea of get every value and the create a new dataset. I’d prefer to eliminate every other rows so I can obtain a new dataset with only the selected row :wink:
Here it is the script:

table = event.source.parent.getComponent('Power Table')
selRow = table.selectedRow
PyTable = system.dataset.toPyDataSet(table.data)
IndicesToDelete = []

for x in range(0,len(PyTable)):
	if x != selRow:
		IndicesToDelete.append(x)

oneRow = system.dataset.deleteRows(PyTable,IndicesToDelete)

In oneRow you’ll have your new Dataset with only the selected row :v:

PS: The next time, put your code in “Preformatted Text (Ctrl + Shift + C )” so it can be more readable like the one I posted :wink:

1 Like

Both methods can be made a lot cleaner too (at least more pythonic) with list comprehensions and other transforms:

A way to calculate the indices

IndicesToDelete = [i for i in range(len(PyTable)) if x != selRow]

And a way to create the new array, by transforming it to a pyDataSet first:

pyDs = system.dataset.toPyDataSet(ds)
headers = system.dataset.getColumnHeaders(ds)
row = 30

newDs = system.dataset.toDataSet(headers, [pyDs[row]])

Although I’m not a big fan of these dataset methods. They lack the mutability and OO methods you expect in Python, which makes all Python code for it more awkward.

1 Like

Yeah I know, but since the code he posted was “basic” I followed his wave for a more “readable” code :sweat_smile:

1 Like

Try the view() expression function from the free Simulation Aids Module. The expression would look something like this:

view("Select * Where _r == args[0] Limit 1",
  {Root Container.Power Table.data},
  {Root Container.Power Table.selectedRow})
1 Like

Great, thanks for the tips. System.dataset.deleteRows worked like a champ, I guess I won’t try to figure out why the list->dataset conversion wasn’t working before.

def getRowData(data, selRow):
    IndicesToDelete = [i for i in range(len(data)) if i != selRow]
    data = system.dataset.deleteRows(data, IndicesToDelete)
    data = system.dataset.toPyDataSet(data)
    return data

I bound the new dataset to a custom property via the onMousePress event handler because I couldn’t figure out how to pass dataset into a runScript command and bind it using the expression language:

data = self.data
self.rowData = project.getData.getRowData(data,rowIndex)
if event.propertyName == "selectedRow":
	selectedRow = event.newValue
	data = event.source.data
	columns = system.dataset.getColumnHeaders(data)
	if selectedRow != -1:
		selRow = [list(system.dataset.toPyDataSet(data)[selectedRow])]
	else:
		selRow = []
	event.source.parent.getComponent('Power Table 2').data = system.dataset.toDataSet(columns, selRow)