Table
Rows under sortable, movable and resizable columns, built directly or virtualized.
Loading the live example…
import (
"fmt"
"slices"
"strings"
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
)
type tableFile struct {
name, kind string
size int
}
var tableFiles = []tableFile{
{"main.go", "Go source", 12}, {"go.mod", "Module", 1},
{"README.md", "Markdown", 4}, {"logo.png", "Image", 86}, {"LICENSE", "Text", 2},
}
type fileTable struct {
table components.TableState
sort ui.SortOrder
selected string
}
func (f *fileTable) View(c *ui.Context) {
// Column IDs stay stable; the application sorts its own rows.
columns := []ui.TableColumn{
{Title: "Name", ID: "name", Sortable: true, MinWidth: 140},
{Title: "Kind", ID: "kind", Width: 110},
{Title: "Size", ID: "size", Width: 80, Align: ui.End, Sortable: true},
}
f.table.Sort = &f.sort
files := slices.Clone(tableFiles)
slices.SortStableFunc(files, func(a, b tableFile) int {
order := 0
switch f.sort.Column {
case "name":
order = strings.Compare(a.name, b.name)
case "size":
order = a.size - b.size
}
if f.sort.Descending {
return -order
}
return order
})
components.Table(c, &f.table, columns, func() {
for _, file := range files {
if components.TableRow(c, file.name, file.name, f.selected == file.name, func(col int) {
switch col {
case 0:
components.Text(c, file.name).SingleLine()
case 1:
components.Text(c, file.kind).SingleLine().TextColor(c.Theme().TextMuted)
case 2:
components.Text(c, fmt.Sprintf("%d KB", file.size)).SingleLine()
}
}).Clicked() {
f.selected = file.name
}
}
}).Label("Files").Width(420).MaxWidthPercent(100)
}Columns are MyGo ui.TableColumn values with stable IDs. Clicking a sortable header sets the sort order, and the application sorts its rows. Dragging a header moves its column and dragging its edge resizes it; TableState.Columns keeps that layout, which the application may persist.
Large tables
components.TableVirtualized builds only the visible rows of a count, calling cell for each column. It scrolls sideways under its header when the columns are wider than the table.
Loading the live example…
import (
"fmt"
"slices"
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
)
type logTable struct {
table components.TableVirtualizedState
sort ui.SortOrder
rows []int // the order rows show in, re-sorted only when the user sorts
selected int
}
// logLatency is a stand-in for a row's data.
func logLatency(i int) int { return i*7919%977 + 1 }
func (l *logTable) View(c *ui.Context) {
if l.rows == nil {
l.rows = make([]int, 100000)
for i := range l.rows {
l.rows[i] = i
}
l.table.Sort = &l.sort
l.table.List.Selected = &l.selected
l.table.List.Key = func(row int) any { return l.rows[row] }
l.table.List.KeepOffset = true
l.table.OnSort = func() {
slices.SortStableFunc(l.rows, func(a, b int) int {
order := a - b
if l.sort.Column == "latency" {
order = logLatency(a) - logLatency(b)
}
if l.sort.Descending {
return -order
}
return order
})
}
}
columns := []ui.TableColumn{
{Title: "Request", ID: "request", Sortable: true, MinWidth: 160},
{Title: "Status", ID: "status", Width: 90},
{Title: "Latency", ID: "latency", Width: 100, Align: ui.End, Sortable: true},
}
components.TableVirtualized(c, &l.table, columns, len(l.rows), func(row, col int) {
i := l.rows[row]
switch col {
case 0:
components.Text(c, fmt.Sprintf("GET /api/items/%d", i+1)).SingleLine()
case 1:
components.Text(c, "200 OK").SingleLine().TextColor(c.Theme().Success)
case 2:
components.Text(c, fmt.Sprintf("%d ms", logLatency(i))).SingleLine()
}
}).Fill().Label("Requests")
}API
Table
func Table(c *ui.Context, state *TableState, columns []ui.TableColumn, rows func()) ui.ElementTable builds a non-virtualized table of TableRows under a header of columns. Applications own rows, selection and sorting; a nil state keeps the column layout on the table without sorting. The table is at least as wide as its columns; place it in a ScrollView when scrolling is needed. Up/Down and Home/End move focus between rows. Dragging a header moves its column and dragging its right edge resizes it, unless the column is Fixed.
pkg.go.devTableRow
func TableRow(c *ui.Context, key any, label string, selected bool, cell func(col int)) ui.ElementTableRow builds a row of a Table, cell building the content of each column by its index in the Table's columns. key identifies the row among its siblings and label names it for assistive technology. Clicked lets the application select or activate the row. Rows alternate with a faint stripe and selection uses the Tree's neutral tint, without a hover face. Call it only while building a Table's rows.
pkg.go.devTableState
TableState keeps the column layout and sort order of a Table or TableVirtualized. Keep one state per table and do not copy it after first use. Columns holds the order and widths chosen by dragging headers, keyed by column ID, and can be persisted by the application. Sort, when set, lets a click on a Sortable header choose the order; the application sorts its rows and OnSort reports the user's change.
Prop
Type
TableVirtualized
func TableVirtualized(c *ui.Context, state *TableVirtualizedState, columns []ui.TableColumn, count int, cell func(row, col int)) ui.ElementTableVirtualized builds Onevra table rows in MyGo's virtual scrolling container, under a header that scrolls sideways with them when the columns are wider than the table. Give it a height or fill a bounded parent. cell builds the content of a row's column, by its index in columns; it may run during layout and must not perform application work. Rows are at least as high as the header, grow to their tallest cell and alternate with a faint stripe, as in Table.
Clicks, Up/Down, Home/End and Page Up/Down choose rows; Enter and double clicks report List.OnSubmit. Header dragging, resizing and sorting follow Table. All row styling is local; the frame theme is untouched.
pkg.go.devTableVirtualizedState
TableVirtualizedState keeps a virtualized table's column layout, sort order, row selection and scrolling. Use one state per table and do not copy it after first use. List follows ListState's selection and activation contract: a nil List.Selected and List.Selection disables selection.
Prop
Type