08 · Tabelas

DataTable

variant advanced · grid · seleção + barra de ações · ordenação · pager rounded/pill/circle/canvas

Tabela completa com busca, filtros, seleção em massa, ordenação e paginação.

Marcando linhas: a barra de ações sobe

Pedido Cliente Status Total Hora
PED-2491 Mercadinho Bom Preço Pago R$ 1.284,90 14:02
PED-2492 Padaria Central Aberto R$ 312,00 14:40
PED-2493 Farmácia Vida Atrasado R$ 2.940,10 ontem
PED-2494 Açougue Serrinha Pago R$ 860,00 ontem
3 selecionados
.heex
<% rows = [%{no: "PED-2491", cl: "Mercadinho Bom Preço", tone: "pos", st: "Pago", tot: "R$ 1.284,90", h: "14:02", dl: "0.00"}, %{no: "PED-2492", cl: "Padaria Central", tone: "", st: "Aberto", tot: "R$ 312,00", h: "14:40", dl: "0.35"}, %{no: "PED-2493", cl: "Farmácia Vida", tone: "neg", st: "Atrasado", tot: "R$ 2.940,10", h: "ontem", dl: "0.70"}, %{no: "PED-2494", cl: "Açougue Serrinha", tone: "pos", st: "Pago", tot: "R$ 860,00", h: "ontem", dl: "1.05"}] %>
<.data_table
  id="dt-bulk"
  rows={rows}
  style="position:relative;overflow:hidden"
  row_key={& &1.no}
  selected={[]}
  all_selected={false}
  on_select="select"
  on_select_all="select_all"
  select_all_label="Todos"
  select_label="Selecionar"
  row_attrs={&%{class: "animate-[mo-rowsel_6s_ease-in-out_var(--dl,0s)_infinite]", style: "--dl:#{&1.dl}s"}}
  select_attrs={&%{class: "[&_input]:animate-[lu-cbbox_6s_ease-in-out_var(--dl,0s)_infinite]", style: "--dl:#{&1.dl}s"}}
>
  <:toolbar>
    <.table_search id="dt-search" placeholder="Buscar pedido" width="220px" />
    <.button canvas size="sm"><.icon name="filter" size="md" />Filtrar</.button>
    <span class="min-w-0 flex-1"></span>
    <.button canvas size="sm"><.icon name="download" size="md" />Exportar</.button>
  </:toolbar>
  <:col :let={o} label="Pedido" class="font-mono text-[13px]">{o.no}</:col>
  <:col :let={o} label="Cliente">{o.cl}</:col>
  <:col :let={o} label="Status"><.badge canvas dot color={Map.get(%{"pos" => "success", "neg" => "error"}, o.tone, "neutral")}>{o.st}</.badge></:col>
  <:col :let={o} label="Total" align="right" class="font-medium text-ink">{o.tot}</:col>
  <:col :let={o} label="Hora" align="right">{o.h}</:col>
  <:bulk>
    <div style="position:absolute;left:0;right:0;bottom:14px;display:flex;justify-content:center">
      <.bulk_bar id="dt-bulk-bar" count={3} label="selecionado" label_plural="selecionados" floating={false} class="animate-[mo-sel_6s_cubic-bezier(.2,.8,.2,1)_infinite]">
        <:action icon="file">Emitir NF-e</:action>
        <:action icon="download">Exportar</:action>
        <:action variant="danger" icon="trash">Excluir</:action>
      </.bulk_bar>
    </div>
    <div style="height:58px"></div>
  </:bulk>
</.data_table>

variant advanced × grid · pager

advanced
Pedido Cliente Status Total Hora
PED-2491 Mercadinho Bom Preço Pago R$ 1.284,90 14:02
PED-2492 Padaria Central Aberto R$ 312,00 14:40
PED-2493 Farmácia Vida Atrasado R$ 2.940,10 ontem
1–25 de 1.096
1 2 3 … 44
grid
Pedido Cliente Status Total Hora
PED-2491 Mercadinho Bom Preço Pago R$ 1.284,90 14:02
PED-2492 Padaria Central Aberto R$ 312,00 14:40
PED-2493 Farmácia Vida Atrasado R$ 2.940,10 ontem
1–25 de 1.096
1 2 3 … 44
.heex
<% rows = [%{no: "PED-2491", cl: "Mercadinho Bom Preço", tone: "pos", st: "Pago", tot: "R$ 1.284,90", h: "14:02", dl: "0.00"}, %{no: "PED-2492", cl: "Padaria Central", tone: "", st: "Aberto", tot: "R$ 312,00", h: "14:40", dl: "0.35"}, %{no: "PED-2493", cl: "Farmácia Vida", tone: "neg", st: "Atrasado", tot: "R$ 2.940,10", h: "ontem", dl: "0.70"}] %>
<div class="grid grid-cols-2 gap-4">
  <div class="flex flex-col gap-1.5">
    <span class="font-mono text-xs text-muted">advanced</span>
    <.data_table id="dt-advanced" rows={rows} total={1096} page={2} page_size={25} on_page="paginate" pager="canvas" summary_label="1–25 de 1.096" row_attrs={&%{style: "--dl:#{&1.dl}s"}}>
      <:toolbar>
    <.table_search id="dt-search" placeholder="Buscar pedido" width="220px" />
    <.button canvas size="sm"><.icon name="filter" size="md" />Filtrar</.button>
    <span class="min-w-0 flex-1"></span>
    <.button canvas size="sm"><.icon name="download" size="md" />Exportar</.button>
  </:toolbar>
      <:col :let={o} label="Pedido" class="font-mono text-[13px]">{o.no}</:col>
  <:col :let={o} label="Cliente">{o.cl}</:col>
  <:col :let={o} label="Status"><.badge canvas dot color={Map.get(%{"pos" => "success", "neg" => "error"}, o.tone, "neutral")}>{o.st}</.badge></:col>
  <:col :let={o} label="Total" align="right" class="font-medium text-ink">{o.tot}</:col>
  <:col :let={o} label="Hora" align="right">{o.h}</:col>
    </.data_table>
  </div>
  <div class="flex flex-col gap-1.5">
    <span class="font-mono text-xs text-muted">grid</span>
    <.data_table id="dt-grid" variant="grid" rows={rows} total={1096} page={2} page_size={25} on_page="paginate" pager="rounded" summary_label="1–25 de 1.096" row_attrs={&%{style: "--dl:#{&1.dl}s"}}>
      <:col :let={o} label="Pedido" class="font-mono text-[13px]">{o.no}</:col>
  <:col :let={o} label="Cliente">{o.cl}</:col>
  <:col :let={o} label="Status"><.badge canvas dot color={Map.get(%{"pos" => "success", "neg" => "error"}, o.tone, "neutral")}>{o.st}</.badge></:col>
  <:col :let={o} label="Total" align="right" class="font-medium text-ink">{o.tot}</:col>
  <:col :let={o} label="Hora" align="right">{o.h}</:col>
    </.data_table>
  </div>
</div>

pager rounded · pill · circle · canvas

rounded
1 2 3 … 44
pill
1 2 3 … 44
circle
1 2 3 … 44
canvas
1 2 3 … 44
.heex
<div class="flex flex-wrap items-center gap-2.5">
  <div :for={s <- ~w(rounded pill circle canvas)} class="flex flex-col gap-1.5">
    <span class="font-mono text-xs text-muted">{s}</span>
    <.data_table_pager page={2} pages={44} shape={s} />
  </div>
</div>

Valores da API que completam a paridade

Total sort_dir=asc
Total sort_dir=desc
.heex
<div class="grid grid-cols-4 gap-3.5">
  <div class="flex flex-col items-start gap-1.5"><.chip>Total <.icon name="arrow-up" size="sm" /></.chip><span class="font-mono text-[11px] text-muted">sort_dir=asc</span></div>
  <div class="flex flex-col items-start gap-1.5"><.chip>Total <.icon name="arrow-down" size="sm" /></.chip><span class="font-mono text-[11px] text-muted">sort_dir=desc</span></div>
</div>

Experimente: row_groups com stream e expand (inserir, remover, reordenar)

Abra um detalhe com a seta e mexa na lista: a linha de detalhe acompanha.
Pedido Cliente Total
PED-2491 Mercadinho Bom Preço R$ 1.284,90
PED-2492 Padaria Central R$ 312,00
PED-2493 Farmácia Vida R$ 2.940,10
PED-2494 Açougue Serrinha R$ 860,00
.heex
<%!-- stream + :expand: um <tbody> por linha, o detalhe acompanha a linha ao inserir no topo, remover ou reordenar --%>
<.live_component module={LedgerShowcaseWeb.Demos.RowsDemo} id="rows-data-table" kind={:data_table} />

Uso

<.data_table id="accounts" rows={@accounts} selectable sortable paginate={25} />

Regras

  • Barra: busca à esquerda, filtros e ações à direita.
  • Rodapé: contagem à esquerda, paginação à direita.

Referência da API

Todos os function components, atributos, valores e slots que este componente cobre.

A table with a search box, sortable headers, a pagination footer and row selection, in the two looks of Ledger UI: advanced (title bar, the table as a card, summary and pager underneath) and grid (search at the right, the gridjs table, summary and pager).

The LiveView owns the state: it filters, sorts and slices the rows and gives the component the current page; the component renders it and sends events back.

<.data_table
  id="users"
  rows={@users}
  search={@search}
  on_search="search"
  sort_by={@sort_by}
  sort_dir={@sort_dir}
  on_sort="sort"
  page={@page}
  page_size={10}
  total={@total}
  on_page="paginate"
>
  <:title>Users</:title>
  <:col :let={user} label="Name" sort_key="name">{user.name}</:col>
  <:col :let={user} label="Email">{user.email}</:col>
</.data_table>

def handle_event("search", %{"search" => q}, socket), do: ...
def handle_event("sort", %{"column" => col, "direction" => dir}, socket), do: ...
def handle_event("paginate", %{"page" => page}, socket), do: ...

Rows may be a stream (rows={@streams.users}): the body renders with phx-update="stream" and total drives the footer. :col, :action, :expand, :empty, :foot, row_id, row_key, row_item, row_click, selection and columns that leave by priority work as in LedgerUI.Components.Table.table/1, which renders the table.

Events

on_search (%{"search" => q}), on_sort (%{"column" => c, "direction" => d}), on_page (%{"page" => "3"}), on_page_size (%{"page_size" => "30"}), on_select (%{"id" => key, "selected" => "true" | "false"}), on_select_all (%{"selected" => ..}). With page_path (a function of the page number) the pager renders patch links instead.

Extras over the README

A :toolbar slot (a row with a rule under it, for a search and buttons written by hand), body="card" (the table directly in a card body instead of the bordered wrapper of the table) and the pager looks canvas (plain) and pages (buttons, with previous arrow). Styled only with Tailwind utilities.

Not implemented

peek and peek_label are accepted; peek draws nothing yet. The pager pages are not focusable (they carry phx-click only).

<.data_table>

Attr Type Default Values / notes
id (required) :string
rows (required) :any The rows of the current page, a list or a stream.
variant :string "advanced" advanced, grid
body :string "wrap" wrap, card
row_id :any nil
row_key :any nil
row_item :any &Function.identity/1
row_click :any nil
row_class :any nil
row_attrs :any nil
hoverable :boolean nil
search :string ""
search_placeholder :string nil
on_search :any nil
sort_by :string nil
sort_dir :string "asc" asc, desc
on_sort :any nil
page :integer 1
page_size :integer 10
total :integer nil
on_page :any nil
page_path :any nil
footer :boolean true
compact :boolean false
sticky_header :boolean false
scroll_label :string nil
peek :boolean false
peek_label :string "Hidden columns"
sort_label :string nil
row_label :any nil
headless :boolean false
borderless :boolean false
page_sizes :list []
on_page_size :any nil
pager :string "rounded" rounded, pill, circle, canvas, pages - The pager look: rounded, pill, circle, canvas (plain) or pages (buttons).
pager_siblings :integer 1
pager_arrows :boolean true
loading :boolean false
loading_rows :integer 3
show_label :string "Show"
entries_label :string "entries"
prev_label :string "Previous"
next_label :string "Next"
page_label :string "Page %{page}"
filter_label :string "Filter"
summary_label :string nil Summary text with %{from}, %{to} and %{total}.
select_all_label :string "Select all rows"
select_label :string "Select row"
selected :list []
all_selected :boolean nil
on_select :any nil
on_select_all :any nil
expand_label :string nil
target :any nil
class :any nil
wrapper_class :any nil
select_attrs :any nil
body_class :any nil
row_groups :boolean nil One <tbody> per row, so the :expand rows of a stream move with their row; on by itself for a stream with :expand. See Table.

Any other HTML/phx attribute goes to the root element.

Slots:

  • <:action>
  • <:actions> - Buttons at the right of the title bar.
  • <:bulk> - Rendered under the table (a bulk_bar).
  • <:col> (required) - attrs: label, class, head_class, align, sort_key, priority, style
  • <:empty>
  • <:expand>
  • <:filters> - A filter panel the title bar's filter button opens. Receives the JS that toggles it.
  • <:foot> - attrs: class, align, colspan, priority
  • <:summary> - Replaces the summary text; receives %{first: n, last: n, total: n}.
  • <:title> - The advanced heading.
  • <:toolbar> - A row above the table, written by hand.

<.data_table_pager>

The pager of the data table footer: the page numbers (rounded, pill, circle, canvas) or the buttons of pages. Pages near the current one, the first and the last, with … between.

Attr Type Default Values / notes
page (required) :integer
pages (required) :integer How many pages there are.
shape :string "rounded" rounded, pill, circle, canvas, pages
on_page :any nil Event or %JS{} sent with %{"page" => "3"}.
page_path :any nil Function of a page number returning a path (patch links).
target :any nil
page_label :string "Page %{page}"
prev_label :string "Previous"
next_label :string "Next"
siblings :integer 1
arrows :boolean true
class :any nil

Any other HTML/phx attribute goes to the root element.

<.table_search>

The search field of the title bar (a sunken box, 32px). With on_search, the field sits in a form (<id>-search-form, input <id>-search) that sends %{"search" => query} on every change (debounced 300ms) and on submit.

Attr Type Default Values / notes
id (required) :string
value :string ""
on_search :any nil Event name or %JS{} for the query changes.
target :any nil
placeholder :string nil
width :string "220px"
shortcut :string nil A key hint (/) drawn at the right.
class :any nil

Any other HTML/phx attribute goes to the root element.

<.table_toolbar>

The toolbar above a table: search, controls, actions, and the row of applied filters as removable chips.

Attr Type Default Values / notes
id :string "table-toolbar"
search :string nil The current query; the search field shows when on_search or search is given.
on_search :any nil
search_placeholder :string nil
search_width :string "260px"
shortcut :string nil
filters :list [] Applied filters as chips: [%{id: "plan", label: "Plano: Pro"}].
on_remove_filter :any nil Sent with %{"id" => id} by a chip's remove button.
on_clear :any nil
filters_label :string "Filtros:"
clear_label :string "Limpar tudo"
remove_label :string "Remover"
target :any nil
class :any nil

Any other HTML/phx attribute goes to the root element.

Slots:

  • <:actions> - Buttons at the right end.
  • <:start> - Controls right after the search (a filter button, a view switch).