table

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package table is a library of widgets used to render a table. In addition to the core table, row and column widgets, it also includes the sorter, paginator, page sizer and quick search widgets.

Index

Constants

This section is empty.

Variables

View Source
var Any = factory.Any

Function aliases

View Source
var Bytes = factory.Bytes
View Source
var HTML = factory.HTML
View Source
var HTMLUnsafe = factory.HTMLUnsafe
View Source
var Many = factory.Many
View Source
var Tag = factory.Tag
View Source
var Text = factory.Text

Functions

This section is empty.

Types

type BytesWidget

type BytesWidget = widget.BytesWidget

type ColumnWidget

type ColumnWidget struct {
	*widget.WidgetBase[*ColumnWidget]
	// contains filtered or unexported fields
}

ColumnWidget renders a table column header. Columns must be nested under a table.

func (*ColumnWidget) Add

func (wgt *ColumnWidget) Add(children ...any) *ColumnWidget

Add adds nested widgets.

func (*ColumnWidget) Children

func (wgt *ColumnWidget) Children() []Widget

Children are the widgets nested under this widget.

func (*ColumnWidget) Draw

func (wgt *ColumnWidget) Draw(w io.Writer, r *http.Request) (err error)

Draw renders the widget's HTML.

func (*ColumnWidget) WithAlignment

func (wgt *ColumnWidget) WithAlignment(alignment string) *ColumnWidget

WithAlignment sets the text alignment of the column. Valid values are "left", "right", "center" and the empty string "" for the default behavior.

func (*ColumnWidget) WithVisibility

func (wgt *ColumnWidget) WithVisibility(nwx string) *ColumnWidget

WithVisibility sets the visibility of the column based on the total width available to the table. The specification is a string that contains a letter for each of the following 3 cases: "n" for narrow (under 600px); "w" for wide (600-1199px); and "x" for expanded (1200px or more). By default a column is visible in all 3 situations, i.e. "nwx".

In narrow spaces it typically makes sense to hide certain columns outright; merge two columns into a third by hiding the former and showing the latter; relocate the action menu from the right-most column to the left-most if there's a chance of it going off screen.

In expanded spaces it typically makes sense to reveal less-important contextual columns.

func (*ColumnWidget) WithWidth

func (wgt *ColumnWidget) WithWidth(width int) *ColumnWidget

WithWidth sets the width of the column in relation to the total widths of all the columns. The default width is 100, so for example, setting a width of 200 sets the column to be twice as wide.

An alternative approach that yields good results is setting the width of the columns to the expected number of characters of their content. Width of all columns must be explicitly set for this approach to work correctly.

type InputWidget

type InputWidget = widget.InputWidget

type PageSizerWidget

type PageSizerWidget struct {
	*widget.WidgetBase[*PageSizerWidget]
	// contains filtered or unexported fields
}

PageSizerWidget renders a table page sizer.

func (*PageSizerWidget) Draw

func (wgt *PageSizerWidget) Draw(w io.Writer, r *http.Request) (err error)

Draw renders the widget's HTML.

func (*PageSizerWidget) Drawn

func (wgt *PageSizerWidget) Drawn(r *http.Request) bool

func (*PageSizerWidget) ForTable

func (wgt *PageSizerWidget) ForTable(name string) *PageSizerWidget

ForTable binds this page sizer to the named table. Defaults to "table". Set this when the page has multiple tables.

func (*PageSizerWidget) WithOptions

func (wgt *PageSizerWidget) WithOptions(rowsPerPage ...int) *PageSizerWidget

WithOptions overrides the page-size choices offered in the dropdown. Default is 10, 25, 50, 100. The table's current default page size is always added to the list (and sorted), so the user never sees a missing "currently selected" option.

type PaginatorWidget

type PaginatorWidget struct {
	*widget.WidgetBase[*PaginatorWidget]
	// contains filtered or unexported fields
}

PaginatorWidget renders a paginator.

func (*PaginatorWidget) Draw

func (wgt *PaginatorWidget) Draw(w io.Writer, r *http.Request) (err error)

Draw renders the widget's HTML.

func (*PaginatorWidget) Drawn

func (wgt *PaginatorWidget) Drawn(r *http.Request) bool

Drawn indicates whether this widget needs to be drawn.

func (*PaginatorWidget) ForTable

func (wgt *PaginatorWidget) ForTable(name string) *PaginatorWidget

ForTable binds this paginator to the named table. Defaults to "table". Set this when the page has multiple tables.

type QuickSearchUnderlinerWidget

type QuickSearchUnderlinerWidget struct {
	*widget.WidgetBase[*QuickSearchUnderlinerWidget]
	// contains filtered or unexported fields
}

QuickSearchUnderlinerWidget highlights its content based on the search box associated with a table.

func (*QuickSearchUnderlinerWidget) Draw

func (wgt *QuickSearchUnderlinerWidget) Draw(w io.Writer, r *http.Request) (err error)

Draw renders the widget's HTML.

func (*QuickSearchUnderlinerWidget) ForTable

ForTable binds this underliner to the named table. Defaults to "table". Set this when the page has multiple tables.

func (*QuickSearchUnderlinerWidget) WithPrefixOnly

func (wgt *QuickSearchUnderlinerWidget) WithPrefixOnly(prefix bool) *QuickSearchUnderlinerWidget

WithPrefixOnly restricts matching to word prefixes. When false (default), search terms match anywhere within a word.

type QuickSearchWidget

type QuickSearchWidget struct {
	*widget.WidgetBase[*QuickSearchWidget]
	// contains filtered or unexported fields
}

QuickSearchWidget renders a quick search input box for a table.

func (*QuickSearchWidget) Draw

func (wgt *QuickSearchWidget) Draw(w io.Writer, r *http.Request) (err error)

Draw renders the widget's HTML.

func (*QuickSearchWidget) ForTable

func (wgt *QuickSearchWidget) ForTable(name string) *QuickSearchWidget

ForTable binds this quick search to the named table. Defaults to "table". Set this when the page has multiple tables.

func (*QuickSearchWidget) WithLength

func (wgt *QuickSearchWidget) WithLength(minChars int, maxChars int) *QuickSearchWidget

WithLength sets the min and max lengths, in characters, allowed by the field. A negative value indicates unbound. A field with a minimum length greater than 0 is automatically assumed as required.

func (*QuickSearchWidget) WithWidth

func (wgt *QuickSearchWidget) WithWidth(css string) *QuickSearchWidget

WithWidth sets the visual width of the field. Pass any CSS length, e.g. "16ch", "200px" or "100%". By default, the field stretches to the full available width (100%).

type RowWidget

type RowWidget struct {
	*widget.WidgetBase[*RowWidget]
	// contains filtered or unexported fields
}

RowWidget renders a table row. Rows must be nested under a table.

func (*RowWidget) Add

func (wgt *RowWidget) Add(cells ...any) *RowWidget

Add adds nested widgets.

func (*RowWidget) Children

func (wgt *RowWidget) Children() []Widget

Children are the widgets nested under this widget.

func (*RowWidget) Draw

func (wgt *RowWidget) Draw(w io.Writer, r *http.Request) (err error)

Draw renders the widget's HTML.

func (*RowWidget) WithAction

func (wgt *RowWidget) WithAction(href string) *RowWidget

WithAction makes the row clickable, navigating to href. Accepts the full action-URL grammar (`?key=`, `^?…`, `/path`, etc.).

func (*RowWidget) WithTarget

func (wgt *RowWidget) WithTarget(target string) *RowWidget

WithTarget sets the HTML target for the row's action. Defaults to the page's `_target` state variable when unset.

func (*RowWidget) WithTextColorDisabled

func (wgt *RowWidget) WithTextColorDisabled() *RowWidget

WithTextColorDisabled greys out the row's text to signal it's inactive. The row itself stays interactive — combine with WithAction("") to also drop the click target.

func (*RowWidget) WithVerticalAlign

func (wgt *RowWidget) WithVerticalAlign(verticalAlign string) *RowWidget

WithVerticalAlign sets the vertical alignment of the row to "middle" (default), "top" or "bottom".

type SorterWidget

type SorterWidget struct {
	*widget.WidgetBase[*SorterWidget]
	// contains filtered or unexported fields
}

SorterWidget renders a sorter.

func (*SorterWidget) Add

func (wgt *SorterWidget) Add(children ...any) *SorterWidget

Add adds nested widgets.

func (*SorterWidget) Children

func (wgt *SorterWidget) Children() []Widget

Children are the widgets nested under this widget.

func (*SorterWidget) Draw

func (wgt *SorterWidget) Draw(w io.Writer, r *http.Request) (err error)

Draw renders the widget's HTML.

func (*SorterWidget) Drawn

func (wb *SorterWidget) Drawn(r *http.Request) bool

Drawn indicates whether this widget needs to be drawn.

func (*SorterWidget) ForTable

func (wgt *SorterWidget) ForTable(name string) *SorterWidget

ForTable binds this sorter to the named table. Defaults to "table". Set this when the page has multiple tables.

type TableFactory

type TableFactory struct{}

TableFactory aggregates the widget constructors of this package. Use TableFactory{} to construct a new factory

func (TableFactory) Col

func (f TableFactory) Col(visibility string, width int, alignment string) *ColumnWidget

Col is a shorthand factory equivalent to Column().WithAlignment(alignment).WithWidth(width).WithVisibility(visibility). Use it when you want a one-line column declaration.

func (TableFactory) Column

func (f TableFactory) Column() *ColumnWidget

Column creates a new widget for a single column header. Add it to a Table via Table.Add; the header cell's content is populated via Column.Add. Defaults: visible at all viewport widths, width 100 (an arbitrary unit weighted against other columns' widths).

func (TableFactory) Grid

func (f TableFactory) Grid() *TableWidget

Grid creates a new widget that renders a borderless, header-less table — suitable for laying out tiles or summary rows where headings would add noise.

func (TableFactory) PageSizer

func (f TableFactory) PageSizer() *PageSizerWidget

PageSizer creates a new widget that renders a dropdown letting the user pick how many rows the table shows per page. Defaults to options 10/25/50/100; override with WithOptions. The widget hides itself when the total row count fits in the smallest option. Bind to a non-default table with ForTable.

func (TableFactory) Paginator

func (f TableFactory) Paginator() *PaginatorWidget

Paginator creates a new widget that renders page-number flippers for a table. The paginator hides itself when there's only one page; when the total row count is unknown (see Table.WithTotalRows) it falls back to a "Next…" affordance. Bind to a non-default table with ForTable.

func (TableFactory) QuickSearch

func (f TableFactory) QuickSearch() *QuickSearchWidget

QuickSearch creates a new widget that renders an auto-submitting search input. Typing into it sets `<table>_q` in state; read it back via Table.Query when fetching rows. Bind to a non-default table with ForTable.

func (TableFactory) QuickSearchUnderliner

func (f TableFactory) QuickSearchUnderliner(content string) *QuickSearchUnderlinerWidget

QuickSearchUnderliner creates a new widget that renders content with any matches of the paired QuickSearch query underlined. Use it inside row cells to highlight the matched substring. Bind to a non-default table with ForTable.

func (TableFactory) Row

func (f TableFactory) Row() *RowWidget

Row creates a new widget for one table row. Add cells in declared column order via Row.Add; missing cells are rendered as empty. Add the row to a Table via Table.Add.

func (TableFactory) Sorter

func (f TableFactory) Sorter(sortKey string, label any) *SorterWidget

Sorter creates a new widget that renders a clickable sort trigger, typically placed inside a Column. sortKey is the identifier passed to your store (via Table.SortOrder); label is the visible text. Clicking cycles ascending → descending → off, updating `<table>_sort` in state. Bind to a non-default table with ForTable.

func (TableFactory) Table

func (f TableFactory) Table() *TableWidget

Table creates a new widget that renders a data table with header, sort, pagination, and filter hooks. Add Column and Row children, then pair with the companion widgets (Sorter, Paginator, PageSizer, QuickSearch) — they bind to this table by name (default "table").

type TableWidget

type TableWidget struct {
	*widget.WidgetBase[*TableWidget]
	// contains filtered or unexported fields
}

TableWidget renders a table.

func (*TableWidget) Add

func (wgt *TableWidget) Add(children ...any) *TableWidget

Add adds nested widgets to the table. Column widgets are added as column headers. Row widgets are added as rows. Remaining elements are composed into a single row.

Example:

// Add columns
tbl.Add(
	wgt.Column(3, "Name"),
	wgt.Column(1, "DOB"),
)
// Add a row
tbl.Add(
	wgt.Row("Albert Einstein", "3/14/1879"),
)
// Add a row
tbl.Add(
	"Steven Hawking", "1/8/1942",
)

func (*TableWidget) Children

func (wgt *TableWidget) Children() []Widget

Children are the widgets nested under this widget.

func (*TableWidget) DisplayRange

func (wgt *TableWidget) DisplayRange(r *http.Request) (fromRow int, toRow int)

DisplayRange returns the half-open row range [fromRow, toRow) the current page should show. Feed it directly into your data store query. When the total is unknown, toRow may extend past the real row count — just take whatever you get back. Changes to query/sort/page-size automatically reset the page to 1.

func (*TableWidget) Draw

func (wgt *TableWidget) Draw(w io.Writer, r *http.Request) (err error)

Draw renders the widget's HTML.

func (*TableWidget) Drawn

func (wgt *TableWidget) Drawn(r *http.Request) bool

Drawn indicates whether this widget needs to be drawn.

func (*TableWidget) Query

func (wgt *TableWidget) Query(r *http.Request) string

Query returns the current quick-search text entered through the paired QuickSearch widget (or empty if none). Apply it as a filter when fetching rows from your data source.

func (*TableWidget) SortOrder

func (wgt *TableWidget) SortOrder(r *http.Request) string

SortOrder returns the currently active sort key, falling back to the default set via WithDefaultSortOrder. A leading "-" indicates descending. Empty means unsorted. Pass this into your store query.

func (*TableWidget) WithBorder

func (wgt *TableWidget) WithBorder(border bool) *TableWidget

WithBorder controls whether cell borders are drawn. Default is true (borders shown).

func (*TableWidget) WithDefaultPageRows

func (wgt *TableWidget) WithDefaultPageRows(r *http.Request, numRowsPerPage int) *TableWidget

WithDefaultPageRows sets the initial page size. Without a paired PageSizer this becomes the fixed page size. Default if never set is 25. Non-positive values are ignored.

func (*TableWidget) WithDefaultSortOrder

func (wgt *TableWidget) WithDefaultSortOrder(r *http.Request, sortOrder string) *TableWidget

WithDefaultSortOrder seeds the initial sort order used when the user hasn't clicked a sorter yet. The value is a sort key, optionally prefixed with "-" for descending. Empty means no default order. Call this each request — it writes to the table's reserved default state variable.

func (*TableWidget) WithEmptyMessage

func (wgt *TableWidget) WithEmptyMessage(emptyMsg string) *TableWidget

WithEmptyMessage sets the message rendered as a placeholder row when the table has no data rows. Default is "The table is empty".

func (*TableWidget) WithHeader

func (wgt *TableWidget) WithHeader(header bool) *TableWidget

WithHeader controls whether the column-header row is rendered. Default is true.

func (*TableWidget) WithName

func (wgt *TableWidget) WithName(name string) *TableWidget

WithName sets the table's name. Companion widgets (Sorter, Paginator, PageSizer, QuickSearch, QuickSearchUnderliner) bind to the table by this name via ForTable, and the state variables they read/write are scoped under it (`<name>_sort`, `<name>_page`, `<name>_rows`, `<name>_q`). Use distinct names when multiple tables share a page. Default is "table".

func (*TableWidget) WithTotalRows

func (wgt *TableWidget) WithTotalRows(r *http.Request, totalNumRows int) *TableWidget

WithTotalRows tells the table how many rows the underlying data source has in total — this drives the paginator's page count and the empty-state. Pass a negative value when the total is unknown (the paginator falls back to a "Next…" affordance). Call this after querying your store and before Draw.

func (*TableWidget) WithVerticalAlign

func (wgt *TableWidget) WithVerticalAlign(verticalAlign string) *TableWidget

WithVerticalAlign sets the vertical alignment of cells within rows. Accepts "middle" (default), "top", or "bottom"; other values are ignored. Override per row with Row.WithVerticalAlign.

type Widget

type Widget = widget.Widget

Type aliases

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL