datatable

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package datatable provides data table components with sorting, filtering, and pagination.

Available variants:

  • New() creates a data table (template: "lvt:datatable:default:v1")

Required lvt-* attributes: name, lvt-on:click, lvt-on:input

Example usage:

// In your controller/state
Users: datatable.New("users",
    datatable.WithColumns([]datatable.Column{
        {ID: "name", Label: "Name", Sortable: true},
        {ID: "email", Label: "Email", Sortable: true},
        {ID: "role", Label: "Role"},
    }),
    datatable.WithPageSize(10),
)

// In your template
{{template "lvt:datatable:default:v1" .Users}}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Templates

func Templates() *base.TemplateSet

Templates returns the datatable component's template set for registration with the LiveTemplate framework.

Example usage in main.go:

import "github.com/livetemplate/lvt/components/datatable"

tmpl, err := livetemplate.New("app",
    livetemplate.WithComponentTemplates(datatable.Templates()),
)

Available templates:

  • "lvt:datatable:default:v1" - Data table with sorting/pagination

Types

type Column

type Column struct {
	// ID is the column identifier (matches data field name)
	ID string
	// Label is the display header
	Label string
	// Sortable allows sorting by this column
	Sortable bool
	// Filterable allows filtering by this column
	Filterable bool
	// Width is optional column width (e.g., "100px", "20%")
	Width string
	// Align is text alignment ("left", "center", "right")
	Align string
	// Hidden hides the column
	Hidden bool
	// Format is a format hint (e.g., "date", "currency", "number")
	Format string
}

Column defines a table column.

type DataTable

type DataTable struct {
	base.Base

	// Columns defines the table columns
	Columns []Column

	// Rows is the data
	Rows []Row

	// SortColumn is the currently sorted column ID
	SortColumn string

	// SortDirection is the current sort direction
	SortDirection SortDirection

	// FilterValue is the current filter/search text
	FilterValue string

	// FilterColumn limits filtering to a specific column (empty for all)
	FilterColumn string

	// Page is the current page (0-indexed)
	Page int

	// PageSize is rows per page (0 for all)
	PageSize int

	// Selectable enables row selection
	Selectable bool

	// MultiSelect allows multiple row selection
	MultiSelect bool

	// SelectedIDs tracks selected row IDs
	SelectedIDs map[string]bool

	// Striped enables alternating row colors
	Striped bool

	// Hoverable enables row hover effect
	Hoverable bool

	// Bordered adds borders to cells
	Bordered bool

	// Compact reduces padding
	Compact bool

	// Loading indicates data is loading
	Loading bool

	// EmptyMessage is shown when no data
	EmptyMessage string
	// contains filtered or unexported fields
}

DataTable is a table component with sorting, filtering, and pagination. Use template "lvt:datatable:default:v1" to render.

NOTE: DataTable implements MarshalJSON to include computed fields in JSON output. If you embed *DataTable in your own struct, the promoted MarshalJSON will serialize only DataTable fields. Wrap the embedding struct with its own MarshalJSON if needed.

Client-side filtering is not yet implemented — FilterValue/FilterColumn are passed through for server-side use but do not affect TotalRows, TotalPages, or pagination counts. Modify Rows directly (via SetData) to reflect server-filtered results.

func New

func New(id string, opts ...Option) *DataTable

New creates a data table.

Example:

dt := datatable.New("users",
    datatable.WithColumns(columns),
    datatable.WithRows(rows),
    datatable.WithPageSize(10),
)

func (*DataTable) AllSelected

func (dt *DataTable) AllSelected() bool

AllSelected returns true if all rows are selected.

func (*DataTable) ClearFilter

func (dt *DataTable) ClearFilter()

ClearFilter clears the filter.

func (*DataTable) ClearSort

func (dt *DataTable) ClearSort()

ClearSort removes sorting.

func (*DataTable) DeselectAll

func (dt *DataTable) DeselectAll()

DeselectAll deselects all rows.

func (*DataTable) DeselectRow

func (dt *DataTable) DeselectRow(id string)

DeselectRow deselects a row by ID.

func (*DataTable) EndIndex

func (dt *DataTable) EndIndex() int

EndIndex returns the end index for current page.

func (*DataTable) FirstPage

func (dt *DataTable) FirstPage()

FirstPage goes to the first page.

func (*DataTable) GetColumn

func (dt *DataTable) GetColumn(id string) *Column

GetColumn returns a column by ID.

func (*DataTable) GetFilteredRows

func (dt *DataTable) GetFilteredRows() []Row

GetFilteredRows returns rows after filtering (cached). NOTE: Client-side filtering is not yet implemented. This method returns all rows regardless of FilterValue. Use SetData to provide pre-filtered rows from your server-side logic.

func (*DataTable) GetPageRows

func (dt *DataTable) GetPageRows() []Row

GetPageRows returns rows for the current page.

func (*DataTable) GetSelectedRows

func (dt *DataTable) GetSelectedRows() []Row

GetSelectedRows returns all selected rows.

func (*DataTable) GoToPage

func (dt *DataTable) GoToPage(page int)

GoToPage goes to a specific page.

func (*DataTable) HasNextPage

func (dt *DataTable) HasNextPage() bool

HasNextPage returns true if there's a next page.

func (*DataTable) HasPreviousPage

func (dt *DataTable) HasPreviousPage() bool

HasPreviousPage returns true if there's a previous page.

func (*DataTable) HasSelection

func (dt *DataTable) HasSelection() bool

HasSelection returns true if any row is selected.

func (*DataTable) HideColumn

func (dt *DataTable) HideColumn(id string)

HideColumn hides a column.

func (*DataTable) IsEmpty

func (dt *DataTable) IsEmpty() bool

IsEmpty returns true if there's no data.

func (*DataTable) IsRowSelected

func (dt *DataTable) IsRowSelected(id string) bool

IsRowSelected checks if a row is selected.

func (*DataTable) IsSortedAsc

func (dt *DataTable) IsSortedAsc(columnID string) bool

IsSortedAsc checks if sorted ascending by a column.

func (*DataTable) IsSortedBy

func (dt *DataTable) IsSortedBy(columnID string) bool

IsSortedBy checks if sorted by a column.

func (*DataTable) IsSortedDesc

func (dt *DataTable) IsSortedDesc(columnID string) bool

IsSortedDesc checks if sorted descending by a column.

func (*DataTable) LastPage

func (dt *DataTable) LastPage()

LastPage goes to the last page.

func (*DataTable) MarshalJSON

func (dt *DataTable) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler to include computed fields for RPC serialization. This ensures that methods like VisibleColumns(), GetPageRows() are available as JSON fields when the datatable is serialized and used in templates via RPC.

func (*DataTable) NextPage

func (dt *DataTable) NextPage()

NextPage goes to the next page.

func (*DataTable) PageInfo

func (dt *DataTable) PageInfo() string

PageInfo returns information about the current page.

func (*DataTable) PreviousPage

func (dt *DataTable) PreviousPage()

PreviousPage goes to the previous page.

func (*DataTable) SelectAll

func (dt *DataTable) SelectAll()

SelectAll selects all rows.

func (*DataTable) SelectRow

func (dt *DataTable) SelectRow(id string)

SelectRow selects a row by ID.

func (*DataTable) SelectedCount

func (dt *DataTable) SelectedCount() int

SelectedCount returns the number of selected rows.

func (*DataTable) SetData

func (dt *DataTable) SetData(rows []Row)

SetData replaces all rows.

func (*DataTable) SetFilter

func (dt *DataTable) SetFilter(value string)

SetFilter sets the filter value.

func (*DataTable) SetLoading

func (dt *DataTable) SetLoading(loading bool)

SetLoading sets the loading state.

func (*DataTable) ShowColumn

func (dt *DataTable) ShowColumn(id string)

ShowColumn unhides a column.

func (*DataTable) Sort

func (dt *DataTable) Sort(columnID string)

Sort sorts by a column. Toggles direction if same column.

func (*DataTable) StartIndex

func (dt *DataTable) StartIndex() int

StartIndex returns the 1-based start index for current page.

func (*DataTable) Styles

func (dt *DataTable) Styles() styles.DatatableStyles

Styles returns the resolved DatatableStyles for this component.

func (*DataTable) ToggleRowSelection

func (dt *DataTable) ToggleRowSelection(id string)

ToggleRowSelection toggles row selection.

func (*DataTable) TotalPages

func (dt *DataTable) TotalPages() int

TotalPages returns the total number of pages.

func (*DataTable) TotalRows

func (dt *DataTable) TotalRows() int

TotalRows returns the total number of rows (after filtering).

func (*DataTable) VisibleColumns

func (dt *DataTable) VisibleColumns() []Column

VisibleColumns returns non-hidden columns.

type Option

type Option func(*DataTable)

Option is a functional option for configuring data tables.

func WithBordered

func WithBordered(bordered bool) Option

WithBordered adds borders to cells.

func WithColumns

func WithColumns(columns []Column) Option

WithColumns sets the table columns.

func WithCompact

func WithCompact(compact bool) Option

WithCompact reduces padding.

func WithEmptyMessage

func WithEmptyMessage(message string) Option

WithEmptyMessage sets the message shown when no data.

func WithFilter

func WithFilter(value string) Option

WithFilter sets initial filter.

func WithHoverable

func WithHoverable(hoverable bool) Option

WithHoverable enables row hover effect.

func WithLoading

func WithLoading(loading bool) Option

WithLoading sets initial loading state.

func WithMultiSelect

func WithMultiSelect(multiSelect bool) Option

WithMultiSelect enables multiple row selection.

func WithPageSize

func WithPageSize(size int) Option

WithPageSize sets the number of rows per page.

func WithRows

func WithRows(rows []Row) Option

WithRows sets the table data.

func WithSelectable

func WithSelectable(selectable bool) Option

WithSelectable enables row selection.

func WithSort

func WithSort(columnID string, direction SortDirection) Option

WithSort sets initial sorting.

func WithStriped

func WithStriped(striped bool) Option

WithStriped enables alternating row colors.

func WithStyled

func WithStyled(styled bool) Option

WithStyled enables Tailwind CSS styling for the component.

type Row

type Row struct {
	// ID is the unique row identifier
	ID string
	// Data holds the row data (map of column ID to value)
	Data map[string]any
	// Selected indicates if row is selected
	Selected bool
	// Disabled prevents row selection
	Disabled bool
}

Row represents a table row with data and metadata.

func (Row) GetCellString

func (r Row) GetCellString(columnID string) string

GetCellString returns the string value for a row and column.

func (Row) GetCellValue

func (r Row) GetCellValue(columnID string) any

GetCellValue returns the value for a row and column.

type SortDirection

type SortDirection int

SortDirection indicates the sort order.

const (
	SortNone SortDirection = iota
	SortAsc
	SortDesc
)

Jump to

Keyboard shortcuts

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