[GH-ISSUE #683] The default behavior of (*Table).Select is broken #500

Closed
opened 2026-03-04 01:05:31 +03:00 by kerem · 1 comment
Owner

Originally created by @darkhz on GitHub (Dec 28, 2021).
Original GitHub issue: https://github.com/rivo/tview/issues/683

(*Table).Select would select rows/columns that are outside the visible area by default.

However, after the introduction of virtual tables(78cf54e8302c30a568db07a52ec85b77e44eb469), this behavior is broken. When attempting to select cells that are outside the visible area, the table does not automatically scroll to the selection.

I traced the problem to line 872 in table.go. clampToSelection is always false when evaluating t.clampToSelection && t.rowsSelectable , therefore the resulting code block does not execute and the table is not scrolled according to the selection.

The following code will demonstrate this issue. Revert and re-add 78cf54e830 to see the difference in behaviors.

package main

import (
	"strconv"

	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

func main() {
	app := tview.NewApplication()
	table := tview.NewTable().
		SetBorders(true).
		SetSelectable(true, false)

	table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		switch event.Rune() {
		case 'a':
			//Get the height of the table
			_, _, _, h := table.GetRect()

			//Get the selected row and the total rows
			row, _ := table.GetSelection()
			rows := table.GetRowCount() - 1

			//Select a cell that is outside the visible area
			step := 4
			outrow := row + (h / 2) + step

			if outrow >= rows {
				outrow = rows
			}

			table.Select(outrow, 0)
		}

		return event
	})

	for i := 1; i <= 100; i++ {
		table.SetCell(i-1, 0, tview.NewTableCell(strconv.Itoa(i)).
			SetExpansion(1))
	}

	if err := app.SetRoot(table, true).SetFocus(table).Run(); err != nil {
		panic(err)
	}
}
Originally created by @darkhz on GitHub (Dec 28, 2021). Original GitHub issue: https://github.com/rivo/tview/issues/683 (*Table).Select would select rows/columns that are outside the visible area by default.<br> However, after the introduction of virtual tables(78cf54e8302c30a568db07a52ec85b77e44eb469), this behavior is broken. When attempting to select cells that are outside the visible area, the table does not automatically scroll to the selection. I traced the problem to [line 872 in table.go](https://github.com/rivo/tview/blob/2a6de950f73bdc70658f7e754d4b5593f15c8408/table.go#L872). `clampToSelection` is always false when evaluating `t.clampToSelection && t.rowsSelectable `, therefore the resulting code block does not execute and the table is not scrolled according to the selection. The following code will demonstrate this issue. Revert and re-add 78cf54e8302c30a568db07a52ec85b77e44eb469 to see the difference in behaviors. ``` package main import ( "strconv" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() table := tview.NewTable(). SetBorders(true). SetSelectable(true, false) table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { switch event.Rune() { case 'a': //Get the height of the table _, _, _, h := table.GetRect() //Get the selected row and the total rows row, _ := table.GetSelection() rows := table.GetRowCount() - 1 //Select a cell that is outside the visible area step := 4 outrow := row + (h / 2) + step if outrow >= rows { outrow = rows } table.Select(outrow, 0) } return event }) for i := 1; i <= 100; i++ { table.SetCell(i-1, 0, tview.NewTableCell(strconv.Itoa(i)). SetExpansion(1)) } if err := app.SetRoot(table, true).SetFocus(table).Run(); err != nil { panic(err) } } ```
kerem closed this issue 2026-03-04 01:05:32 +03:00
Author
Owner

@rivo commented on GitHub (Feb 15, 2022):

This was not so much about virtual tables but rather something I needed to do for #618.

I made a change that will turn on clamping when Table.Select() is called.

<!-- gh-comment-id:1040558133 --> @rivo commented on GitHub (Feb 15, 2022): This was not so much about virtual tables but rather something I needed to do for #618. I made a change that will turn on clamping when `Table.Select()` is called.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#500
No description provided.