# Get selected row of table after sorting

**URL:** <https://forum.inductiveautomation.com/t/get-selected-row-of-table-after-sorting/18079>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [May 17, 2018, 5:46pm UTC](https://forum.inductiveautomation.com/t/get-selected-row-of-table-after-sorting/18079 "2018-05-17T17:46:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stuart](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Stuart](https://forum.inductiveautomation.com/u/Stuart)\
**Post date:** [May 17, 2018, 5:46pm UTC](https://forum.inductiveautomation.com/t/get-selected-row-of-table-after-sorting/18079/1 "2018-05-17T17:46:40Z")

</div>

I have some code that gets values from a selected row in a table, that works well as long as the table has not been sorted (clicking on a column header). Here is the code that I am using to get the values. This is on a button next to the table.

```auto
# Get the table component
tableComponent = event.source.parent.getComponent('Power Table')
table = tableComponent.getTable()

# Get the selected rows
selectedRows = table.getSelectedRows()

# Loop through the list of selected rows (will just be 1)
for r in selectedRows:
	intCell = tableComponent.data.getValueAt(r,"Int Column")
	stringCell = tableComponent.data.getValueAt(r,"String Column")

# Do something with the results
print (intCell)
print (stringCell)

```

That code works perfectly until the table has been sorted. Once it is sorted, this no longer works. After a sort, it doesn’t look like the underlying dataset is changed to reflect the sort. After the sort, if I choose the 6th row down, I will get the 7th (starts at 0) row from the origianl dataset.

How can I prevent this from happening?

Should / Can I rebuild the dataset after every sort? This seems like it could get a bit messy / process intensive.

Thanks,

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 17, 2018, 6:08pm UTC](https://forum.inductiveautomation.com/t/get-selected-row-of-table-after-sorting/18079/2 "2018-05-17T18:08:38Z")

</div>

Power tables are complicated. Here’s some code that should always get you the right rows, regardless of sorting or filtering:

```auto
from com.jidesoft.grid import TableModelWrapperUtils
lsm = self.getTable().getSelectionModel()
start = lsm.getMinSelectionIndex()
end = lsm.getMaxSelectionIndex() + 1
output = []
for row in range(start, end):
	if lsm.isSelectedIndex(row):
		output.append(row)
		
output = TableModelWrapperUtils.getActualRowsAt(root.getTable().getModel(), output, True)

```

---

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [May 17, 2018, 6:57pm UTC](https://forum.inductiveautomation.com/t/get-selected-row-of-table-after-sorting/18079/3 "2018-05-17T18:57:54Z")

</div>

I just made a slight change to your code, and I believe that you could accomplish what you want by using `viewDataset` instead of the `data` property of the `Power Table` component.

```auto
# Get the table component
tableComponent = event.source.parent.getComponent('Power Table')
table = tableComponent.getTable()

# Get the selected rows
selectedRows = table.getSelectedRows()

# Loop through the list of selected rows (will just be 1)
for r in selectedRows:
	intCell = tableComponent.viewDataset.getValueAt(r,"Int Column")
	stringCell = tableComponent.viewDataset.getValueAt(r,"String Column")

# Do something with the results
print (intCell)
print (stringCell)

```

Just bare in mind that with the `viewDataset` you can only process the _visible_ columns. If you want to access hidden columns, you should be using `data` instead.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Stuart](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Stuart](https://forum.inductiveautomation.com/u/Stuart)\
**Post date:** [May 17, 2018, 7:54pm UTC](https://forum.inductiveautomation.com/t/get-selected-row-of-table-after-sorting/18079/4 "2018-05-17T19:54:41Z")

</div>

Nice and easy. This will work for some tables, but for many of mine, I am selecting the primary key as part of my query and hiding it for later use. I’ll try out @paul-griffith’s answer or maybe just disable sorting on those tables…

Thanks.
