[GH-ISSUE #398] #question Howto update form values in 2 way binding #290

Closed
opened 2026-03-04 01:03:39 +03:00 by kerem · 2 comments
Owner

Originally created by @shaunenslin on GitHub (Feb 7, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/398

Hi
Another question is I would like to change the values on a particular form field, based on what was selected.

By way of explanation, I would like to ask 2 questions via a drop down and then update the extractName field as they go. I do need the option where they can overwrite it in the end.

So, I am updating the extractName var and then performing an app.Draw hoping that will do the trick, but it has not.

Is some sort of 2 way binding available via a screen refresh of sorts?

var selectedSystemIdx, selectedEnvironmentIdx int
	var extractName string
	availableSystems := strings.Split(init.AvailableSystems, ",")
	environments := []string{"TEST", "QA", "LIVE"}
	form := tview.NewForm().
		AddDropDown("Select from the available source systems: (hit Enter):", availableSystems, 0, func(option string, index int) {
			selectedSystemIdx = index
			extractName = availableSystems[selectedSystemIdx]
			p.app.Draw()
		}).
		AddDropDown("What environment is this extract: (hit Enter):", environments, 0, func(option string, index int) {
			selectedEnvironmentIdx = index
			extractName = availableSystems[selectedSystemIdx] + environments[selectedEnvironmentIdx]
			p.app.Draw()
		}).
		AddInputField("Give your extract a name", extractName, 20, nil, func(text string) {
			extractName = text
		}).
		AddInputField("Test", extractName, 20, nil, nil).
		AddButton("Save", func() {
			fmt.Println(selectedSystemIdx, selectedEnvironmentIdx, extractName)
		}).
		AddButton("Quit", func() {
			p.app.Stop()
			os.Exit(0)
		})
	form.SetBorder(true).SetTitle("Enter details for you extract").SetTitleAlign(tview.AlignLeft)
	if err := p.app.SetRoot(form, true).SetFocus(form).Run(); err != nil {
		panic(err)
	}
Originally created by @shaunenslin on GitHub (Feb 7, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/398 Hi Another question is I would like to change the values on a particular form field, based on what was selected. By way of explanation, I would like to ask 2 questions via a drop down and then update the extractName field as they go. I do need the option where they can overwrite it in the end. So, I am updating the extractName var and then performing an app.Draw hoping that will do the trick, but it has not. Is some sort of 2 way binding available via a screen refresh of sorts? ```` var selectedSystemIdx, selectedEnvironmentIdx int var extractName string availableSystems := strings.Split(init.AvailableSystems, ",") environments := []string{"TEST", "QA", "LIVE"} form := tview.NewForm(). AddDropDown("Select from the available source systems: (hit Enter):", availableSystems, 0, func(option string, index int) { selectedSystemIdx = index extractName = availableSystems[selectedSystemIdx] p.app.Draw() }). AddDropDown("What environment is this extract: (hit Enter):", environments, 0, func(option string, index int) { selectedEnvironmentIdx = index extractName = availableSystems[selectedSystemIdx] + environments[selectedEnvironmentIdx] p.app.Draw() }). AddInputField("Give your extract a name", extractName, 20, nil, func(text string) { extractName = text }). AddInputField("Test", extractName, 20, nil, nil). AddButton("Save", func() { fmt.Println(selectedSystemIdx, selectedEnvironmentIdx, extractName) }). AddButton("Quit", func() { p.app.Stop() os.Exit(0) }) form.SetBorder(true).SetTitle("Enter details for you extract").SetTitleAlign(tview.AlignLeft) if err := p.app.SetRoot(form, true).SetFocus(form).Run(); err != nil { panic(err) } ````
kerem closed this issue 2026-03-04 01:03:40 +03:00
Author
Owner

@tslocum commented on GitHub (Feb 14, 2020):

Attach a handler via DropDown.SetDoneFunc to both DropDowns which listens for when the user advances to the next field and prefills the extract name.

<!-- gh-comment-id:586031939 --> @tslocum commented on GitHub (Feb 14, 2020): Attach a handler via [DropDown.SetDoneFunc](https://godoc.org/github.com/rivo/tview#DropDown.SetDoneFunc) to both DropDowns which listens for when the user advances to the next field and prefills the extract name.
Author
Owner

@rivo commented on GitHub (Feb 19, 2020):

Also, p.app.Draw() is not needed here. See my answer in #397.

Please let me know if you still need help with this issue.

<!-- gh-comment-id:588352279 --> @rivo commented on GitHub (Feb 19, 2020): Also, `p.app.Draw()` is not needed here. See my answer in #397. Please let me know if you still need help with this issue.
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#290
No description provided.