Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: get table content #1037

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pavigor
Copy link

@pavigor pavigor commented Oct 14, 2024

This PR allow access to table content. It can be useful in case when you need to replace one table to another without creating any time new table.

package main

import (
	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

func main() {
    grid := tview.NewGrid().SetRows(2, 0).SetColumns(3, 0).SetBorders(true)
    var table *tview.Table
    table1 := tview.NewTable().SetBorders(true)
    table1.SetCell(0, 0, tview.NewTableCell("DataTable1")
    table1.SetCell(0, 1, tview.NewTableCell("DataTable1"))
    table2 := tview.NewTable().SetBorders(true)
    table2.SetCell(0, 0, tview.NewTableCell("DataTable2")
    table2.SetCell(0, 1, tview.NewTableCell("DataTable2"))

    // Tree
   treeView := tview.NewTreeView()
   root := tview.NewTreeNode("Root")
   node1 := tview.NewTreeNode("Node1").SetColor(tcell.ColorGreen)
   node1.SetSelectedFunc(func() {
        // Dynamic update table content
	table.SetContent(table1.GetContent())
   })
   node2 := tview.NewTreeNode("Node1")
   node2.SetSelectedFunc(func() {
        // Dynamic update table content
	table.SetContent(table2.GetContent())
   })
   root.AddChild(node1).AddChild(node2)
   treeView.SetRoot(root).SetCurrentNode(root)

   grid.AddItem(treeView, 0, 0, 2, 2, 0, 0, true)
   grid.AddItem(table, 0, 2, 2, 2, 0, 0, false)

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

@@ -562,6 +562,11 @@ func (t *Table) SetContent(content TableContent) *Table {
return t
}

// GetContent returns current table's content
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/rivo/tview/blob/master/CONTRIBUTING.md:

Start all sentences upper-case [...] and end them with a period.

Also, I think you mean "the table's current content."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants