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 ¶
- func Templates() *base.TemplateSet
- type Column
- type DataTable
- func (dt *DataTable) AllSelected() bool
- func (dt *DataTable) ClearFilter()
- func (dt *DataTable) ClearSort()
- func (dt *DataTable) DeselectAll()
- func (dt *DataTable) DeselectRow(id string)
- func (dt *DataTable) EndIndex() int
- func (dt *DataTable) FirstPage()
- func (dt *DataTable) GetColumn(id string) *Column
- func (dt *DataTable) GetFilteredRows() []Row
- func (dt *DataTable) GetPageRows() []Row
- func (dt *DataTable) GetSelectedRows() []Row
- func (dt *DataTable) GoToPage(page int)
- func (dt *DataTable) HasNextPage() bool
- func (dt *DataTable) HasPreviousPage() bool
- func (dt *DataTable) HasSelection() bool
- func (dt *DataTable) HideColumn(id string)
- func (dt *DataTable) IsEmpty() bool
- func (dt *DataTable) IsRowSelected(id string) bool
- func (dt *DataTable) IsSortedAsc(columnID string) bool
- func (dt *DataTable) IsSortedBy(columnID string) bool
- func (dt *DataTable) IsSortedDesc(columnID string) bool
- func (dt *DataTable) LastPage()
- func (dt *DataTable) MarshalJSON() ([]byte, error)
- func (dt *DataTable) NextPage()
- func (dt *DataTable) PageInfo() string
- func (dt *DataTable) PreviousPage()
- func (dt *DataTable) SelectAll()
- func (dt *DataTable) SelectRow(id string)
- func (dt *DataTable) SelectedCount() int
- func (dt *DataTable) SetData(rows []Row)
- func (dt *DataTable) SetFilter(value string)
- func (dt *DataTable) SetLoading(loading bool)
- func (dt *DataTable) ShowColumn(id string)
- func (dt *DataTable) Sort(columnID string)
- func (dt *DataTable) StartIndex() int
- func (dt *DataTable) Styles() styles.DatatableStyles
- func (dt *DataTable) ToggleRowSelection(id string)
- func (dt *DataTable) TotalPages() int
- func (dt *DataTable) TotalRows() int
- func (dt *DataTable) VisibleColumns() []Column
- type Option
- func WithBordered(bordered bool) Option
- func WithColumns(columns []Column) Option
- func WithCompact(compact bool) Option
- func WithEmptyMessage(message string) Option
- func WithFilter(value string) Option
- func WithHoverable(hoverable bool) Option
- func WithLoading(loading bool) Option
- func WithMultiSelect(multiSelect bool) Option
- func WithPageSize(size int) Option
- func WithRows(rows []Row) Option
- func WithSelectable(selectable bool) Option
- func WithSort(columnID string, direction SortDirection) Option
- func WithStriped(striped bool) Option
- func WithStyled(styled bool) Option
- type Row
- type SortDirection
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 ¶
New creates a data table.
Example:
dt := datatable.New("users",
datatable.WithColumns(columns),
datatable.WithRows(rows),
datatable.WithPageSize(10),
)
func (*DataTable) AllSelected ¶
AllSelected returns true if all rows are selected.
func (*DataTable) DeselectRow ¶
DeselectRow deselects a row by ID.
func (*DataTable) GetFilteredRows ¶
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 ¶
GetPageRows returns rows for the current page.
func (*DataTable) GetSelectedRows ¶
GetSelectedRows returns all selected rows.
func (*DataTable) HasNextPage ¶
HasNextPage returns true if there's a next page.
func (*DataTable) HasPreviousPage ¶
HasPreviousPage returns true if there's a previous page.
func (*DataTable) HasSelection ¶
HasSelection returns true if any row is selected.
func (*DataTable) HideColumn ¶
HideColumn hides a column.
func (*DataTable) IsRowSelected ¶
IsRowSelected checks if a row is selected.
func (*DataTable) IsSortedAsc ¶
IsSortedAsc checks if sorted ascending by a column.
func (*DataTable) IsSortedBy ¶
IsSortedBy checks if sorted by a column.
func (*DataTable) IsSortedDesc ¶
IsSortedDesc checks if sorted descending by a column.
func (*DataTable) MarshalJSON ¶
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) PreviousPage ¶
func (dt *DataTable) PreviousPage()
PreviousPage goes to the previous page.
func (*DataTable) SelectedCount ¶
SelectedCount returns the number of selected rows.
func (*DataTable) SetLoading ¶
SetLoading sets the loading state.
func (*DataTable) ShowColumn ¶
ShowColumn unhides a column.
func (*DataTable) StartIndex ¶
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 ¶
ToggleRowSelection toggles row selection.
func (*DataTable) TotalPages ¶
TotalPages returns the total number of pages.
func (*DataTable) VisibleColumns ¶
VisibleColumns returns non-hidden columns.
type Option ¶
type Option func(*DataTable)
Option is a functional option for configuring data tables.
func WithEmptyMessage ¶
WithEmptyMessage sets the message shown when no data.
func WithHoverable ¶
WithHoverable enables row hover effect.
func WithMultiSelect ¶
WithMultiSelect enables multiple row selection.
func WithPageSize ¶
WithPageSize sets the number of rows per page.
func WithSelectable ¶
WithSelectable enables row selection.
func WithSort ¶
func WithSort(columnID string, direction SortDirection) Option
WithSort sets initial sorting.
func WithStriped ¶
WithStriped enables alternating row colors.
func WithStyled ¶
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 ¶
GetCellString returns the string value for a row and column.
func (Row) GetCellValue ¶
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 )