[GH-ISSUE #700] Table search-bar without changing focus #511

Closed
opened 2026-03-04 01:05:38 +03:00 by kerem · 3 comments
Owner

Originally created by @quantumsheep on GitHub (Feb 8, 2022).
Original GitHub issue: https://github.com/rivo/tview/issues/700

This is more of a question than an issue/feature request.

Is it possible to simply implement a search-bar along with a table without having to change focus between the two ?
I'm actually doing this with a custom Flex widget I made but I would love it if it's already implemented natively.

Originally created by @quantumsheep on GitHub (Feb 8, 2022). Original GitHub issue: https://github.com/rivo/tview/issues/700 This is more of a question than an issue/feature request. Is it possible to simply implement a search-bar along with a table without having to change focus between the two ? I'm actually doing this with a custom `Flex` widget I made but I would love it if it's already implemented natively.
kerem closed this issue 2026-03-04 01:05:38 +03:00
Author
Owner

@darkhz commented on GitHub (Feb 10, 2022):

Yes it is possible. If you could explain your use case more clearly, I could explain better. But generally, you can focus on the InputField, and map certain InputField events like KeyUp,KeyDown etc. (via SetInputCapture) to be sent to the table's InputHandler.

Test this code for instance, you can type in the InputField as well as select columns in the Table.

package main

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

func main() {
	app := tview.NewApplication()

	table := tview.NewTable()
	table.SetSelectable(true, false)

	for i, s := range []string{
		"Select",
		"these",
		"columns",
		"while",
		"typing",
		"in",
		"the",
		"InputField",
	} {
		table.SetCellSimple(i, 0, s)
	}

	input := tview.NewInputField()
	input.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		switch event.Key() {
		case tcell.KeyUp, tcell.KeyDown:
			table.InputHandler()(event, nil)
		}

		return event
	})

	flex := tview.NewFlex().
		AddItem(input, 1, 0, true).
		AddItem(table, 0, 10, false).
		SetDirection(tview.FlexRow)

	if err := app.SetRoot(flex, true).Run(); err != nil {
		panic(err)
	}
}
<!-- gh-comment-id:1035512972 --> @darkhz commented on GitHub (Feb 10, 2022): Yes it is possible. If you could explain your use case more clearly, I could explain better. But generally, you can focus on the InputField, and map certain InputField events like KeyUp,KeyDown etc. (via SetInputCapture) to be sent to the table's InputHandler. Test this code for instance, you can type in the InputField as well as select columns in the Table. ``` package main import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() table := tview.NewTable() table.SetSelectable(true, false) for i, s := range []string{ "Select", "these", "columns", "while", "typing", "in", "the", "InputField", } { table.SetCellSimple(i, 0, s) } input := tview.NewInputField() input.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { switch event.Key() { case tcell.KeyUp, tcell.KeyDown: table.InputHandler()(event, nil) } return event }) flex := tview.NewFlex(). AddItem(input, 1, 0, true). AddItem(table, 0, 10, false). SetDirection(tview.FlexRow) if err := app.SetRoot(flex, true).Run(); err != nil { panic(err) } } ```
Author
Owner

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

Thanks, @darkhz. That's an elegant solution.

But to answer the specific question, only one primitive can have focus at any given time.

You could also capture events in the Flex component. Depends on what you want to achieve.

<!-- gh-comment-id:1041859064 --> @rivo commented on GitHub (Feb 16, 2022): Thanks, @darkhz. That's an elegant solution. But to answer the specific question, only one primitive can have focus at any given time. You could also capture events in the `Flex` component. Depends on what you want to achieve.
Author
Owner

@rivo commented on GitHub (Dec 17, 2022):

I'm closing this as it looks resolved to me.

<!-- gh-comment-id:1356328636 --> @rivo commented on GitHub (Dec 17, 2022): I'm closing this as it looks resolved to me.
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#511
No description provided.