tview
tview copied to clipboard
Table search-bar without changing focus
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.
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)
}
}
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.
I'm closing this as it looks resolved to me.