Skip to main content
Yoummday Light Light Dark System

Table

<sl-table> | SlTable
Since 2.37.0 stable

A component that displays data in a table.

Row 1 - Col 1 Row 1 - Col 2 Row 1 - Col 3 Row 2 - Col 1 Row 2 - Col 2 Row 2 - Col 3 Row 3 - Col 1 Row 3 - Col 2 Row 3 - Col 3 Row 4 - Col 1 Row 4 - Col 2 Row 5 - Col 3
<sl-table>
  <sl-table-row>
    <sl-table-cell>Row 1 - Col 1</sl-table-cell>
    <sl-table-cell>Row 1 - Col 2</sl-table-cell>
    <sl-table-cell>Row 1 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 2 - Col 1</sl-table-cell>
    <sl-table-cell>Row 2 - Col 2</sl-table-cell>
    <sl-table-cell>Row 2 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 3 - Col 1</sl-table-cell>
    <sl-table-cell>Row 3 - Col 2</sl-table-cell>
    <sl-table-cell>Row 3 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 4 - Col 1</sl-table-cell>
    <sl-table-cell>Row 4 - Col 2</sl-table-cell>
    <sl-table-cell>Row 5 - Col 3</sl-table-cell>
  </sl-table-row>
</sl-table>

Examples

With Header

Head row - Col 1 Head row - Col 2 Head row - Col 3 Row 2 - Col 1 Row 2 - Col 2 Row 2 - Col 3 Row 3 - Col 1 Row 3 - Col 2 Row 3 - Col 3 Row 4 - Col 1 Row 4 - Col 2 Row 5 - Col 3
<sl-table>
  <sl-table-row slot="head">
    <sl-table-cell>Head row - Col 1</sl-table-cell>
    <sl-table-cell>Head row - Col 2</sl-table-cell>
    <sl-table-cell>Head row - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 2 - Col 1</sl-table-cell>
    <sl-table-cell>Row 2 - Col 2</sl-table-cell>
    <sl-table-cell>Row 2 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 3 - Col 1</sl-table-cell>
    <sl-table-cell>Row 3 - Col 2</sl-table-cell>
    <sl-table-cell>Row 3 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 4 - Col 1</sl-table-cell>
    <sl-table-cell>Row 4 - Col 2</sl-table-cell>
    <sl-table-cell>Row 5 - Col 3</sl-table-cell>
  </sl-table-row>
</sl-table>
Head row - Col 1 Head row - Col 2 Head row - Col 3 Row 2 - Col 1 Row 2 - Col 2 Row 2 - Col 3 Row 3 - Col 1 Row 3 - Col 2 Row 3 - Col 3 Row 4 - Col 1 Row 4 - Col 2 Row 4 - Col 3
<div style="height: 200px; overflow-x:scroll">
<sl-table stickyHead>
  <sl-table-row slot="head">
    <sl-table-cell>Head row - Col 1</sl-table-cell>
    <sl-table-cell>Head row - Col 2</sl-table-cell>
    <sl-table-cell>Head row - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 2 - Col 1</sl-table-cell>
    <sl-table-cell>Row 2 - Col 2</sl-table-cell>
    <sl-table-cell>Row 2 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 3 - Col 1</sl-table-cell>
    <sl-table-cell>Row 3 - Col 2</sl-table-cell>
    <sl-table-cell>Row 3 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 4 - Col 1</sl-table-cell>
    <sl-table-cell>Row 4 - Col 2</sl-table-cell>
    <sl-table-cell>Row 4 - Col 3</sl-table-cell>
  </sl-table-row>
</sl-table>
</div>

Sticky first column

Head row - Col 1 Head row - Col 2 Head row - Col 3 Head row - Col 4 Head row - Col 5 Row 2 - Col 1 Row 2 - Col 2 Row 2 - Col 3 Row 2 - Col 4 Row 2 - Col 5 Row 3 - Col 1 Row 3 - Col 2 Row 3 - Col 3 Row 3 - Col 4 Row 3 - Col 5 Row 4 - Col 1 Row 4 - Col 2 Row 4 - Col 3 Row 4 - Col 4 Row 4 - Col 5
<div style="width: 75%; overflow-y: auto;">
<sl-table stickyFirstColumn>
  <sl-table-row slot="head">
    <sl-table-cell>Head row - Col 1</sl-table-cell>
    <sl-table-cell>Head row - Col 2</sl-table-cell>
    <sl-table-cell>Head row - Col 3</sl-table-cell>
    <sl-table-cell>Head row - Col 4</sl-table-cell>
    <sl-table-cell>Head row - Col 5</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 2 - Col 1</sl-table-cell>
    <sl-table-cell>Row 2 - Col 2</sl-table-cell>
    <sl-table-cell>Row 2 - Col 3</sl-table-cell>
    <sl-table-cell>Row 2 - Col 4</sl-table-cell>
    <sl-table-cell>Row 2 - Col 5</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 3 - Col 1</sl-table-cell>
    <sl-table-cell>Row 3 - Col 2</sl-table-cell>
    <sl-table-cell>Row 3 - Col 3</sl-table-cell>
    <sl-table-cell>Row 3 - Col 4</sl-table-cell>
    <sl-table-cell>Row 3 - Col 5</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 4 - Col 1</sl-table-cell>
    <sl-table-cell>Row 4 - Col 2</sl-table-cell>
    <sl-table-cell>Row 4 - Col 3</sl-table-cell>
    <sl-table-cell>Row 4 - Col 4</sl-table-cell>
    <sl-table-cell>Row 4 - Col 5</sl-table-cell>
  </sl-table-row>
</sl-table>
</div>

Both sticky

Head row - Col 1 Head row - Col 2 Head row - Col 3 Head row - Col 4 Head row - Col 5 Row 2 - Col 1 Row 2 - Col 2 Row 2 - Col 3 Row 2 - Col 4 Row 2 - Col 5 Row 3 - Col 1 Row 3 - Col 2 Row 3 - Col 3 Row 3 - Col 4 Row 2 - Col 5 Row 4 - Col 1 Row 4 - Col 2 Row 4 - Col 3 Row 4 - Col 4 Row 4 - Col 5
<div style="width: 75%; height: 200px; overflow: auto;">
<sl-table stickyHead stickyFirstColumn>
  <sl-table-row slot="head">
    <sl-table-cell>Head row - Col 1</sl-table-cell>
    <sl-table-cell>Head row - Col 2</sl-table-cell>
    <sl-table-cell>Head row - Col 3</sl-table-cell>
    <sl-table-cell>Head row - Col 4</sl-table-cell>
    <sl-table-cell>Head row - Col 5</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 2 - Col 1</sl-table-cell>
    <sl-table-cell>Row 2 - Col 2</sl-table-cell>
    <sl-table-cell>Row 2 - Col 3</sl-table-cell>
    <sl-table-cell>Row 2 - Col 4</sl-table-cell>
    <sl-table-cell>Row 2 - Col 5</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 3 - Col 1</sl-table-cell>
    <sl-table-cell>Row 3 - Col 2</sl-table-cell>
    <sl-table-cell>Row 3 - Col 3</sl-table-cell>
    <sl-table-cell>Row 3 - Col 4</sl-table-cell>
    <sl-table-cell>Row 2 - Col 5</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 4 - Col 1</sl-table-cell>
    <sl-table-cell>Row 4 - Col 2</sl-table-cell>
    <sl-table-cell>Row 4 - Col 3</sl-table-cell>
    <sl-table-cell>Row 4 - Col 4</sl-table-cell>
    <sl-table-cell>Row 4 - Col 5</sl-table-cell>
  </sl-table-row>
</sl-table>
</div>

With Sorting

<div id="demo-table"></div>

<script type="module">
  // this whole script is only neccessary for this demo
  // in a lit environment just write <sl-table .data=${myData} @sort-completed=${outerElement.requestUpdate()}>
  import { html, render } from 'https://unpkg.com/lit-html';

  const table = document.getElementById('demo-table');
  let someData = [{ col1: 'A1', col2: 'A2', col3: 'A3'}, { col1: 'B1', col2: 'B2', col3: 'B3'}, { col1: 'C1', col2: 'C2', col3: 'C3'}, { col1: 'D1', col2: 'D2', col3: 'D3'}];
  const rerender = () => {
    render(html`
      <sl-table .data=${someData}>
        <sl-table-row slot="head">
          <sl-table-cell sortable="col1">Head row - Col 1</sl-table-cell>
          <sl-table-cell sortable="col2">Head row - Col 2</sl-table-cell>
          <sl-table-cell sortable="col3" active direction="desc">Head row - Col 3</sl-table-cell>
        </sl-table-row>
        ${someData.map(row => html` <sl-table-row>
          <sl-table-cell>${row.col1}</sl-table-cell>
          <sl-table-cell>${row.col2}</sl-table-cell>
          <sl-table-cell>${row.col3}</sl-table-cell>
        </sl-table-row>`)}
      </sl-table>
    `, table);
  }
  rerender();
  table.addEventListener('sl-sort-completed', rerender)

</script>

With selected row

Head row - Col 1 Head row - Col 2 Head row - Col 3 Row 2 - Col 1 Row 2 - Col 2 Row 2 - Col 3 Row 3 - Col 1 Row 3 - Col 2 Row 3 - Col 3 Row 4 - Col 1 Row 4 - Col 2 Row 5 - Col 3
<sl-table>
  <sl-table-row slot="head">
    <sl-table-cell>Head row - Col 1</sl-table-cell>
    <sl-table-cell>Head row - Col 2</sl-table-cell>
    <sl-table-cell>Head row - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 2 - Col 1</sl-table-cell>
    <sl-table-cell>Row 2 - Col 2</sl-table-cell>
    <sl-table-cell>Row 2 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row selected>
    <sl-table-cell>Row 3 - Col 1</sl-table-cell>
    <sl-table-cell>Row 3 - Col 2</sl-table-cell>
    <sl-table-cell>Row 3 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row>
    <sl-table-cell>Row 4 - Col 1</sl-table-cell>
    <sl-table-cell>Row 4 - Col 2</sl-table-cell>
    <sl-table-cell>Row 5 - Col 3</sl-table-cell>
  </sl-table-row>
</sl-table>

With variant colored rows

Row 2 - Col 1 Row 2 - Col 2 Row 2 - Col 3 Row 3 - Col 1 Row 3 - Col 2 Row 3 - Col 3 Row 4 - Col 1 Row 4 - Col 2 Row 5 - Col 3 Row 4 - Col 1 Row 4 - Col 2 Row 5 - Col 3 Row 2 - Col 1 Row 2 - Col 2 Row 2 - Col 3
<sl-table>
  <sl-table-row variant="info">
    <sl-table-cell>Row 2 - Col 1</sl-table-cell>
    <sl-table-cell>Row 2 - Col 2</sl-table-cell>
    <sl-table-cell>Row 2 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row variant="success">
    <sl-table-cell>Row 3 - Col 1</sl-table-cell>
    <sl-table-cell>Row 3 - Col 2</sl-table-cell>
    <sl-table-cell>Row 3 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row variant="warning">
    <sl-table-cell>Row 4 - Col 1</sl-table-cell>
    <sl-table-cell>Row 4 - Col 2</sl-table-cell>
    <sl-table-cell>Row 5 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row variant="danger">
    <sl-table-cell>Row 4 - Col 1</sl-table-cell>
    <sl-table-cell>Row 4 - Col 2</sl-table-cell>
    <sl-table-cell>Row 5 - Col 3</sl-table-cell>
  </sl-table-row>
  <sl-table-row variant="primary">
    <sl-table-cell>Row 2 - Col 1</sl-table-cell>
    <sl-table-cell>Row 2 - Col 2</sl-table-cell>
    <sl-table-cell>Row 2 - Col 3</sl-table-cell>
  </sl-table-row>
</sl-table>

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

Script Import Bundler React

To import this component from the CDN using a script tag:

<script type="module" src="https://v2-48-10.yoummday-theme.pages.dev/components/table/table.js"></script>

To import this component from the CDN using a JavaScript import:

import 'https://v2-48-10.yoummday-theme.pages.dev/components/table/table.js';

To import this component using a bundler:

import '@yoummday/ymmd-shoelace/dist/components/table/table.js';

To import this component as a React component:

import SlTable from '@yoummday/ymmd-shoelace/dist/react/table';

Slots

Name Description
(default) The default slot for the table body.
head The slot for the table head.

Learn more about using slots.

Properties

Name Description Reflects Type Default
data The data to display in the table - only for sorting, not for rendering. Record[] | undefined -
stickyFirstColumn Makes the first column sticky. boolean false
stickyHead Makes the head sticky. boolean false
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
sl-sort-request onSlSortRequest Emitted when the column is selected for sorting. -
sl-sort-completed onSlSortCompleted Emitted when the sorting is completed (if .data was passed to the table). -

Learn more about events.

Parts

Name Description
head The component’s head wrapper sl-thead.
body The component’s body wrapper sl-tbody.

Learn more about customizing CSS parts.