Skip to content

Table

Table inherits tview.Table’s built-in navigation when SetSelectable(true, false) is active:

KeyAction
/ Move row selection
Home / EndFirst / last row
PgUp / PgDnPage up / down
EnterFire selection handler
Table preview

Table is an enhanced table wrapper with header support and selection.

Constructor func NewTable() *Table

Usage

table := components.NewTable()
table.SetHeaders("Name", "Status", "Created")
table.AddRow("Alice", "Active", "2024-01-15")
table.AddRow("Bob", "Inactive", "2024-01-10")
fmt.Println(table.RowCount())

AddRowWithStatus

table := components.NewTable()
table.SetHeaders("Name", "Status", "Created")
table.AddRow("Alice", "Active", "2024-01-15")
fmt.Println(table.RowCount())

Methods

Method Signature Description
AddColoredRow func (t *Table) AddColoredRow(cells []string, colors []tcell.Color) *Table AddColoredRow adds a row with explicit colors per cell.
AddRow func (t *Table) AddRow(cells ...string) *Table AddRow adds a data row with default foreground color.
AddRowWithColor func (t *Table) AddRowWithColor(color tcell.Color, cells ...string) int AddRowWithColor adds a row where all cells have the same color. Convenience method for adding styled rows with uniform color.
AddRowWithStatus func (t *Table) AddRowWithStatus(status *theme.Status, statusCol int, cells ...string) int AddRowWithStatus adds a row with a typed status for a specific column. The status column will display with the status color and icon. Other cells use the default foreground color. The status is stored as a reference for dynamic theme updates.
AddStyledRow func (t *Table) AddStyledRow(cells []TableCell) *Table AddStyledRow adds a row with full cell styling. If Status is set on a cell, its color takes precedence over the Color field.
ClearRows func (t *Table) ClearRows() *Table ClearRows removes all data rows (keeps headers).
ClearSelection func (t *Table) ClearSelection() ClearSelection deselects all rows.
ConfigureEmpty func (t *Table) ConfigureEmpty(icon, title, message string) *Table ConfigureEmpty sets the empty state shown when the table has no data rows.
DeselectByKey func (t *Table) DeselectByKey(key string) DeselectByKey deselects a row by its key.
GetCell func (t *Table) GetCell(row, col int) *tview.TableCell GetCell returns the cell at the specified table row and column. Note: row is the table row (including header), not data index.
GetDataRowCount func (t *Table) GetDataRowCount() int GetDataRowCount returns the number of data rows (excluding header).
GetRowByKey func (t *Table) GetRowByKey(key string) int GetRowByKey returns the current data index for a row key, or -1 if not found.
GetRowCells func (t *Table) GetRowCells(index int) []TableCell GetRowCells returns TableCell structs for a row (read-only snapshot). Index is 0-based for data rows (after header). Returns nil if index is out of bounds.
GetRowData func (t *Table) GetRowData(index int) []string GetRowData returns the text content of all cells in a row. Index is 0-based for data rows (after header). Returns nil if index is out of bounds.
GetRowKey func (t *Table) GetRowKey(index int) string GetRowKey returns the key for a data row index, or empty string if not set.
GetSelectedKeys func (t *Table) GetSelectedKeys() []string GetSelectedKeys returns keys of all selected rows.
GetSelectedRows func (t *Table) GetSelectedRows() []int GetSelectedRows returns indices of selected rows.
InsertColoredRowAt func (t *Table) InsertColoredRowAt(index int, cells []string, colors []tcell.Color) error InsertColoredRowAt inserts a row with explicit colors at the specified index.
InsertRowAt func (t *Table) InsertRowAt(index int, cells ...string) error InsertRowAt inserts a new row at the specified index. Existing rows at and after index are shifted down. Index is 0-based for data rows (after header).
IsKeySelected func (t *Table) IsKeySelected(key string) bool IsKeySelected checks if a row with the given key is selected.
IsRowSelected func (t *Table) IsRowSelected(row int) bool IsRowSelected checks if a row is selected.
RemoveRowAt func (t *Table) RemoveRowAt(index int) error RemoveRowAt removes the row at the specified index. Index is 0-based for data rows (after header).
RowCount func (t *Table) RowCount() int RowCount returns the number of data rows (excluding header). Alias for GetDataRowCount().
ScrollToRow func (t *Table) ScrollToRow(row int) ScrollToRow scrolls to make the specified row visible.
SelectAll func (t *Table) SelectAll() SelectAll selects all data rows.
SelectByKey func (t *Table) SelectByKey(key string) SelectByKey selects a row by its key.
SelectRow func (t *Table) SelectRow(index int) SelectRow selects a data row by index (0-based). This sets the table cursor to the specified row.
SelectedRow func (t *Table) SelectedRow() int SelectedRow returns the currently selected data row index (0-based). Returns -1 if no row is selected or only header is selected.
SetEmptyIcon func (t *Table) SetEmptyIcon(icon string) *Table SetEmptyIcon sets the icon for the empty state.
SetEmptyMessage func (t *Table) SetEmptyMessage(message string) *Table SetEmptyMessage sets the message for the empty state.
SetEmptyTitle func (t *Table) SetEmptyTitle(title string) *Table SetEmptyTitle sets the title for the empty state.
SetHeaders func (t *Table) SetHeaders(headers ...string) *Table SetHeaders sets column headers (row 0, fixed).
SetMultiSelect func (t *Table) SetMultiSelect(enabled bool) *Table SetMultiSelect enables/disables multi-selection.
SetOnSelect func (t *Table) SetOnSelect(fn func(row int)) *Table SetOnSelect sets callback for row selection (Enter key). The callback receives the 0-based data row index (excluding header).
SetOnSelectionChange func (t *Table) SetOnSelectionChange(fn func(rows []int)) *Table SetOnSelectionChange sets callback when multi-selection changes.
SetRowKey func (t *Table) SetRowKey(index int, key string) SetRowKey associates a unique key with a row for stable selection. Index is 0-based for data rows (after header).
SetSelectedFunc func (t *Table) SetSelectedFunc(fn func(row, col int)) *Table SetSelectedFunc sets the callback when a row is selected with Enter.
SetSelectionChangedFunc func (t *Table) SetSelectionChangedFunc(fn func(row, col int)) *Table SetSelectionChangedFunc sets the callback for when selection changes.
ToggleSelection func (t *Table) ToggleSelection() ToggleSelection toggles selection of current row.
ToggleSelectionByKey func (t *Table) ToggleSelectionByKey(key string) ToggleSelectionByKey toggles selection of a row by its key.
UpdateColoredRow func (t *Table) UpdateColoredRow(index int, cells []string, colors []tcell.Color) error UpdateColoredRow updates a row with explicit colors per cell. Index is 0-based for data rows (after header).
UpdateRow func (t *Table) UpdateRow(index int, cells ...string) error UpdateRow updates an existing row's cells in-place. Index is 0-based for data rows (after header). Returns error if index is out of bounds.
UpdateStyledRow func (t *Table) UpdateStyledRow(index int, cells []TableCell) error UpdateStyledRow updates a row with full cell styling. Index is 0-based for data rows (after header).

Types

type TableCell struct TableCell defines styling for a single cell.
FieldTypeDescription
Text string
Color tcell.Color Explicit color (0 = use Status color or default Fg)
Status *theme.Status Typed status for color/icon (takes precedence over Color if set)
Align int tview.AlignLeft, AlignCenter, AlignRight
Expansion int Column expansion factor
MaxWidth int Maximum width (0 = no limit)
Selectable bool Whether this cell is selectable