Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SimplySeth opened this issue Jul 12, 2024 · 1 comment

Comments

@SimplySeth
Copy link

SimplySeth commented Jul 12, 2024

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
}

@rivo
Copy link
Owner

rivo commented 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:

table.SetBorders(false).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants