[GH-ISSUE #46] request for sample code on how to make selected TableCell editable for data entry #31

Closed
opened 2026-03-04 01:01:16 +03:00 by kerem · 4 comments
Owner

Originally created by @githubfanster on GitHub (Jan 29, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/46

hello! do you have any sample code on how to make a selected TableCell editable for data entry? i'm not sure what the magic incantation is, but i'm assuming a selected TableCell can be made editable?

thanks for any help!

Originally created by @githubfanster on GitHub (Jan 29, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/46 hello! do you have any sample code on how to make a selected TableCell editable for data entry? i'm not sure what the magic incantation is, but i'm assuming a selected TableCell can be made editable? thanks for any help!
kerem closed this issue 2026-03-04 01:01:16 +03:00
Author
Owner

@rivo commented on GitHub (Jan 30, 2018):

Hi,

The Table class itself doesn't have editing built-in. However, the TableCell class has the function GetLastPosition() which tells you where on the screen the table cell was drawn.

You could temporarily place an InputField on top of the table at that exact position to make it look like the cell can be edited.

Let me know if this helps.

<!-- gh-comment-id:361497341 --> @rivo commented on GitHub (Jan 30, 2018): Hi, The `Table` class itself doesn't have editing built-in. However, the `TableCell` class has the function [`GetLastPosition()`](https://godoc.org/github.com/rivo/tview#TableCell.GetLastPosition) which tells you where on the screen the table cell was drawn. You could temporarily place an [`InputField`](https://godoc.org/github.com/rivo/tview#InputField) on top of the table at that exact position to make it look like the cell can be edited. Let me know if this helps.
Author
Owner

@githubfanster commented on GitHub (Jan 30, 2018):

thanks for the reply. i'm not sure how to draw the InputField to screen
using the selectedCell's position. can you help?

            table := tview.NewTable().SetBorders(true)
    table.SetSelectable(true, true).SetSelectedFunc(func(row, column

int) {
fmt.Printf("selected row: %d, column: %d\n", row, column)
selectedCell := table.GetCell(row, column)
x, y, width := selectedCell.GetLastPosition()

inpFld := tview.NewInputField()

               //** how to superimpose InputField on selected cell's

position on the screen?
//** do i need height info?
//** how do i draw the InputField to make it show up?
inpFld.SetRect(x, y, width, 20)
inpFld.SetText(selectedCell.Text)
})

On Tue, Jan 30, 2018 at 3:11 PM, rivo notifications@github.com wrote:

Hi,

The Table class itself doesn't have editing built-in. However, the
TableCell class has the function GetLastPosition()
https://godoc.org/github.com/rivo/tview#TableCell.GetLastPosition which
tells you where on the screen the table cell was drawn.

You could temporarily place an InputField
https://godoc.org/github.com/rivo/tview#InputField on top of the table
at that exact position to make it look like the cell can be edited.

Let me know if this helps.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/rivo/tview/issues/46#issuecomment-361497341, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AhuMfU5v0I5Es6rS_zMUdue0T4j22-5kks5tPsCYgaJpZM4Rwozu
.

<!-- gh-comment-id:361523585 --> @githubfanster commented on GitHub (Jan 30, 2018): thanks for the reply. i'm not sure how to draw the InputField to screen using the selectedCell's position. can you help? table := tview.NewTable().SetBorders(true) table.SetSelectable(true, true).SetSelectedFunc(func(row, column int) { fmt.Printf("selected row: %d, column: %d\n", row, column) selectedCell := table.GetCell(row, column) x, y, width := selectedCell.GetLastPosition() inpFld := tview.NewInputField() //** how to superimpose InputField on selected cell's position on the screen? //** do i need height info? //** how do i draw the InputField to make it show up? inpFld.SetRect(x, y, width, 20) inpFld.SetText(selectedCell.Text) }) On Tue, Jan 30, 2018 at 3:11 PM, rivo <notifications@github.com> wrote: > Hi, > > The Table class itself doesn't have editing built-in. However, the > TableCell class has the function GetLastPosition() > <https://godoc.org/github.com/rivo/tview#TableCell.GetLastPosition> which > tells you where on the screen the table cell was drawn. > > You could temporarily place an InputField > <https://godoc.org/github.com/rivo/tview#InputField> on top of the table > at that exact position to make it look like the cell can be edited. > > Let me know if this helps. > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/rivo/tview/issues/46#issuecomment-361497341>, or mute > the thread > <https://github.com/notifications/unsubscribe-auth/AhuMfU5v0I5Es6rS_zMUdue0T4j22-5kks5tPsCYgaJpZM4Rwozu> > . >
Author
Owner

@rivo commented on GitHub (Jan 30, 2018):

You're probably going to have to use the Pages primitive to superimpose the input field on the table. Here's an example of what that could look like:

// Demo code for the Table primitive.
package main

import (
	"strings"

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

func main() {
	pages := tview.NewPages()

	// Create table.
	table := tview.NewTable().
		SetSelectable(true, true).
		SetFixed(1, 1)
	lorem := strings.Split("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.", " ")
	cols, rows := 10, 40
	word := 0
	for r := 0; r < rows; r++ {
		for c := 0; c < cols; c++ {
			color := tcell.ColorWhite
			if c < 1 || r < 1 {
				color = tcell.ColorYellow
			}
			table.SetCell(r, c,
				tview.NewTableCell(lorem[word]).
					SetTextColor(color).
					SetAlign(tview.AlignCenter).
					SetSelectable(c > 0 && r > 0))
			word = (word + 1) % len(lorem)
		}
	}
	table.SetSelectedFunc(func(row int, column int) {
		// A cell was selected. Let the user edit it.
		cell := table.GetCell(row, column)
		var inputField *tview.InputField
		inputField = tview.NewInputField().
			SetText(cell.Text).
			SetDoneFunc(func(key tcell.Key) {
				cell.SetText(inputField.GetText())
				pages.RemovePage("edit")
			})
		x, y, width := cell.GetLastPosition()
		inputField.SetRect(x, y, width+1, 1)
		pages.AddPage("edit", inputField, false, true)
	})

	// Set up pages.
	pages.AddPage("table", table, true, true)

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

It's a working example. (A modification of the table demo.)

<!-- gh-comment-id:361529254 --> @rivo commented on GitHub (Jan 30, 2018): You're probably going to have to use the `Pages` primitive to superimpose the input field on the table. Here's an example of what that could look like: ```go // Demo code for the Table primitive. package main import ( "strings" "github.com/gdamore/tcell" "github.com/rivo/tview" ) func main() { pages := tview.NewPages() // Create table. table := tview.NewTable(). SetSelectable(true, true). SetFixed(1, 1) lorem := strings.Split("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.", " ") cols, rows := 10, 40 word := 0 for r := 0; r < rows; r++ { for c := 0; c < cols; c++ { color := tcell.ColorWhite if c < 1 || r < 1 { color = tcell.ColorYellow } table.SetCell(r, c, tview.NewTableCell(lorem[word]). SetTextColor(color). SetAlign(tview.AlignCenter). SetSelectable(c > 0 && r > 0)) word = (word + 1) % len(lorem) } } table.SetSelectedFunc(func(row int, column int) { // A cell was selected. Let the user edit it. cell := table.GetCell(row, column) var inputField *tview.InputField inputField = tview.NewInputField(). SetText(cell.Text). SetDoneFunc(func(key tcell.Key) { cell.SetText(inputField.GetText()) pages.RemovePage("edit") }) x, y, width := cell.GetLastPosition() inputField.SetRect(x, y, width+1, 1) pages.AddPage("edit", inputField, false, true) }) // Set up pages. pages.AddPage("table", table, true, true) if err := tview.NewApplication().SetRoot(pages, true).SetFocus(table).Run(); err != nil { panic(err) } } ``` It's a working example. (A modification of the table demo.)
Author
Owner

@githubfanster commented on GitHub (Jan 30, 2018):

thank you very much. that worked for me!

On Tue, Jan 30, 2018 at 5:24 PM, rivo notifications@github.com wrote:

You're probably going to have to use the Pages primitive to superimpose
the input field on the table. Here's an example of what that could look
like:

// Demo code for the Table primitive.package main
import (
"strings"

"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
func main() {
pages := tview.NewPages()

// Create table.
table := tview.NewTable().
SetSelectable(true, true).
SetFixed(1, 1)
lorem := strings.Split("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.", " ")
cols, rows := 10, 40
word := 0
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
color := tcell.ColorWhite
if c < 1 || r < 1 {
color = tcell.ColorYellow
}
table.SetCell(r, c,
tview.NewTableCell(lorem[word]).
SetTextColor(color).
SetAlign(tview.AlignCenter).
SetSelectable(c > 0 && r > 0))
word = (word + 1) % len(lorem)
}
}
table.SetSelectedFunc(func(row int, column int) {
// A cell was selected. Let the user edit it.
cell := table.GetCell(row, column)
var inputField *tview.InputField
inputField = tview.NewInputField().
SetText(cell.Text).
SetDoneFunc(func(key tcell.Key) {
cell.SetText(inputField.GetText())
pages.RemovePage("edit")
})
x, y, width := cell.GetLastPosition()
inputField.SetRect(x, y, width+1, 1)
pages.AddPage("edit", inputField, false, true)
})

// Set up pages.
pages.AddPage("table", table, true, true)

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

It's a working example. (A modification of the table demo.)


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/rivo/tview/issues/46#issuecomment-361529254, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AhuMfa-Vgek97WtP07_a6NVWapAz1rLSks5tPt-ygaJpZM4Rwozu
.

<!-- gh-comment-id:361533952 --> @githubfanster commented on GitHub (Jan 30, 2018): thank you very much. that worked for me! On Tue, Jan 30, 2018 at 5:24 PM, rivo <notifications@github.com> wrote: > You're probably going to have to use the Pages primitive to superimpose > the input field on the table. Here's an example of what that could look > like: > > // Demo code for the Table primitive.package main > import ( > "strings" > > "github.com/gdamore/tcell" > "github.com/rivo/tview" > ) > func main() { > pages := tview.NewPages() > > // Create table. > table := tview.NewTable(). > SetSelectable(true, true). > SetFixed(1, 1) > lorem := strings.Split("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.", " ") > cols, rows := 10, 40 > word := 0 > for r := 0; r < rows; r++ { > for c := 0; c < cols; c++ { > color := tcell.ColorWhite > if c < 1 || r < 1 { > color = tcell.ColorYellow > } > table.SetCell(r, c, > tview.NewTableCell(lorem[word]). > SetTextColor(color). > SetAlign(tview.AlignCenter). > SetSelectable(c > 0 && r > 0)) > word = (word + 1) % len(lorem) > } > } > table.SetSelectedFunc(func(row int, column int) { > // A cell was selected. Let the user edit it. > cell := table.GetCell(row, column) > var inputField *tview.InputField > inputField = tview.NewInputField(). > SetText(cell.Text). > SetDoneFunc(func(key tcell.Key) { > cell.SetText(inputField.GetText()) > pages.RemovePage("edit") > }) > x, y, width := cell.GetLastPosition() > inputField.SetRect(x, y, width+1, 1) > pages.AddPage("edit", inputField, false, true) > }) > > // Set up pages. > pages.AddPage("table", table, true, true) > > if err := tview.NewApplication().SetRoot(pages, true).SetFocus(table).Run(); err != nil { > panic(err) > } > } > > It's a working example. (A modification of the table demo.) > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/rivo/tview/issues/46#issuecomment-361529254>, or mute > the thread > <https://github.com/notifications/unsubscribe-auth/AhuMfa-Vgek97WtP07_a6NVWapAz1rLSks5tPt-ygaJpZM4Rwozu> > . >
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#31
No description provided.