[GH-ISSUE #510] Creating input field dynamically on a form #370

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

Originally created by @JCzz on GitHub (Oct 7, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/510

Hi and thank you very much for this well structured package!

I am sure I am doing something wrong, I have the following "working" code - but it is not optimal. Can you see if there is a better way?

I am adding InputFields dynamically to a Form, but it is like the enclosed keys is always the last key. Therefore this do not work:
inputFormItems.InputFields[sortKey]
in the accept or changed function in AddInputField

In Pseudo code:

  1. add InputFielde from a list of key values
  2. update the list with any changes on each InputField

This code works:

var keys []string
for key := range inputFormItems.InputFields {
    keys = append(keys, key)
}
sort.Strings(keys)

for _, sortKey := range keys {
    v := inputFormItems.InputFields[sortKey]
    inputForm.AddInputField(sortKey, v, 20, nil, func(text string) {
        i, _ := inputForm.GetFocusedItemIndex()
        inputFormItems.InputFields[inputForm.GetFormItem(i).GetLabel()] = text

    })
}

Thanks again and I hope I was able to describe the issue

Originally created by @JCzz on GitHub (Oct 7, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/510 Hi and thank you very much for this well structured package! I am sure I am doing something wrong, I have the following "working" code - but it is not optimal. Can you see if there is a better way? I am adding InputFields dynamically to a Form, but it is like the enclosed keys is always the last key. Therefore this do not work: `inputFormItems.InputFields[sortKey]` in the accept or changed function in AddInputField In Pseudo code: 1. add InputFielde from a list of key values 2. update the list with any changes on each InputField This code works: ``` var keys []string for key := range inputFormItems.InputFields { keys = append(keys, key) } sort.Strings(keys) for _, sortKey := range keys { v := inputFormItems.InputFields[sortKey] inputForm.AddInputField(sortKey, v, 20, nil, func(text string) { i, _ := inputForm.GetFocusedItemIndex() inputFormItems.InputFields[inputForm.GetFormItem(i).GetLabel()] = text }) } ``` Thanks again and I hope I was able to describe the issue
kerem closed this issue 2026-03-04 01:04:25 +03:00
Author
Owner

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

Form.AddInputField adds a new InputField to the form. Form.AddFormItem may be used to add an existing InputField.

<!-- gh-comment-id:708633206 --> @tslocum commented on GitHub (Oct 14, 2020): [Form.AddInputField](https://godoc.org/github.com/rivo/tview#Form.AddInputField) adds a *new* `InputField` to the form. [Form.AddFormItem](https://godoc.org/github.com/rivo/tview#Form.AddFormItem) may be used to add an existing `InputField`.
Author
Owner

@JCzz commented on GitHub (Dec 27, 2020):

Thanks

<!-- gh-comment-id:751495397 --> @JCzz commented on GitHub (Dec 27, 2020): Thanks
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#370
No description provided.