LightningWeb.Live.Helpers.TableHelpers (Lightning v2.19.0-pre)

View Source

Utilities for simplifying common table operations like sorting and filtering.

This module provides reusable functions to reduce complexity in LiveView components that handle sortable, filterable tables.

Summary

Functions

Renders a filter input with magnifying glass icon and clear button.

Filters a list of items based on searchable fields and a filter term.

Creates a sort function based on direction string.

Resolves a client-supplied sort direction to :asc or :desc, defaulting to :asc for anything else.

Resolves a client-supplied sort field to one of allowed (a list of column atoms), returning default for anything unrecognised.

Sorts items by a given field and direction.

Toggles sort direction between "asc" and "desc".

Functions

filter_and_sort(items, filter, search_fields, sort_key, sort_direction, sort_map)

Combines filtering and sorting in one operation.

Examples

users = [%{name: "Bob", email: "bob@test.com"}, %{name: "Alice", email: "alice@test.com"}]
filter_and_sort(users, "test", [:email], "name", "asc", %{"name" => :name})

filter_input(assigns)

Renders a filter input with magnifying glass icon and clear button.

Attributes

  • filter - The current filter value
  • placeholder - Placeholder text for the input
  • target - Optional phx-target for the events
  • All other attributes are passed through to the input

Attributes

  • filter (:string) (required)
  • placeholder (:string) - Defaults to "Filter...".
  • target (:any) - Defaults to nil.
  • Global attributes are accepted. Supports all globals plus: ["class", "phx-keyup", "phx-debounce"].

filter_items(items, filter, search_fields)

Filters a list of items based on searchable fields and a filter term.

Examples

users = [%{name: "Alice", email: "alice@example.com"}]
search_fields = [:name, :email]
filter_items(users, "alice", search_fields)
# => [%{name: "Alice", email: "alice@example.com"}]

sort_compare_fn(arg)

Creates a sort function based on direction string.

Examples

iex> sort_compare_fn("asc")
&<=/2

iex> sort_compare_fn("desc")
&>=/2

sort_direction(value)

@spec sort_direction(String.t() | nil) :: :asc | :desc

Resolves a client-supplied sort direction to :asc or :desc, defaulting to :asc for anything else.

sort_field(value, allowed, default)

@spec sort_field(String.t() | nil, [atom(), ...], atom()) :: atom()

Resolves a client-supplied sort field to one of allowed (a list of column atoms), returning default for anything unrecognised.

Matches on each allowed atom's string form rather than calling String.to_atom/1 or String.to_existing_atom/1 on the input, so a crafted sort param can neither create atoms (atom-table exhaustion) nor crash on an unknown/non-column value; it just falls back to the default column.

sort_items(items, sort_key, sort_direction, sort_map)

Sorts items by a given field and direction.

Examples

users = [%{name: "Bob"}, %{name: "Alice"}]
sort_items(users, "name", "asc", %{"name" => :name})
# => [%{name: "Alice"}, %{name: "Bob"}]

toggle_sort_direction(current_direction, current_key, new_key)

Toggles sort direction between "asc" and "desc".

Examples

iex> toggle_sort_direction("asc", "name", "name")
{"name", "desc"}

iex> toggle_sort_direction("desc", "name", "name")
{"name", "asc"}

iex> toggle_sort_direction("asc", "name", "email")
{"email", "asc"}