TermUI.Widgets.Table (TermUI v0.2.0)
View SourceTable widget for displaying tabular data.
Table provides efficient display of large datasets with virtual scrolling, column sorting, row selection, and flexible column layout.
Usage
Table.new(
columns: [
Column.new(:name, "Name"),
Column.new(:age, "Age", width: Constraint.length(10), align: :right)
],
data: [
%{name: "Alice", age: 30},
%{name: "Bob", age: 25}
],
on_select: fn selected -> IO.inspect(selected) end
)Features
- Virtual Scrolling: Efficiently handles 10,000+ rows
- Column Layout: Fixed, proportional, and percentage widths
- Selection: Single or multi-selection with keyboard/mouse
- Sorting: Click headers to sort ascending/descending
- Custom Rendering: Format cells with render functions
Selection Modes
:none- No selection allowed:single- One row at a time:multi- Multiple rows with Ctrl/Shift+click
Keyboard Navigation
- Arrow keys: Move selection
- Page Up/Down: Scroll by page
- Home/End: Jump to first/last row
- Enter: Confirm selection
- Space: Toggle selection (multi mode)
Summary
Functions
Clears the current selection.
Gets the current selection.
Creates a new Table widget.
Scrolls to a specific row index.
Sets the selection programmatically.
Sorts table by a column.
Toggles sort on a column.
Gets the total row count.
Gets the visible row count.
Types
Functions
Clears the current selection.
Gets the current selection.
Returns
List of selected row data.
Creates a new Table widget.
Options
:columns- List of Column specs (required):data- List of row maps (required):selection_mode- :none, :single, or :multi (default: :single):sortable- Enable sorting (default: true):on_select- Callback when selection changes:on_sort- Callback when sort changes:header_style- Style for header row:row_style- Style for data rows:selected_style- Style for selected rows:alternating- Alternating row backgrounds (default: false)
@spec scroll_to(map(), non_neg_integer()) :: map()
Scrolls to a specific row index.
@spec set_selection(map(), [non_neg_integer()]) :: map()
Sets the selection programmatically.
Parameters
state- Current table stateindices- List of row indices to select
Returns
Updated state with new selection.
@spec sort_by(map(), atom(), sort_direction()) :: map()
Sorts table by a column.
Parameters
state- Current table statecolumn_key- Column key to sort bydirection- :asc, :desc, or nil to clear
Returns
Updated state with sorted data.
Toggles sort on a column.
Cycles through: nil -> :asc -> :desc -> nil
@spec total_count(map()) :: non_neg_integer()
Gets the total row count.
@spec visible_count(map()) :: non_neg_integer()
Gets the visible row count.