Bug: index (i) refers to wrong table row after sorting

I have a table with a custom column of type Button.

The button calls a js query which uses i to determine which row called it. If the table is not sorted, mytable.data[i] returns the correct row. However, if I sort table mytable.data[i] returns the wrong row.

i is always the displayed row number, i.e. it will always return 0 if the first row is selected no matter the table sort. As a result clicking the first row's button will always return mytable.data[0] even if the table is sorted in descending order.

@bradlymathews

Hey there :wave: Would you be able to use table.selectedRow to return the clicked row?

Yeah, that sounds like a good workaround.

Hi, I am having the same problem, and the workaround is not really a solution since I cannot make the row as selected in this case.
I have also tried to use the table.displayedData array but it seems like that there isn't an array that matches what is really shown in the table.
Do you plan to fix the "i" index? Is there any other solution I am missing?
Thanks for the help.
G.

Hey @gtron thanks for reaching out :wave: Linking my answer to this question in another thread: Sorting by custom column - #2 by lauren.gus

Depending on your use case, you could use an action button instead of a custom column with 'select row on click' toggled on :slightly_smiling_face:

Hi @lauren.gus,

I could not apply exactly your suggestion because my table allows multiple rows selection.
Anyway, I managed to convert my button to an action in one use case, and found out that the "i" index works properly when triggering a query from the Action.

Unfortunately, this is still a workaround and it doesn't cover all cases.
It would be nice to have a properly working index also when calling from a Button.

Many thanks for the help!

@gtron

Happy to help! If your table allows multiple rows would you be able to grab the most recent selection (if that is what you are needing for your use case)?

I'm having the same problem. I can't use selectedRow in this case. I want a very simple thing: To retrieve the row that corresponds to the selectedCell, why is that so hard?

Hey @cto_thelions :wave: Did my previous suggestion not work -- that should get the information for the most recently selected row. Would you mind sharing more about your use case?

@cto_thelions I had hit the same bug last week but when building a demo app for retoolers to look at, I couldn't reproduce. Maybe it was a browser (mem) issue but the following code outputs the expected row all the time now, even when filters are set and table is large (22K for me):

const row = table.displayedData[table.selectedCell.index]
console.log(row)