Introducing a new way to review your PRs.

Read the announcement

petalframework / petal_components

Phoenix + Live View HEEX Components

View on GitHub

24 documents · Updated Sep 2, 2026

docs / data-tables

Data tables

The data table presents a collection of records as a browsable surface with shared state for search, filters, sorting, paging, selection, and column layout. It keeps the visible rows, the controls that describe them, and the navigation or event contract aligned so an application can use the same table with in-memory data or another query source.

Table model

The table has three related concerns: a state value describes the requested view, a data engine produces the rows for that view, and a rendering component exposes the result to the user. This separation lets the same state drive a URL-based table or an event-based table without changing the table's columns or user-facing behavior.

Query state

lib/petal_components/data_table/state.ex defines PetalComponents.DataTable.State, the shared query state for the table. It holds the sort order, filters, quick-search term, page, page size, and optional total count; total is a result supplied after the data source runs, while the other fields describe the requested view.

The state defines a common vocabulary for filter intent. Text filters support matching and equality, ordered values support comparisons and ranges, set filters support inclusion, and date filters express before, on, or after. Empty-value filters provide a separate way to find nil, empty strings, or empty lists. Filter and search text comparisons are case-insensitive, and an inclusive between range uses both bounds.

State changes reset the page when the current result position may no longer be valid. Sort changes cycle through ascending, descending, and no sort. URL and event input is checked against a field allow-list, and page size input is bounded before it becomes state.

References: lib/…/state.ex• L1-20 lib/…/state.ex• L22-80 lib/…/state.ex• L146-199 lib/…/state.ex• L201-251

List engine

lib/petal_components/data_table/engine/list.ex defines PetalComponents.DataTable.Engine.List, the zero-setup engine for lists of maps or structs that fit in memory. It applies the state in a fixed order: quick search, field filters, sorting, result counting, and page slicing. It returns the rows for the current page together with state that contains the post-filter total.

The list engine searches string fields and can receive an explicit search-field set when the default field choice is not suitable. It compares common numbers, decimals, dates, date-times, text, and scalar values without changing the table API. Rows with nil values sort last, while an empty sort keeps the source order.

The engine is suited to already-loaded data and examples. A larger application can consume the same state with its own query source, but that source must preserve the state contract so the rendered controls and returned rows describe the same view.

References: lib/…/list.ex• L1-25 lib/…/list.ex• L35-70 lib/…/list.ex• L72-169

Rendering surface

lib/petal_components/data_table.ex defines PetalComponents.DataTable, the full presentation surface around the base table. It combines column rendering with a toolbar, table body, empty or loading state, range summary, and pagination.

Column definitions

Each required :col slot names a record field and receives the full row for custom cell content. A column may be sortable or filterable, may set a label and alignment, and may provide select options when its filter type is select.

The table can add a trailing :action slot for row actions. A :toolbar slot adds application controls above the table, while an :empty slot replaces the default empty message. A :bulk_action slot receives selected row identifiers when selection is active. Column filters use the shared PetalComponents.DataTable.FilterEditor, so the data table and the standalone filter bar use the same value shapes and display rules.

The underlying lib/petal_components/table.ex component remains the simple table primitive. It supplies headers, cells, optional row actions, sortable header semantics, density, stripes, sticky headers, and footer content; the data table supplies the stateful browsing layer around it.

References: lib/…/data_table.ex• L58-64 lib/…/data_table.ex• L219-241 lib/…/data_table.ex• L499-589 lib/…/filter_editor.ex• L1-25 lib/…/table.ex• L6-71 lib/…/table.ex• L93-153

Browsing controls

The toolbar can expose quick search, per-column filters, custom tools, and a column-visibility menu. The footer reports the visible range and chooses numbered pagination when the total is known; it uses a simple next-and-previous form when the total is unknown.

Filter editors match the declared column type. Text, number, and date columns use an operator and value; number and date filters can use two bounds; select filters use a list of choices and an inclusion operation. The editor also supports value-free empty checks. Lists with eight or more choices get a browser-only option search, while checked choices stay visible as the list narrows. An active filter is shown on its column control and can be cleared. The default empty state distinguishes no records from no records that match the active search or filters.

The loading state replaces record content with skeleton rows while retaining the table shape. A capped body region gives a sticky header a scroll area in which it can remain visible.

References: lib/…/data_table.ex• L100-140 lib/…/data_table.ex• L353-495 lib/…/data_table.ex• L543-588 lib/…/data_table.ex• L592-637 lib/…/filter_editor.ex• L31-33 lib/…/filter_editor.ex• L77-228 lib/…/filter_editor.ex• L257-311 assets/…/petal_components.js• L7020-7042 lib/…/data_table.ex• L1020-1120

Selection and column layout

Selection is presentation state rather than query state. With selectable, the table adds row checkboxes and a tri-state header checkbox; the toolbar then changes to show the selection count, bulk actions, and a clear action. The row identity must be unique across the full collection, not only the visible page, so a selection remains meaningful while the user pages through results.

Column visibility and order are also presentation state. column_toggle keeps all declared columns in the menu while hiding selected columns from the table, and reorderable adds movement controls. The table always keeps at least one visible column. These settings belong in application assigns or another UI-state store, not in the query URL.

References: lib/…/data_table.ex• L33-44 lib/…/data_table.ex• L141-207 lib/…/data_table.ex• L335-351 lib/…/data_table.ex• L397-469 lib/…/data_table.ex• L746-859

Interaction modes

The table exposes two ways to carry user changes back to the application. Both modes use the same state and the same operation meaning; they differ in whether the browser changes the URL or sends a LiveView event.

Link mode uses path as the navigation base. Sorts, pages, search, page size, and filters become URL parameters, so a table view can be shared, restored with the browser back button, and loaded again through handle_params and State.from_params/2.

The server builds URL templates for controls whose values can change in the browser. The table's PetalDataTable hook fills those templates from the current search and page-size controls, then performs a LiveView patch. This keeps an uncommitted search term and a new page-size choice together in one navigation.

Event mode

Event mode uses on_change as the single event for query operations. The event payload identifies an operation such as sort, page, search, page-size change, filter edit, or filter clear; State.handle_op/3 turns that payload into the next query state.

Selection, column visibility, and column movement use UI operations rather than query operations. They can use on_ui when the application wants a separate event, or fall back to on_change; the application must update those assigns itself because query-state handling does not consume them.

References: lib/…/data_table.ex• L7-47 lib/…/data_table.ex• L242-317 lib/…/data_table.ex• L644-711 lib/…/data_table.ex• L1136-1141 lib/…/state.ex• L253-305 rules.md• L231-279

Browser behavior

The browser layer in assets/…/petal_components.js adds only the behavior that server-rendered markup cannot provide. It handles link-mode control wiring, disclosure menus, DOM-only checkbox state, and focus continuity while LiveView updates the table.

Filter and column panels are disclosures tied to their nearby triggers. The hook opens one panel at a time, keeps the panel attached to the page while the page scrolls, and recalculates its fit on resize. It can flip a panel above its trigger, keep it within the visible viewport, and limit its height for scrolling.

An outside tap closes a panel, while a drag, cancelled touch gesture, or multi-finger gesture does not. Escape closes the table panel without also closing an enclosing modal, and focus returns to the trigger after a menu action. The hook restores an open panel after a LiveView patch and moves focus to an enabled sibling if the prior control became unavailable.

References: assets/…/petal_components.js• L6753-6974 assets/…/petal_components.js• L6982-7017 test/…/data_table.test.js• L254-506

LiveView synchronization

The server stamps the header checkbox's indeterminate state into markup, but the browser must copy that value to the checkbox DOM property. The hook performs this sync after mount and after each patch. It also reapplies the client-only narrowing of long select-option lists after a patch and keeps checked options visible while the user narrows the list.

In link mode, quick search waits for the configured debounce period, while page-size changes navigate at once. Filter submission updates only the chosen filter and removes blank or incomplete values; empty-value operators remain meaningful even without a value. Event-mode filter forms keep their normal LiveView submit path; the hook closes the panel without replacing that event handling. Both modes use the same editor field names for single values, two-bound ranges, and selected value lists.

References: assets/…/petal_components.js• L6687-6751 assets/…/petal_components.js• L6982-7050 assets/…/petal_components.js• L7071-7175 lib/…/filter_editor.ex• L318-360 test/…/data_table.test.js• L104-252 test/…/data_table.test.js• L509-629

Integration flow

An application normally keeps the current state and the current page rows together. The data source receives the state, returns rows and any known total, and the data table renders that result with the same state.

The following flow shows how a user action becomes a new table view in either URL or event mode.

flowchart LR
    A[User changes search, filter, sort, or page] --> B{Wiring mode}
    B -->|Link| C[LiveView patch parameters]
    B -->|Event| D[LiveView operation payload]
    C --> E[Build State]
    D --> E
    E --> F[Run list engine or application query]
    F --> G[Rows and result count]
    G --> H[Render table controls and records]

For URL-driven tables, the allow-list passed to State.from_params/2 is the boundary between user input and supported fields. For event-driven tables, the same field allow-list belongs in State.handle_op/3. In both cases, the query source and the column declarations must agree on field names.

References: lib/…/data_table.ex• L7-31 lib/…/state.ex• L146-179 lib/…/state.ex• L253-305 lib/…/list.ex• L35-70 lib/…/data_table.ex• L24-41

Change checklist

When these files or sections are changed, remember to consider these:

  • When a column field or sort/filter capability changes, update the state field allow-list and the query source together. URL and event input reject fields that the allow-list does not recognize.
  • When a filter value shape changes, keep the shared filter editor, URL encoding, parameter decoding, event handling, and query engine aligned. This is important for select lists and two-bound ranges.
  • When selection identity changes, use the same row-key function for row checkboxes, page-wide selection, and the application bulk action. The key must remain unique across pages.
  • When a link-mode control or URL parameter changes, update the server URL template and the PetalDataTable browser wiring as one contract.
  • When menu markup or checkbox stamps change, update the hook's focus, disclosure, option-filter, or indeterminate synchronization rules with the rendered anatomy.

Important failure modes

TriggerConsequenceGuard
A row identity is not unique across the collectionSelection can apply to the wrong record or fail to persist across pagesUse a stable unique row_id, normally a record identifier, and use the same key for bulk operations
A sortable or filterable field is missing from the state allow-listThe control renders but URL or event input leaves state unchangedKeep declared column fields and the fields option passed to state parsing aligned
A list engine runs a page without first applying search and filtersThe range count and visible rows describe different result setsTreat the returned state and rows as one result from the same query pipeline
A filter editor and decoder disagree on list or range shapeA filter disappears, matches nothing, or cannot be restored from a URLPreserve the shared shapes for values[] and two-bound between filters
A capped sticky table omits the scroll regionThe header has no table body region in which to remain pinnedProvide max_height when using sticky_header inside the data table
The data-table hook is not registered in the LiveSocket hook setLink-mode search and page-size controls do not navigate, and menus or indeterminate checkboxes do not workRegister the bundled PetalDataTable hook set in the consuming LiveView application

Key files

FileWhy to read it
lib/petal_components/data_table/state.exDefines the shared query state, filter vocabulary, URL contract, and event operation contract
lib/petal_components/data_table/engine/list.exShows the end-to-end search, filter, sort, count, and page behavior for in-memory records
lib/petal_components/data_table.exDefines the public data-table surface, interaction modes, slots, loading states, selection, and column controls
lib/petal_components/data_table/filter_editor.exDefines the shared typed filter editors, filter display text, and link-mode filter encoding
lib/petal_components/table.exDefines the lower-level table structure and sortable header behavior used by the data table
assets/…/petal_components.jsDefines browser behavior for link navigation, menus, focus, option lists, and checkbox state
lib/petal_components/showcase/data_table.exGives compact examples of the basic, toolbar, selection, column, loading, and empty table surfaces
test/petal/data_table_test.exsDescribes the server-rendered contracts for stateful controls, accessibility output, and edge conditions
test/…/data_table.test.jsDescribes browser guarantees for navigation timing, menu gestures, focus, and LiveView patch behavior
Data presentation and browsingPlaces data tables beside the library's other data browsing surfaces
Charts, paging, and progressLinks to related pagination and loading behavior
Hierarchical data and manipulationLinks to related collection views that support nested records and manipulation