Is there a way to select cells or rows in a Vision Power Table with scripting

The tl;dr is the title itself and I'm sure I'm about to embarrass myself because this has to be easy.

I'm using v8.1.24

I just need to select rows or cells in a Power Table from a script on a button. I need a "setSelectedRows()" method, but there does not appear to be one. I actually tried setSelectedRows() with a couple of different syntaxes and of course nothing worked.

The back story is:

I have a Power Table in which a user selects some rows. I loop through those rows, may or may not adjust the value of a cell in those rows, and would then like to proceed with the rest of the task.

I start off the script with:

pt = event.source.parent.getComponent('ptLengths')
dt = pt.data
selrw = pt.getSelectedRows()

and do all of my work on dt. When I'm done making the changes, I do:

pt.data = dt

At this point (I think) in the script, the table loses its selected rows. I still have the list of the originally selected rows, I just need a way to re-select them on the Power Table. Feel free to embarrass me with a link to the manual because I have not been able to find how to do this in there.

There is no supported way to do this.

If you use reflection on a Power Table component, you will find a table private field. That appears to be a JideSoft SortableTable.

Use reflection to force access to that private field, then do reflection on that to figure what is possible.

Hint: setRowSelectionInterval() and addRowSelectionInterval()

On a power table this can be done directly without reflection if row selection is allowed and selection mode is multiple interval:
Example:
image

# Get the Vision advanced table from the power table
table = event.source.parent.getComponent('Power Table').table

# Set the row selection
table.setRowSelectionInterval(2, 4)

Result:
image

additionally you can remove individual rows from the selection or add rows to the selection with removeSelectionInterval and addSelectionInerval:

table.removeRowSelectionInterval(3,3)
table.addRowSelectionInterval(6,8)

Result:
image

1 Like

I stand corrected. I didn't notice the getter for table.

There is also table.changeSelection(). This may not be the preferred way, though.
Programmatically Set Selected Cell in Power Table - Ignition - Inductive Automation Forum

1 Like

Thanks all, for your replies and help. Right after I posted this I was removed from work for a few days, so I apologize for the delay in thanking you all. I'll give these ideas a try.

So far I have not been able to get @justinedwards.jle suggestion to work and I'm trying to avoid the reflection that @pturmel mentions because I know nothing about reflection and I would like to avoid that advanced method if possible.

I have the mentioned settings on the table set correctly.

image

but I should mention that column selection is allowed I have tried both column selection allowed and disallowed and that some of the columns are editable.

I tacked this code onto the end of what I had for troubleshooting purposes - vrws is the list of originally selected valid rows.

print "vrws: {}".format(vrws)
table = event.source.parent.getComponent('ptLengths').table
print "Selecting row: {}".format(vrws[0])
table.setRowSelectionInterval(vrws[0], vrws[0])
for i in range(1,len(vrws)):
	print "Selecting row: {}".format(vrws[i])
	table.addRowSelectionInterval(vrws[i],vrws[i])
nwrws = pt.getSelectedRows()
print "nwrws: {}".format(nwrws)

The code acts like it works, but in the end nothing in the table is selected and nwrws is empty. This is the output to the console:

vrws: [1, 2, 3, 4, 5, 6, 7]
Selecting row: 1
Selecting row: 2
Selecting row: 3
Selecting row: 4
Selecting row: 5
Selecting row: 6
Selecting row: 7
nwrws: array('i')

I also looked at the post Programmatically Set Selected Cell in Power Table that @dkhayes117 mentions, and there may be some hope there with table.changeSelection() , but at the moment I'm trying to figure out how to make that work with a non-contiguous selection.

In the end, I had to use table.changeSelection(), and to make it work I had to use the toggle and extend flags differently than what seemed obvious to me. My confusion came from the fact that the extend flag changes what it does based on the toggle flag. When both flags are false, table.changeSelection(), clears the previous selection, and starts a new selection, but if toggle is true and extend is false, the previous selection is not cleared and the selection state of the cell referred to is simply toggled. That allowed the following code to work (I had to assume that nothing was selected after the aforementioned table changes).

print "val_rws: {}".format(val_rws)
table = event.source.parent.getComponent('ptLengths').table
print "Selecting row: {}".format(val_rws[0])
table.changeSelection(val_rws[0], 0,0,0)
for i in range(1,len(val_rws)):
	print "Selecting row: {}".format(val_rws[i])
	table.changeSelection(val_rws[i],0,1,0)
nwrws = pt.getSelectedRows()
print "nwrws: {}".format(nwrws)
1 Like