08 · Tables

Table

variant basic · bordered · hoverable · zebra · grid · data · data-grid · rounded row/top/none · sort asc/desc · align

The base table: muted uppercase header, rows with a 1px divider and right-aligned numbers.

Sorting and hovering

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
.heex
<.table id="orders-sort">
  <.table_head>
    <:col>Pedido</:col>
    <:col>Cliente</:col>
    <:col>Status</:col>
    <:col align="right">Total <span class="inline-block animate-[mo-sort_4s_ease-in-out_infinite]"><.icon name="arrow-up" size="sm" /></span></:col>
    <:col align="right">Hora</:col>
  </.table_head>
  <.table_body class="*:animate-[mo-rowhl_var(--dur,6s)_var(--dl,0s)_infinite]">
      <.table_row :for={{no, cl, tone, st, tot, h, dl} <- [{"PED-2491", "Mercadinho Bom Preço", "pos", "Pago", "R$ 1.284,90", "14:02", "0.00"}, {"PED-2492", "Padaria Central", "", "Aberto", "R$ 312,00", "14:40", "1.50"}, {"PED-2493", "Farmácia Vida", "neg", "Atrasado", "R$ 2.940,10", "ontem", "3.00"}, {"PED-2494", "Açougue Serrinha", "pos", "Pago", "R$ 860,00", "ontem", "4.50"}]} style={"--dl:#{dl}s"}>
        <:cell class="font-mono text-[13px]">{no}</:cell>
        <:cell>{cl}</:cell>
        <:cell><.badge canvas dot color={Map.get(%{"pos" => "success", "neg" => "error"}, tone, "neutral")}>{st}</.badge></:cell>
        <:cell align="right" class="font-medium text-ink">{tot}</:cell>
        <:cell align="right">{h}</:cell>
      </.table_row>
  </.table_body>
</.table>

variant

basic
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
bordered
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
hoverable
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
zebra
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
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
data
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
data-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
.heex
<div class="grid grid-cols-2 gap-[18px]">
  <div :for={v <- ~w(basic bordered hoverable zebra grid data data-grid)} class="flex flex-col gap-1.5">
    <span class="font-mono text-xs text-muted">{v}</span>
    <.table id={"variant-" <> v} variant={v}>
      <.table_head>
        <:col>Pedido</:col>
        <:col>Cliente</:col>
        <:col>Status</:col>
        <:col align="right">Total</:col>
        <:col align="right">Hora</:col>
      </.table_head>
      <.table_body>
        <.table_row :for={{no, cl, tone, st, tot, h, dl} <- [{"PED-2491", "Mercadinho Bom Preço", "pos", "Pago", "R$ 1.284,90", "14:02", "0.00"}, {"PED-2492", "Padaria Central", "", "Aberto", "R$ 312,00", "14:40", "0.35"}, {"PED-2493", "Farmácia Vida", "neg", "Atrasado", "R$ 2.940,10", "ontem", "0.70"}]} style={"--dl:#{dl}s"}>
          <:cell class="font-mono text-[13px]">{no}</:cell>
          <:cell>{cl}</:cell>
          <:cell><.badge canvas dot color={Map.get(%{"pos" => "success", "neg" => "error"}, tone, "neutral")}>{st}</.badge></:cell>
          <:cell align="right" class="font-medium text-ink">{tot}</:cell>
          <:cell align="right">{h}</:cell>
        </.table_row>
      </.table_body>
    </.table>
  </div>
</div>

rounded · align · sort_dir

rounded="row"
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
rounded="top"
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
sort_dir asc / desc
Total Total
.heex
<div class="grid grid-cols-3 gap-4">
  <div :for={r <- ~w(row top)} class="flex flex-col gap-1.5">
    <span class="font-mono text-xs text-muted">rounded="{r}"</span>
    <.table id={"rounded-" <> r} rounded={r}>
      <.table_head>
        <:col>Pedido</:col>
        <:col>Cliente</:col>
        <:col>Status</:col>
        <:col align="right">Total</:col>
        <:col align="right">Hora</:col>
      </.table_head>
      <.table_body>
        <.table_row :for={{no, cl, tone, st, tot, h, dl} <- [{"PED-2491", "Mercadinho Bom Preço", "pos", "Pago", "R$ 1.284,90", "14:02", "0.00"}, {"PED-2492", "Padaria Central", "", "Aberto", "R$ 312,00", "14:40", "0.35"}]} style={"--dl:#{dl}s"}>
          <:cell class="font-mono text-[13px]">{no}</:cell>
          <:cell>{cl}</:cell>
          <:cell><.badge canvas dot color={Map.get(%{"pos" => "success", "neg" => "error"}, tone, "neutral")}>{st}</.badge></:cell>
          <:cell align="right" class="font-medium text-ink">{tot}</:cell>
          <:cell align="right">{h}</:cell>
        </.table_row>
      </.table_body>
    </.table>
  </div>
  <div class="flex flex-col gap-1.5">
    <span class="font-mono text-xs text-muted">sort_dir asc / desc</span>
    <div class="flex flex-wrap items-center gap-2.5">
      <.chip>Total <.icon name="arrow-up" size="sm" /></.chip>
      <.chip>Total <.icon name="arrow-down" size="sm" /></.chip>
    </div>
  </div>
</div>

API values that complete the parity

R$ 1.284,90
align=left
R$ 1.284,90
align=center
R$ 1.284,90
align=right
.heex
<div class="grid grid-cols-3 gap-3.5">
  <div :for={a <- ~w(left center right)} class="flex flex-col items-start gap-1.5">
    <div class={["w-40 border-b border-line py-1.5 font-mono text-[13px] leading-[18px]", a == "center" && "text-center", a == "right" && "text-right"]}>R$ 1.284,90</div>
    <span class="font-mono text-[11px] text-muted">align={a}</span>
  </div>
</div>

Try it: right-click on a row opens the real menu

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

Clique com o botão direito numa linha.

.heex
<.context_menu id="tbl-ctx" class="relative max-w-[640px]">
  <.table id="orders-ctx">
    <.table_head>
      <:col>Pedido</:col>
      <:col>Cliente</:col>
      <:col align="right">Total</:col>
    </.table_head>
    <.table_body>
      <.table_row :for={{no, cl, tot} <- [{"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"}]}>
        <:cell class="font-mono text-[13px]">{no}</:cell>
        <:cell>{cl}</:cell>
        <:cell align="right" class="font-medium text-ink">{tot}</:cell>
      </.table_row>
    </.table_body>
  </.table>
  <:item icon="external" kbd="↵" phx-click={show_toast("Pedido aberto", kind: "neutral")}>Abrir</:item>
  <:item icon="copy" phx-click={show_toast("Pedido duplicado", kind: "success")}>Duplicar</:item>
  <:item icon="trash" kbd="⌫" tone="danger" divider phx-click={show_toast("Pedido excluído", kind: "neutral", actions: [%{label: "Desfazer"}])}>Excluir</:item>
</.context_menu>
<p class="mt-2 mb-0 text-xs text-muted">Clique com o botão direito numa linha.</p>
<.toaster id="ledger-toaster" />

Try it: row_groups with a stream and expand (insert, remove, reorder)

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 (row_groups liga sozinho). Estado e eventos em LedgerShowcaseWeb.Demos.RowsDemo:
     stream_insert(socket, :orders, order, at: 0) · stream_delete(socket, :orders, order) · stream(socket, :orders, items, reset: true) --%>
<.live_component module={LedgerShowcaseWeb.Demos.RowsDemo} id="rows-table" kind={:table} />

Usage

<.table id="accounts" rows={@accounts}>
  <:col :let={a} label="Conta">{a.name}</:col>
</.table>

Rules

  • Numbers and currency on the right in tabular figures; text on the left.
  • Hover in bg-hover; selected in accent-soft.
  • dense density (8px) for long lists.

API reference

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

Ledger UI's tables: basic, bordered, hoverable, zebra, the grid and the lean data looks, with :col slots the way Phoenix's generated CoreComponents.table/1 takes them.

<.table id="users" rows={@users}>
  <:col :let={user} label="Name">{user.name}</:col>
  <:col :let={user} label="Job" align="right">{user.job}</:col>
</.table>

<.table id="users" rows={@streams.users} variant="hoverable"
  row_click={fn {_id, user} -> JS.navigate(~p"/users/#{user.id}") end}>
  <:col :let={{_id, user}} label="Name" sort_key="name">{user.name}</:col>
  <:action :let={{_id, user}}><.button size="sm">Edit</.button></:action>
</.table>

The markup is div[data-part=table-wrap] > table[data-part=table], styled only with Tailwind utilities: the look of the table (variant, compact, rounded) is a set of local variables on the <table> (--tpx, --td-py, ...) that every cell reads, so the same cell classes serve the rows mode and the hand-written tables. variant is nil for the bare table; rounded (row, top) rounds the rows or the header row.

A cell takes its text colour, weight and size from the utilities of its class (<:col class="font-medium text-ink">, class="font-mono text-[13px]").

In the rows mode the classes every cell shares (padding, borders, header type, background) are set once on the <table> with descendant selectors; a cell only carries what varies (alignment, class, priority, card role). The hand-written table keeps them on each cell.

Events

Every event attr takes an event name or a %JS{}; target sets phx-target.

  • on_sort: %{"column" => sort_key, "direction" => "asc" | "desc"} (the direction to sort by next). A column with sort_key gets class="sort" and the arrows; sort_by/sort_dir mark the current order.
  • on_select: %{"id" => key, "selected" => "true" | "false"} (the new state of that row). on_select_all: %{"selected" => "true" | "false"}. selected lists the checked keys; the key is row_key.(row) or the row's DOM id. With a stream, stream_insert the rows whose selection changed and give all_selected.

Hand-written tables

Without rows, the table renders its inner block: table_head/1, table_body/1, table_row/1, table_cell/1, table_foot/1 and table_empty/1 (see each).

Columns that leave by priority

A :col with priority 1, 2 or 3 (up to 4) leaves (header, cells and :foot cell together) when the table's own box (a container, @container) gets narrower than the matching breakpoints entry (720, 560 and 420px by default). Tailwind only sees literal class names, so the widths are a fixed list (the hide_widths list of the internal Table.Layout module); another width raises ArgumentError.

Card layout, details panel, copy columns (used by card_table/1)

Extra attributes card_below, details, row_editing, row_action and the column attributes role, width, truncate, card, copy* back the "card" table (see LedgerUI.Components.CardTable): under card_below px each row is a two-line grid placed by the role of its cells (head, status, title, value); card_below is one of the card_widths list of the internal Table.Layout module. details adds a panel under the clicked row (hook LedgerUI.CardTable) with the :detail slots and the columns that left by priority; copy turns a cell into a copy button; row_editing swaps a row for the :edit slot.

Streams with :expand / :detail: row_groups

The detail row (:expand) and the panel row (:detail) are siblings of the row's <tr>. In a stream the client only moves the element that carries the stream id, so a reset, a stream_insert at at: 0 or a stream_delete would leave those rows behind. With row_groups each item is a <tbody id={dom_id}> holding its <tr> and the rows that belong to it, and phx-update="stream" goes on the <table> (its <thead>, <tfoot> and the empty <tbody> keep their own ids). It is on by itself when rows is a stream and there is an :expand or a panel (details with :detail or priority columns); row_groups={false} keeps the single <tbody> and row_groups={true} forces the layout for lists too. In that layout the <tr> id is "<dom_id>-row", body_class is applied to every item's <tbody>, the hook of the panel sits on the <table> and the zebra stripes count items, not <tr>s. Without row_groups (and without the stream + expand/panel case) the markup is the one it always was.

Not implemented

peek / peek_label (the panel with the hidden columns) and bleed are accepted but draw nothing yet. borderless is accepted: the design has no style for it.

<.table>

Attr Type Default Values / notes
id (required) :string
rows :any nil A list or a @streams.x stream. Leave it out to write the table by hand.
row_id :any nil Function giving each row's DOM id (streams: the stream id).
row_click :any nil Function of the row returning a JS or an event.
row_class :any nil Function of the row returning extra classes of its <tr>.
row_attrs :any nil Function of the row returning a map of extra <tr> attributes.
row_item :any &Function.identity/1 Maps each row before the slots get it.
row_key :any nil Function giving the selection key of a row (default: DOM id).
variant :string nil nil, basic, bordered, hoverable, zebra, grid, data, data-grid, flat - nil is the bare table (rows light up on hover; basic does not). grid is the gridjs look; data/data-grid the lean one of data_table/1.
rounded :string nil nil, row, top, none
sort_by :string nil
sort_dir :string "asc" asc, desc
on_sort :any nil
on_select :any nil
on_select_all :any nil
selected :list []
all_selected :boolean nil
hoverable :boolean nil
compact :boolean false
headless :boolean false
sticky_header :boolean false
borderless :boolean false
bleed :boolean false Not implemented yet.
card_below :integer nil Width (px) of the table's own box under which rows become two-line cards (needs roles); one of Table.Layout.card_widths/0.
details :boolean false A click on a row opens a panel under it (hook LedgerUI.CardTable).
panel_label :string "Details"
row_editing :any nil Function of the row; true swaps its cells for the :edit slot.
row_action :any nil Function of the row; false leaves the content of its :action cells out.
body_class :any nil Classes of the <tbody> (of each item's, with row_groups).
copy_toast :boolean false A toast (ledger:toast, the contract of clipboard) after a copy column copies. A column's own copy_toast wins.
copy_toast_message :string nil The toast text; %s is the copied value, shown in bold. Default: "%s · <copied_label>".
row_groups :boolean nil One <tbody> per row, holding its <tr>, panel and detail rows, so a stream moves them together. nil turns it on for a stream with :expand or a details panel.
select_attrs :any nil Function of the row returning extra attributes (class, style) of its checkbox label.
expand_open :any nil Function of the row: with :expand, shows its detail row always open (no toggle column), for rows it returns true for.
breakpoints :list [720, 560, 420] Widths (px) under which priority 1, 2, 3... leave; each is one of Table.Layout.hide_widths/0.
wrapper :boolean true false leaves out the bordered box (a table inside a card body).
container :boolean nil
peek :boolean false Not implemented yet.
peek_label :string "Hidden columns"
target :any nil
expand_label :string nil
scroll_label :string nil
sort_label :string nil
row_label :any nil
select_all_label :string "Select all rows"
select_label :string "Select row"
wrapper_class :any nil
class :any nil Classes of the <table>.

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

Slots:

  • <:action> - attrs: priority, card - Cells appended to every row, after the columns.
  • <:col> - attrs: label, class, head_class, dim, align, sort_key, priority, style, role, width, truncate, card, copy, copy_label, copied_label, copy_show, copy_tip, copy_toast, copy_toast_message - A column; receives the row (after row_item).
  • <:detail> - attrs: label - A label: value line of the details panel; receives the row.
  • <:edit> - Inline form in place of the cells of a row for which row_editing is true.
  • <:empty> - Shown in a full-width row when there are no rows.
  • <:expand> - Detail shown under a row; receives %{row: row, toggle: js}.
  • <:foot> - attrs: class, align, colspan, priority - A cell of the <tfoot> row, for totals.
  • <:inner_block> - The hand-written table when rows is not given.

<.table_head>

Attr Type Default Values / notes
sort_by :string nil
sort_dir :string "asc" asc, desc
on_sort :any nil
on_select_all :any nil Adds a select-all checkbox cell first; the event it sends.
all_selected :boolean false
some_selected :boolean false
select_all_id :string nil
select_all_label :string "Select all rows"
sort_label :string nil
target :any nil
sticky_header :boolean false
class :any nil Classes of the <tr>.

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

Slots:

  • <:col> - attrs: label, class, align, colspan, priority, sort_key, title, id - A header cell; its content is the label (or give label).
  • <:inner_block> - Extra <th> cells after the columns, or further header rows.

<.table_body>

Attr Type Default Values / notes
class :any nil

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

Slots:

  • <:inner_block> (required)

<.table_row>

Attr Type Default Values / notes
selected :boolean nil true checks the box and tints the row.
on_click :any nil
on_select :any nil Adds a selection checkbox cell first; the event it sends.
select_value :any nil Sent as "id" by the checkbox.
select_label :string "Select row"
target :any nil
class :any nil

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

Slots:

  • <:cell> - attrs: class, align, colspan, rowspan, priority, title, id - A <td> of the row.
  • <:inner_block> - Free cells after the :cell ones.

<.table_cell>

Attr Type Default Values / notes
header :boolean false Renders a <th scope="row">.
align :string nil nil, left, center, right
colspan :any nil
rowspan :any nil
priority :integer nil
class :any nil

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

Slots:

  • <:inner_block>

<.table_foot>

Attr Type Default Values / notes
class :any nil Classes of the :cell row.

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

Slots:

  • <:cell> - attrs: class, align, colspan, priority - A <td> of the totals row.
  • <:inner_block> - Free <tr> rows after the :cell row.

<.table_empty>

Attr Type Default Values / notes
id (required) :string
class :any nil Classes of the cell.

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

Slots:

  • <:inner_block> (required)

<.table_checkbox>

Attr Type Default Values / notes
id :string nil
checked :boolean false
indeterminate :boolean false Draws the dash of a partial selection (needs an id, for its hook).
label (required) :string The aria-label of the box.
middle :boolean false vertical-align: middle (the box of a body row; the header's sits on the baseline).
on_click :any nil
values :map %{} Sent as phx-value-* with on_click.
target :any nil
class :any nil

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

<.table_expand_toggle>

Attr Type Default Values / notes
row_id (required) :string
open :boolean false
label :string "Toggle details"
class :any nil

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

<.table_detail_row>

Attr Type Default Values / notes
row_id (required) :string
open :boolean false
class :any nil Classes of the detail panel.

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

Slots:

  • <:inner_block> (required)

<.table_skeleton>

Attr Type Default Values / notes
id (required) :string
rows :integer 5
columns :list [] Header labels; one column each.
cols :integer 4 How many columns when columns is empty.
variant :string nil nil, basic, bordered, hoverable, zebra, grid, data, data-grid, flat
rounded :string nil nil, row, top, none
compact :boolean false
headless :boolean false
shapes :list [] Per column: text (a bar), avatar, pill or number.
label :string "Loading"
class :any nil

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