[GH-ISSUE #258] Adding Rows to Table After Root is set #201

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

Originally created by @SaltyPotatoes2 on GitHub (Apr 4, 2019).
Original GitHub issue: https://github.com/rivo/tview/issues/258

I'm noticing that it is impossible to add rows to Tables using table.InsertRow after app.SetRoot has been set. Is this intentional, is there a way to allow the additions of rows after the fact?

Originally created by @SaltyPotatoes2 on GitHub (Apr 4, 2019). Original GitHub issue: https://github.com/rivo/tview/issues/258 I'm noticing that it is impossible to add rows to Tables using table.InsertRow after app.SetRoot has been set. Is this intentional, is there a way to allow the additions of rows after the fact?
kerem closed this issue 2026-03-04 01:02:56 +03:00
Author
Owner

@rivo commented on GitHub (Apr 5, 2019):

I don't see why this wouldn't work. Here's an example:

package main

import (
	"fmt"

	"github.com/gdamore/tcell"
	"github.com/rivo/tview"
)

func main() {
	table := tview.NewTable().
		SetBorders(true)

	for row := 0; row < 5; row++ {
		for col := 0; col < 5; col++ {
			table.SetCellSimple(row, col, fmt.Sprintf("%d,%d", row, col))
		}
	}

	table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Key() == tcell.KeyEnter {
			table.InsertRow(0).SetCellSimple(0, 0, "Inserted")
		}
		return event
	})

	if err := tview.NewApplication().SetRoot(table, true).Run(); err != nil {
		panic(err)
	}
}

Maybe you're updating the table in a goroutine? If so, please check out this Wiki article on concurrency in tview.

<!-- gh-comment-id:480430519 --> @rivo commented on GitHub (Apr 5, 2019): I don't see why this wouldn't work. Here's an example: ```go package main import ( "fmt" "github.com/gdamore/tcell" "github.com/rivo/tview" ) func main() { table := tview.NewTable(). SetBorders(true) for row := 0; row < 5; row++ { for col := 0; col < 5; col++ { table.SetCellSimple(row, col, fmt.Sprintf("%d,%d", row, col)) } } table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Key() == tcell.KeyEnter { table.InsertRow(0).SetCellSimple(0, 0, "Inserted") } return event }) if err := tview.NewApplication().SetRoot(table, true).Run(); err != nil { panic(err) } } ``` Maybe you're updating the table in a goroutine? If so, please check out [this Wiki article](https://github.com/rivo/tview/wiki/Concurrency) on concurrency in `tview`.
Author
Owner

@SaltyPotatoes2 commented on GitHub (Apr 7, 2019):

I was unable to get QueueUpdateDraw to work, or anything outside of a tview event function; so I got it updating by adding this as my main loop:

	for {
		if time.Now().After(after) {
			app.Draw()
			after = time.Now().Add(1 * time.Second)
		}
	}

Then an After Draw function:

	app.SetAfterDrawFunc(func(tcell.Screen) {
		mainLoop()
	})

Not ideal, but not terrible by any means.

<!-- gh-comment-id:480622742 --> @SaltyPotatoes2 commented on GitHub (Apr 7, 2019): I was unable to get QueueUpdateDraw to work, or anything outside of a tview event function; so I got it updating by adding this as my main loop: ``` for { if time.Now().After(after) { app.Draw() after = time.Now().Add(1 * time.Second) } } ``` Then an After Draw function: ``` app.SetAfterDrawFunc(func(tcell.Screen) { mainLoop() }) ``` Not ideal, but not terrible by any means.
Author
Owner

@rivo commented on GitHub (May 13, 2019):

This looks to me like you'll have an extremely busy loop (maybe time.Sleep() would be better for something like this?).

I don't know why QueueUpdateDraw() did not work for you but if you would like me to follow up on it, please send me an example where you tried it and where it didn't work. I'll reopen this issue in that case.

<!-- gh-comment-id:491823887 --> @rivo commented on GitHub (May 13, 2019): This looks to me like you'll have an extremely busy loop (maybe `time.Sleep()` would be better for something like this?). I don't know why `QueueUpdateDraw()` did not work for you but if you would like me to follow up on it, please send me an example where you tried it and where it didn't work. I'll reopen this issue in that case.
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#201
No description provided.