Corex.DataTable.Selection
(Corex v0.1.0-alpha.33)
View Source
Helpers for selectable .data_table usage in LiveViews.
Use in mount to assign initial selection state, and in handle_event("select", ...) / handle_event("select_all", ...) to update selection and sync checkboxes. Keeps the LiveView minimal.
Example
def mount(_params, _session, socket) do
socket =
socket
|> assign(:users, fetch_users())
|> Corex.DataTable.Selection.assign_for_selection(:users, table_id: "users-table", row_id: &"user-#{&1.id}")
{:ok, socket}
end
def handle_event("select", params, socket) do
{:noreply, Corex.DataTable.Selection.handle_select(socket, params, :users)}
end
def handle_event("select_all", params, socket) do
{:noreply, Corex.DataTable.Selection.handle_select_all(socket, params, :users)}
end
def render(assigns) do
~H"""
<.data_table
id="users-table"
rows={@users}
row_id={&"user-#{&1.id}"}
selectable={true}
selected={@selected}
on_select="select"
on_select_all="select_all"
>
<:col :let={user} label="ID">{user.id}</:col>
<:col :let={user} label="Name">{user.name}</:col>
</.data_table>
"""
end
Summary
Functions
Assigns selection state for the data table.
Handles the "select" event (single row checkbox) and returns the updated socket.
Handles the "select_all" event and returns the updated socket.
Functions
Assigns selection state for the data table.
Use in mount/3 after assigning the rows list. Options:
:table_id– required, the data_tableid(e.g."users-table"):row_id– required, function from row to string id (e.g.&"user-#{&1.id}")
Adds :selected (empty list), :selection_table_id, and :selection_row_id
for use by the handlers.
Handles the "select" event (single row checkbox) and returns the updated socket.
Use in handle_event("select", params, socket) and return
{:noreply, Corex.DataTable.Selection.handle_select(socket, params, :users)}.
params must contain "id" (checkbox DOM id) and "checked". rows_assign
is the assign key holding the list (e.g. :users).
Handles the "select_all" event and returns the updated socket.
Use in handle_event("select_all", params, socket) and return
{:noreply, Corex.DataTable.Selection.handle_select_all(socket, params, :users)}.
params must contain "checked". Syncs all row checkboxes via
Corex.Checkbox.set_checked. rows_assign is the assign key holding the list.