Skip to content
VinUIComponentsData Table

Data Table

A table you can sort, filter, select rows in and page through. Same markup and props in every style: switch the style in the header to see it change.

Basic

Sort by a header, filter by text, select rows and page through 12 invoices, five at a time. In HTML, add data-vin-data-table (and data-page-size) and include vinui.js; every row is in the markup and the script hides the ones on other pages.

Status
INV-1042Asha RaoPaid₹12,400
INV-1041Ben CarterPending₹8,250
INV-1040Chen WeiOverdue₹3,100
INV-1039Divya NairPaid₹15,000
INV-1038Elena PetrovaPaid₹4,720
data-table-basic.html
<div class="vin-data-table" data-vin-data-table data-page-size="5" style="max-width: 720px">
  <div class="vin-data-table__toolbar">
    <div class="vin-input vin-input-group vin-data-table__search">
      <span class="vin-input-group__addon"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg></span>
      <input type="search" class="vin-input-group__input" placeholder="Filter invoices…" aria-label="Filter invoices">
    </div>
  </div>
  <div class="vin-table-wrap" role="region" aria-label="Invoices" tabindex="0">
    <table class="vin-table">
      <thead>
        <tr>
          <th scope="col" class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select all rows"></th>
          <th scope="col"><button type="button" class="vin-data-table__sort">Invoice<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m8 9 4-4 4 4"/><path d="m16 15-4 4-4-4"/></svg></button></th>
          <th scope="col"><button type="button" class="vin-data-table__sort">Customer<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m8 9 4-4 4 4"/><path d="m16 15-4 4-4-4"/></svg></button></th>
          <th scope="col">Status</th>
          <th scope="col" class="vin-table__num"><button type="button" class="vin-data-table__sort">Amount<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m8 9 4-4 4 4"/><path d="m16 15-4 4-4-4"/></svg></button></th>
        </tr>
      </thead>
      <tbody>
        <tr><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1042"></td><td>INV-1042</td><td>Asha Rao</td><td><span class="vin-badge vin-badge--success">Paid</span></td><td class="vin-table__num">₹12,400</td></tr>
        <tr><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1041"></td><td>INV-1041</td><td>Ben Carter</td><td><span class="vin-badge vin-badge--warning">Pending</span></td><td class="vin-table__num">₹8,250</td></tr>
        <tr><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1040"></td><td>INV-1040</td><td>Chen Wei</td><td><span class="vin-badge vin-badge--danger">Overdue</span></td><td class="vin-table__num">₹3,100</td></tr>
        <tr><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1039"></td><td>INV-1039</td><td>Divya Nair</td><td><span class="vin-badge vin-badge--success">Paid</span></td><td class="vin-table__num">₹15,000</td></tr>
        <tr><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1038"></td><td>INV-1038</td><td>Elena Petrova</td><td><span class="vin-badge vin-badge--success">Paid</span></td><td class="vin-table__num">₹4,720</td></tr>
        <tr hidden><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1037"></td><td>INV-1037</td><td>Farid Haddad</td><td><span class="vin-badge vin-badge--warning">Pending</span></td><td class="vin-table__num">₹9,900</td></tr>
        <tr hidden><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1036"></td><td>INV-1036</td><td>Grace Kim</td><td><span class="vin-badge vin-badge--success">Paid</span></td><td class="vin-table__num">₹2,350</td></tr>
        <tr hidden><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1035"></td><td>INV-1035</td><td>Hiro Tanaka</td><td><span class="vin-badge vin-badge--danger">Overdue</span></td><td class="vin-table__num">₹6,480</td></tr>
        <tr hidden><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1034"></td><td>INV-1034</td><td>Isla Murphy</td><td><span class="vin-badge vin-badge--success">Paid</span></td><td class="vin-table__num">₹18,200</td></tr>
        <tr hidden><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1033"></td><td>INV-1033</td><td>Jonas Weber</td><td><span class="vin-badge vin-badge--warning">Pending</span></td><td class="vin-table__num">₹5,600</td></tr>
        <tr hidden><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1032"></td><td>INV-1032</td><td>Kavya Iyer</td><td><span class="vin-badge vin-badge--success">Paid</span></td><td class="vin-table__num">₹7,300</td></tr>
        <tr hidden><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select INV-1031"></td><td>INV-1031</td><td>Liam Byrne</td><td><span class="vin-badge vin-badge--success">Paid</span></td><td class="vin-table__num">₹11,050</td></tr>
      </tbody>
    </table>
  </div>
  <div class="vin-data-table__footer">
    <p class="vin-data-table__status" aria-live="polite">0 of 12 selected</p>
    <nav class="vin-pagination" aria-label="Pagination">
      <ul class="vin-pagination__list">
        <li><button type="button" class="vin-btn vin-btn--outline vin-btn--sm" disabled><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m15 6-6 6 6 6"/></svg> Previous</button></li>
        <li><span class="vin-pagination__status">Page 1 of 3</span></li>
        <li><button type="button" class="vin-btn vin-btn--outline vin-btn--sm">Next <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 6 6 6-6 6"/></svg></button></li>
      </ul>
    </nav>
  </div>
</div>

Sorting only

Starts sorted by commits, highest first. Click a header to sort by it; click again to reverse.

Tom OkaforEngineering2021-09-021,290
Sara LindEngineering2022-01-17642
Lucía OrtizProduct2022-11-05215
Meera ShahDesign2023-04-11184
Ravi MenonDocs2024-06-3097
data-table-sorting.html
<div class="vin-data-table" data-vin-data-table style="max-width: 560px">
  <div class="vin-table-wrap" role="region" aria-label="Contributors" tabindex="0">
    <table class="vin-table">
      <thead>
        <tr>
          <th scope="col"><button type="button" class="vin-data-table__sort">Name<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m8 9 4-4 4 4"/><path d="m16 15-4 4-4-4"/></svg></button></th>
          <th scope="col"><button type="button" class="vin-data-table__sort">Role<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m8 9 4-4 4 4"/><path d="m16 15-4 4-4-4"/></svg></button></th>
          <th scope="col"><button type="button" class="vin-data-table__sort">Joined<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m8 9 4-4 4 4"/><path d="m16 15-4 4-4-4"/></svg></button></th>
          <th scope="col" class="vin-table__num" aria-sort="descending"><button type="button" class="vin-data-table__sort">Commits<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m8 9 4-4 4 4"/><path d="m16 15-4 4-4-4"/></svg></button></th>
        </tr>
      </thead>
      <tbody>
        <tr><td>Tom Okafor</td><td>Engineering</td><td>2021-09-02</td><td class="vin-table__num">1,290</td></tr>
        <tr><td>Sara Lind</td><td>Engineering</td><td>2022-01-17</td><td class="vin-table__num">642</td></tr>
        <tr><td>Lucía Ortiz</td><td>Product</td><td>2022-11-05</td><td class="vin-table__num">215</td></tr>
        <tr><td>Meera Shah</td><td>Design</td><td>2023-04-11</td><td class="vin-table__num">184</td></tr>
        <tr><td>Ravi Menon</td><td>Docs</td><td>2024-06-30</td><td class="vin-table__num">97</td></tr>
      </tbody>
    </table>
  </div>
</div>

Selection

Two rows start selected, so the header checkbox shows a dash (mixed). Clicking it selects every row.

Asha Raoasha@example.comOwner
Ben Carterben@example.comAdmin
Chen Weichen@example.comMember
Divya Nairdivya@example.comMember
data-table-selection.html
<div class="vin-data-table" data-vin-data-table style="max-width: 560px">
  <div class="vin-table-wrap" role="region" aria-label="Team members" tabindex="0">
    <table class="vin-table">
      <thead>
        <tr>
          <th scope="col" class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select all rows"></th>
          <th scope="col"><button type="button" class="vin-data-table__sort">Name<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m8 9 4-4 4 4"/><path d="m16 15-4 4-4-4"/></svg></button></th>
          <th scope="col"><button type="button" class="vin-data-table__sort">Email<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m8 9 4-4 4 4"/><path d="m16 15-4 4-4-4"/></svg></button></th>
          <th scope="col"><button type="button" class="vin-data-table__sort">Role<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m8 9 4-4 4 4"/><path d="m16 15-4 4-4-4"/></svg></button></th>
        </tr>
      </thead>
      <tbody>
        <tr><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select Asha Rao"></td><td>Asha Rao</td><td>asha@example.com</td><td>Owner</td></tr>
        <tr><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select Ben Carter"></td><td>Ben Carter</td><td>ben@example.com</td><td>Admin</td></tr>
        <tr data-selected><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select Chen Wei" checked></td><td>Chen Wei</td><td>chen@example.com</td><td>Member</td></tr>
        <tr data-selected><td class="vin-data-table__select"><input type="checkbox" class="vin-checkbox" aria-label="Select Divya Nair" checked></td><td>Divya Nair</td><td>divya@example.com</td><td>Member</td></tr>
      </tbody>
    </table>
  </div>
  <div class="vin-data-table__footer">
    <p class="vin-data-table__status" aria-live="polite">2 of 4 selected</p>
  </div>
</div>

Installation

React, Vue and Svelte: add the component to your project with the CLI. It reads your framework from vin.json and also copies any component this one needs. Then import it:

Terminal
npx @vinish/ui add data-table
React
import { DataTable, dataTableStatus } from "@/components/vin/data-table";
Vue
import DataTable, { dataTableStatus } from "@/components/vin/data-table.vue";
Svelte
import DataTable, { dataTableStatus } from "$lib/components/vin/data-table.svelte";

The import paths assume the default folders: @/components/vin (React and Vue, with the usual @ → src alias) and $lib/components/vin (SvelteKit). Use your own if you chose another folder in init. How the Vue and Svelte versions pass values and content is in Frameworks.

Plain HTML: the classes come with the style's stylesheet. This component also needs vinui.js for its keyboard and pointer behaviour.

API

From spec/data-table.json, which every framework version is tested against.

PropValuesDefaultIn HTML
columns
What each column shows. Columns sort unless sortable is false; numeric right-aligns them.
{ key; header; cell?; sortValue?; filterValue?; sortable?; numeric? }[]—th[scope=col] > button.vin-data-table__sort (th.vin-table__num for numbers)
data
The rows. Sorting, filtering and paging happen in the browser.
T[]——
rowKey
Identifies each row, for selection.
keyof T | (row: T) => string | number——
label
Names the table's scrolling region (and the filter box).
string—aria-label on .vin-table-wrap[role=region]
pageSize
Rows per page; 0 shows them all. The pager is Pagination's simple version.
number0data-page-size on .vin-data-table; nav.vin-pagination in .vin-data-table__footer
selectable
A checkbox per row and a "select all" checkbox in the header, which shows a dash when some rows are selected.
booleanfalseth / td.vin-data-table__select > input.vin-checkbox; tr[data-selected]
defaultSelected / onSelectionChange
Rows selected at first, and a callback with the keys after each change.
(string | number)[]——
filterable
A search box that hides rows whose text doesn't match.
booleanfalse.vin-data-table__toolbar > .vin-input.vin-input-group.vin-data-table__search > input[type=search]
filterLabel / filterPlaceholder
The filter box's name (default "Filter invoices") and placeholder.
string——
defaultSort
Sort on first render. Clicking a header sorts ascending, then descending.
{ key: string; direction: "ascending" | "descending" }—aria-sort on the th
rowLabel
Name of a row's checkbox. Default: "Select" + the first column.
(row: T) => string——
emptyTextReactNodeNo results.tr.vin-data-table__empty > td[colspan]
sort value (HTML)
In HTML, cells sort by their text (numbers like ₹12,400 sort as numbers). Put data-value on a cell to sort by something else.
attribute—td[data-value="12400"]

Parts

React, Vue, SvelteHTML classes
<DataTable>.vin-data-table.vin-data-table__toolbar.vin-data-table__search.vin-data-table__sort.vin-data-table__select.vin-data-table__empty.vin-data-table__footer.vin-data-table__status

Accessibility

  • A real table with th[scope] headers. Sortable headers hold a button, and the sorted column has aria-sort="ascending" or "descending", so screen readers say how it's sorted.
  • The sort arrows are hidden from screen readers; the button's name is the column name.
  • Each row checkbox has a name ("Select INV-1042"). The header checkbox is "Select all rows" and is mixed (indeterminate) when some are selected.
  • The status line ("2 of 12 selected") is a polite live region, so changes are announced.
  • The filter box is a labelled search input; paging uses Pagination's Previous / Next buttons, which are disabled at the ends.
  • On small screens the table scrolls sideways inside its focusable region instead of squashing the columns.