mirror of
https://github.com/rivo/tview.git
synced 2026-04-26 13:25:51 +03:00
[GH-ISSUE #498] Invalid selection in tables with rows of different lengths #363
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/tview#363
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @rlmaers on GitHub (Sep 10, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/498
Background
I'm working on an application that use the table primitive and where each row might have different number of columns, and I've stumbled upon a bug regarding selection.
Description
The following code creates a table with an non-selectable header row with one column and a selectable second row with two columns. When pressing up, the initial selection of the second row simply disappears.
Cause
I believe the bug (in this case) is caused by the following code in the
Drawmethod.After pressing up both
t.selectedRow == -1andt.selectedColumn == t.lastColumnare true before this code is executed. On the loops first iteration we havegetCell(0, 1) == nilbecause the first row only has one column, and the loop is terminated resulting in an invalid selection.Workaround
A simple workaround is to make sure that all rows have the same number of columns, and inserting dummy columns where needed.
Fix
I'm not that familiar with the library, so I might be wrong; but a possible fix is probably to replace the static value
t.lastColumnwith the dynamic valuelen(t.cells[t.selectedRow])-1in all appropriate places.I understand if fixing this issue is not prioritized given that there's a simple workaround.
@rivo commented on GitHub (Dec 4, 2020):
Thanks for letting me know about this. There were still bugs in finding the next selectable cell. The latest commit should resolve your problem (and hopefully similar other ones, too).