Zubra UI
Code for
GitHub

data-table

A table of records you can sort, filter and select from. Click a column's heading to sort by it (again to reverse; announced with aria-sort), type in the box to filter every column, and with selectable tick rows (the heading's box selects every shown row). columns is [{ key, label, sortable?, align?, format? }] (format: "number", "currency", "date"); rows is an array of objects with an id. selected (an array of ids) is two-way. Long tables page by pageSize.

Try it: events appear here (selectedchange, sortchange).

Source: data-table.zb, and its registry item.

Install

Compiled (any project)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/data-table.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/data-table.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/data-table.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/data-table.json
One file, no dependencies (5.7 KB gzipped)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/data-table.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/data-table.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/data-table.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/data-table.json
.zb source (to edit it with Zubra)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/data-table.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/data-table.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/data-table.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/data-table.json
No build: a script tag, straight from this site
HTML
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/data-table.js"></script>

The compiled install writes src/components/zubra/data-table.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.

Usage

index.html
<data-table label="Invoices" currency="USD"></data-table>

<script type="module">
  import './components/zubra/data-table.js';

  const element = document.querySelector('data-table');
  element.selectable = true;
  element.columns = [
    { key: 'number', label: 'Invoice', sortable: true },
    { key: 'customer', label: 'Customer', sortable: true },
    { key: 'status', label: 'Status', sortable: true },
    { key: 'amount', label: 'Amount', sortable: true, format: 'currency' },
  ];
  element.rows = [
    {
      id: 1,
      number: 'INV-1042',
      customer: 'Northwind',
      status: 'Paid',
      amount: 1250,
    },
    {
      id: 2,
      number: 'INV-1043',
      customer: 'Acme Bakery',
      status: 'Due',
      amount: 310.5,
    },
    {
      id: 3,
      number: 'INV-1044',
      customer: 'Globex',
      status: 'Overdue',
      amount: 4800,
    },
    {
      id: 4,
      number: 'INV-1045',
      customer: 'Initech',
      status: 'Paid',
      amount: 99,
    },
    {
      id: 5,
      number: 'INV-1046',
      customer: 'Umbrella',
      status: 'Due',
      amount: 2075,
    },
  ];
  element.addEventListener('selectedchange', (event) => console.log('selectedchange', event.detail));
  element.addEventListener('sortchange', (event) => console.log('sortchange', event.detail));
</script>
Example.jsx
import './components/zubra/data-table.js';

const selectable = true;
const columns = [
  { key: 'number', label: 'Invoice', sortable: true },
  { key: 'customer', label: 'Customer', sortable: true },
  { key: 'status', label: 'Status', sortable: true },
  { key: 'amount', label: 'Amount', sortable: true, format: 'currency' },
];
const rows = [
  {
    id: 1,
    number: 'INV-1042',
    customer: 'Northwind',
    status: 'Paid',
    amount: 1250,
  },
  {
    id: 2,
    number: 'INV-1043',
    customer: 'Acme Bakery',
    status: 'Due',
    amount: 310.5,
  },
  {
    id: 3,
    number: 'INV-1044',
    customer: 'Globex',
    status: 'Overdue',
    amount: 4800,
  },
  {
    id: 4,
    number: 'INV-1045',
    customer: 'Initech',
    status: 'Paid',
    amount: 99,
  },
  {
    id: 5,
    number: 'INV-1046',
    customer: 'Umbrella',
    status: 'Due',
    amount: 2075,
  },
];

export function DataTableExample() {
  return (
    <data-table
      label="Invoices"
      currency="USD"
      selectable={selectable}
      columns={columns}
      rows={rows}
      onselectedchange={(event) => console.log('selectedchange', event.detail)}
      onsortchange={(event) => console.log('sortchange', event.detail)}
    ></data-table>
  );
}
Example.vue
<script setup>
import './components/zubra/data-table.js';
const selectable = true;
const columns = [
  { key: 'number', label: 'Invoice', sortable: true },
  { key: 'customer', label: 'Customer', sortable: true },
  { key: 'status', label: 'Status', sortable: true },
  { key: 'amount', label: 'Amount', sortable: true, format: 'currency' },
];
const rows = [
  {
    id: 1,
    number: 'INV-1042',
    customer: 'Northwind',
    status: 'Paid',
    amount: 1250,
  },
  {
    id: 2,
    number: 'INV-1043',
    customer: 'Acme Bakery',
    status: 'Due',
    amount: 310.5,
  },
  {
    id: 3,
    number: 'INV-1044',
    customer: 'Globex',
    status: 'Overdue',
    amount: 4800,
  },
  {
    id: 4,
    number: 'INV-1045',
    customer: 'Initech',
    status: 'Paid',
    amount: 99,
  },
  {
    id: 5,
    number: 'INV-1046',
    customer: 'Umbrella',
    status: 'Due',
    amount: 2075,
  },
];
const onSelectedchange = (event) => console.log('selectedchange', event.detail);
const onSortchange = (event) => console.log('sortchange', event.detail);
</script>

<template>
  <data-table
    label="Invoices"
    currency="USD"
    :selectable.prop="selectable"
    :columns.prop="columns"
    :rows.prop="rows"
    @selectedchange="onSelectedchange"
    @sortchange="onSortchange"
  ></data-table>
</template>

<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) -->
Example.svelte
<script>
  import './components/zubra/data-table.js';
  const selectable = true;
  const columns = [
    { key: 'number', label: 'Invoice', sortable: true },
    { key: 'customer', label: 'Customer', sortable: true },
    { key: 'status', label: 'Status', sortable: true },
    { key: 'amount', label: 'Amount', sortable: true, format: 'currency' },
  ];
  const rows = [
    {
      id: 1,
      number: 'INV-1042',
      customer: 'Northwind',
      status: 'Paid',
      amount: 1250,
    },
    {
      id: 2,
      number: 'INV-1043',
      customer: 'Acme Bakery',
      status: 'Due',
      amount: 310.5,
    },
    {
      id: 3,
      number: 'INV-1044',
      customer: 'Globex',
      status: 'Overdue',
      amount: 4800,
    },
    {
      id: 4,
      number: 'INV-1045',
      customer: 'Initech',
      status: 'Paid',
      amount: 99,
    },
    {
      id: 5,
      number: 'INV-1046',
      customer: 'Umbrella',
      status: 'Due',
      amount: 2075,
    },
  ];
</script>

<data-table
  label="Invoices"
  currency="USD"
  {selectable}
  {columns}
  {rows}
  onselectedchange={(event) => console.log('selectedchange', event.detail)}
  onsortchange={(event) => console.log('sortchange', event.detail)}
></data-table>
example.component.ts
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/data-table.js';

@Component({
  selector: 'app-data-table-example',
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `
    <data-table
      label="Invoices"
      currency="USD"
      [selectable]="selectable"
      [columns]="columns"
      [rows]="rows"
      (selectedchange)="onSelectedchange($event)"
      (sortchange)="onSortchange($event)"
    ></data-table>
  `,
})
export class DataTableExample {
  selectable = true;

  columns = [
    { key: 'number', label: 'Invoice', sortable: true },
    { key: 'customer', label: 'Customer', sortable: true },
    { key: 'status', label: 'Status', sortable: true },
    { key: 'amount', label: 'Amount', sortable: true, format: 'currency' },
  ];

  rows = [
    {
      id: 1,
      number: 'INV-1042',
      customer: 'Northwind',
      status: 'Paid',
      amount: 1250,
    },
    {
      id: 2,
      number: 'INV-1043',
      customer: 'Acme Bakery',
      status: 'Due',
      amount: 310.5,
    },
    {
      id: 3,
      number: 'INV-1044',
      customer: 'Globex',
      status: 'Overdue',
      amount: 4800,
    },
    {
      id: 4,
      number: 'INV-1045',
      customer: 'Initech',
      status: 'Paid',
      amount: 99,
    },
    {
      id: 5,
      number: 'INV-1046',
      customer: 'Umbrella',
      status: 'Due',
      amount: 2075,
    },
  ];

  onSelectedchange(event: Event) {
    console.log('selectedchange', (event as CustomEvent).detail);
  }

  onSortchange(event: Event) {
    console.log('sortchange', (event as CustomEvent).detail);
  }
}

API

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
columns
property only
{ key, label, sortable }[][]
rows
property only
{ id, number, customer, status, amount }[][]
selectedunknown[][]
selectablebooleanfalse
filterablebooleantrue
pageSize
attribute page-size
number0
currencystring"USD"
labelstring"Records"
Events
EventWhen
selectedchangeThe selection changed; detail is the array of selected ids.
sortchangeThe sort changed; detail is { key, direction }.

Size

3.0 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 19.5 KB.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour