[GH-ISSUE #1005] Help: Is there an example somewheres of a table where you can highlight and select an entire row? #729

Open
opened 2026-03-04 01:07:19 +03:00 by kerem · 1 comment
Owner

Originally created by @SimplySeth on GitHub (Jul 12, 2024).
Original GitHub issue: https://github.com/rivo/tview/issues/1005

Is there an example somewheres of a table where you can highlight and select an entire row?

This is what I'm trying so far:

func TableView(app *tview.Application) *tview.Table {
	blob := readJsonFile()
	data := blob["result"][0:10] # perhaps I have to implement paging
	headers := mkTaskHeaders(blob)
	rowCount := len(data)
	table := tview.NewTable().
		SetBorders(true).
		SetBordersColor(tcell.ColorYellow)
	for r := 0; r < rowCount; r++ {
		for i, c := range headers {
			if r == 0 {
				table.SetCell(r, i, tview.NewTableCell(mkTitle(c)).
					SetAlign(tview.AlignCenter))
			}
			if c == "short_description" {
				table.SetCell(
					r+1, i, tview.NewTableCell(data[r][c]).
						SetAlign(tview.AlignLeft))
			} else {
				table.SetCell(
					r+1, i, tview.NewTableCell(data[r][c]).
						SetAlign(tview.AlignCenter))
			}
		}
		table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
			selected, _ := table.GetSelectable()

			if event.Key() == tcell.KeyEnter {
				table.SetSelectable(true, true)
			}
			if selected && event.Key() == tcell.KeyEnter {
				row, col := table.GetSelection()
				cell := table.GetCell(row, col).
					SetBackgroundColor(tcell.ColorYellow)
				fmt.Println(cell.Reference)
				fmt.Println(cell.Attributes)
				fmt.Println(cell.Text)
			}
			return event
		})

	}
	return table
}

Originally created by @SimplySeth on GitHub (Jul 12, 2024). Original GitHub issue: https://github.com/rivo/tview/issues/1005 Is there an example somewheres of a table where you can highlight and select an entire row? This is what I'm trying so far: ``` func TableView(app *tview.Application) *tview.Table { blob := readJsonFile() data := blob["result"][0:10] # perhaps I have to implement paging headers := mkTaskHeaders(blob) rowCount := len(data) table := tview.NewTable(). SetBorders(true). SetBordersColor(tcell.ColorYellow) for r := 0; r < rowCount; r++ { for i, c := range headers { if r == 0 { table.SetCell(r, i, tview.NewTableCell(mkTitle(c)). SetAlign(tview.AlignCenter)) } if c == "short_description" { table.SetCell( r+1, i, tview.NewTableCell(data[r][c]). SetAlign(tview.AlignLeft)) } else { table.SetCell( r+1, i, tview.NewTableCell(data[r][c]). SetAlign(tview.AlignCenter)) } } table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { selected, _ := table.GetSelectable() if event.Key() == tcell.KeyEnter { table.SetSelectable(true, true) } if selected && event.Key() == tcell.KeyEnter { row, col := table.GetSelection() cell := table.GetCell(row, col). SetBackgroundColor(tcell.ColorYellow) fmt.Println(cell.Reference) fmt.Println(cell.Attributes) fmt.Println(cell.Text) } return event }) } return table } ```
Author
Owner

@rivo commented on GitHub (Aug 4, 2024):

From what I can see, you don't need to call SetInputCapture(), especially not for each row. Just call table.SetSelectable(true, false) once when initializing your table. Use SetSelectedFunc() to listen for selections (Enter key). It's all there in the documentation.

For an example of this, check out the demo presentation included in the package. The code for selecting rows is here: github.com/rivo/tview@65571ae51e/demos/presentation/table.go (L311)

<!-- gh-comment-id:2267503368 --> @rivo commented on GitHub (Aug 4, 2024): From what I can see, you don't need to call `SetInputCapture()`, especially not for each row. Just call [`table.SetSelectable(true, false)`](https://pkg.go.dev/github.com/rivo/tview#Table.SetSelectable) once when initializing your table. Use [`SetSelectedFunc()`](https://pkg.go.dev/github.com/rivo/tview#Table.SetSelectedFunc) to listen for selections (<kbd>Enter</kbd> key). It's all there in the documentation. For an example of this, check out the demo presentation included in the package. The code for selecting rows is here: https://github.com/rivo/tview/blob/65571ae51e71d29a5e8a0082667b778939cca29f/demos/presentation/table.go#L311
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#729
No description provided.