Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Projects and Practice

Project: Sortable, Filterable Data Table in Lit

Build a data table web component in Lit with sorting, text filtering, pagination, a column definition API, custom cell renderers and proper table accessibility.

Tables are where many component libraries earn their reputation. This project builds data-table: you give it columns and rows, and it adds sorting, a search box and pagination. Along the way you will make a few design decisions that apply to any "data in, UI out" component: derive instead of store, keep the original data untouched, and expose a small API.

The API first

Design what the user will write before you write the component:

JavaScript
const table = document.querySelector("data-table");
table.columns = [
  { key: "name", label: "Name" },
  { key: "price", label: "Price", numeric: true, render: (v) => "$" + v.toFixed(2) },
];
table.rows = [{ name: "Notebook", price: 4.5 }, /* ... */];
table.pageSize = 5;

columns describes how to show each field; rows is plain data. Because they are objects and arrays, they are set as properties (never attributes), and users replace them to update the table.

Derive, do not store

The visible rows depend on four inputs: rows, the filter text, the sort setting and the page. Do not keep a second "visibleRows" property that you must remember to update. Compute it in render instead. Then there is no way for the pieces to disagree.

flowchart LR
    R["rows (input)"] --> F["filter by text"]
    F --> S["sort by column"]
    S --> P["slice current page"]
    P --> V["render rows"]

Each stage is a pure function of the previous one.

For very large tables you would memoize or move this to a controller, but for thousands of rows a simple pipeline is fast.

The component

data-table

What to notice

  • attribute: false on columns and rows: complex data is passed as properties. See attributes and converters.
  • Getters, not state. _filtered, _sorted and _pageRows are plain getters. Lit re-runs render when _filter, _sortKey, _sortDir, _page or rows change, and the getters give fresh answers each time.
  • [...this._filtered].sort(...). sort mutates in place. Copying first protects the user's array, a bug that is hard to find when it happens.
  • Resetting the page. When the filter or the sort changes, the old page number might not exist anymore, so we go back to page 1. Forgetting this gives the "No matching rows" bug on page 4 of 1.
  • Custom renderers. c.render(value, row) returns a string or even a Lit template, so cells can contain links, badges or buttons, without the table knowing about them.
  • Accessibility. A real <table> with <th> gives screen readers row and column navigation for free. aria-sort announces the sort state, header buttons are keyboard-operable, and the live row count tells users what the filter did.

A div grid styled to look like a table loses all of that semantics. Use real table elements unless you are willing to rebuild the ARIA grid pattern completely.

Scaling up

For tens of thousands of rows, rendering only the visible window ("virtualization") matters more than anything else. Lit's labs offer a virtualizer for this. For server data, move sorting and paging to the server and let the component fire events such as sort-change, page-change and filter-change, then load a new rows array. The pattern from async data loading applies directly.

Try it yourself

  1. Add a selectable mode with a checkbox per row and a selection-change event.
  2. Debounce the search input by 200 ms.
  3. Add a column sortable: false option.
  4. Highlight the matching text in each cell with <mark>.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why are filtered, sorted and paged rows computed in getters?

    Derived values computed during render always match the current inputs. Stored copies must be manually updated and can drift.
  2. 2. Why does the sort copy the array with [...rows] first?

    Array.prototype.sort changes the array in place. Copying keeps the input untouched.
  3. 3. Why set attribute: false on columns and rows?

    Attributes are strings. Rich data is set through JavaScript properties.
  4. 4. Why reset the page to the first page when the filter changes?

    A filter can reduce the page count, leaving the user on an empty page that does not exist.
  5. 5. Which markup gives screen readers table navigation without extra work?

    Native table semantics are built in; replacements need many ARIA attributes to match.

Summary

  • Design the public API first: columns and rows as properties.
  • Derive filtered, sorted and paged data instead of storing it.
  • Never mutate the input; reset the page when results change.
  • Use real table markup plus aria-sort and live counts.
  • Large or remote data needs virtualization or server-side events.

Next: Practice: Exercises and Common Mistakes