Interactive Tables in MDX With TanStack Table
Sorting, Search and Downloads on the Table the Author Wrote
بِسْمِ ٱللّٰهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ
Size
Spacing
Font
A table in a post is read once and then used. The reader looks for the row that matters to him, wants two columns side by side that sit far apart, or needs the numbers in a spreadsheet. With a static table, all of that is manual work: scrolling, comparing by eye, copying cell by cell.
An earlier post on this blog argued for tables without any JavaScript, and for small tables that still holds. A table of four rows reads at a glance, and a script would add nothing.
Larger tables are different. From this post on, a table on this site with at least four columns or at least six rows gets a few tools: sortable headers, movable columns, a full view with search, and downloads as CSV, PNG and WebP. The author still writes a plain Markdown table. What follows is how the tools work, why TanStack Table carries them, and what they cost.
Quick definitions
What is a pipe table?
The way Markdown writes a table: one line per row, cells divided by vertical bars, and a line of dashes under the header. When the site is built, it becomes an ordinary HTML table, so the author never writes HTML.
What does headless mean for a library?
A headless library holds the logic and draws nothing. TanStack Table knows how rows are sorted, searched and ordered; what the table looks like and where its buttons sit stays with the page that uses it.
What is lazy loading?
Loading a piece of code only when it is about to be needed, not with the page. A reader who never reaches a large table, or never saves one, never downloads the code behind it.
What the tools do
The table below is one of them. It lists what TanStack Table v9 offers and which of it this site uses.
| TanStack Table feature | Used here | What it gives the reader |
|---|---|---|
| Row sorting | yes | Click a header to sort by that column; again to reverse; a third time for the author’s order |
| Global filtering | yes | The search in the full view hides rows without a match |
| Column ordering | yes | Drag a header, or press Alt with an arrow key on it, to move the column |
| Column pinning | yes | On a narrow screen the first column stays in place while the rest scrolls |
| Column filtering | no | One search box serves short tables better than a filter per column |
| Pagination | no | Post tables are short enough to show whole |
| Row selection | no | A post has nothing to do with selected rows |
| Grouping and aggregation | no | The rows are already grouped the way the author wrote them |
| Row expanding | no | Pipe tables have no nested rows |
| Column sizing and resizing | no | The browser sizes the columns from their content |
| Column visibility | no | Moving a column covers the need to compare two of them |
Click “Used here” twice to put the used features first, or open the full view with the button under the table and search for “column”. The download menu saves the table as you see it: sorted as sorted, columns in the order you left them, and in the full view only the rows the search kept. The CSV is saved as UTF-8 with a byte order mark, the sign spreadsheet apps look for before they read umlauts and Chinese correctly. PNG and WebP are for sharing, drawn in the site’s fonts and the theme you are reading in.
Comparison tables often leave the first header empty, because that column only labels the rows. Such a column stays where the author put it: its rows keep their order, and a sort button there would have no name for a screen reader. Every other header is a real button with aria-sort on its cell, and keyboard focus stays on a header while it moves.
Why TanStack Table
Most table libraries come with their own controls and their own styling, and several draw the table from data with their own markup. For a blog that is the wrong way round. The table already exists: the MDX compiler turned the author’s pipe table into HTML, with links and inline code in the cells, styled by the site. A library that redraws it from data loses that markup or asks for it to be rebuilt, and one with its own controls brings a second look and its own interface text into a site that has both already, in five languages.
| Library | License | Its own controls and styling |
|---|---|---|
| TanStack Table | MIT | none: it holds the logic, the page draws everything |
| DataTables | MIT | yes |
| Tabulator | MIT | yes |
| Grid.js | MIT | yes |
| simple-datatables | LGPL-3.0 | yes |
TanStack Table is headless: it keeps the state (sort, search, column order, pinning) and computes which rows to show in which order. The site’s script then moves the existing <tr> elements into that order and hides the rest. For a new column order, every cell carries its column’s id, and each row’s cells are put back in the order TanStack holds. No cell is ever drawn again, so a link stays a link and code stays code. simple-datatables was left out for its license: LGPL is copyleft, and this site keeps to permissive licenses.
The site already uses TanStack Charts for the same reason: it leaves the look to the stylesheet.
Version 9 helps with size. Every feature is registered on its own, so a table carries only the code of the features it declares (features guide). With the four used here registered, the engine (TanStack Table plus this site’s code around it) comes to 13.2 KB after brotli.
Loading only what is used
None of this loads on a page that does not need it. A page whose tables are all small loads no table script, and a page with a tool table starts with a small script that waits. Recorded in this site’s production build, which file arrives when, and its size after brotli:
| When | What loads | Size (brotli) |
|---|---|---|
| The page loads | The waiting script and the buttons | 1.6 KB |
| A table comes within 600 pixels of the screen | The engine: TanStack Table and the sorting and moving | 13.2 KB |
| The reader opens the full view | The full view and its search | 1.0 KB |
| The reader saves a file | The export, and for an image the screenshot library | 0.8 KB + 7.6 KB |
That table has four rows and three columns, so it stays plain, by the same rule.
The images come from modern-screenshot (MIT), which draws the table with its computed styles and embeds the page’s own web fonts, so the picture shows the same faces as the page. The first PNG had one flaw: the text touched the edge of the image. A <table> ignores padding, so the margin could not come from CSS. Alhamdulillah, drawing the table onto a slightly larger canvas in the page’s background color, with 32 pixels of room on every side, fixed it.
Old posts keep their tables
Four posts on this site had tables before the tools existed, and they keep them, in all five languages. Their tables were written, translated and checked as plain tables, and the post about static tables would contradict itself if its own tables grew buttons. One line in a post’s frontmatter, static_Tables: true, gives every table in it the plain wrapper, whatever its size. New posts leave it out.
What it costs
- Code to maintain. About a dozen small files: the engine, the moves, the full view, the search highlight, the exports and the menu. The menu repeats the one under the charts and could become a shared module.
- Two dependencies. TanStack Table and modern-screenshot, both pinned to an exact version. Each update needs a look at the tables.
- Dragging was tested with a mouse. It uses the browser’s own drag and drop, which touch browsers support unevenly and which was not tested on a touch screen. The keyboard move works wherever there is a keyboard; sorting, the full view and the downloads were checked at phone width.
- Plain matching. The search finds text that contains what was typed. TanStack documents fuzzy filtering, which can also sort the rows by how closely they match (fuzzy filtering guide); the tables here have not needed it so far.
- Two kinds of table. Old posts keep plain tables, new posts get tools. A reader moving between posts sees both.
A table that stays the author’s
A table library can be chosen by its feature list, and most lists look alike. The question that decided it here was a different one: who owns the table? When the library owns it, the author’s markup becomes its input and the look becomes its theme. When the library only holds the logic, the table stays what the author wrote, and the tools sit on top of it. Only the tables that need them pay for them.
Can TanStack Table add sorting to an existing HTML table?
Not on its own, but with a short script. TanStack Table works on data and draws nothing: it computes the order and the visible rows. On this site a script reads the text of each row into TanStack Table and then moves the existing rows into the order it returns, so the HTML of every cell stays as the author wrote it.
Do the tables still work without JavaScript?
Yes, as plain tables. The table is HTML written at build time and scrolls sideways when it is too wide. Without JavaScript the tool buttons are hidden, because there is nothing to run them.
Which tables on this site get the tools?
Tables with at least four columns or at least six rows, in posts published from this one on. Smaller tables read at a glance and stay plain, and the posts that had tables before the tools keep their plain tables.
Does the table search find typos?
No. It finds the rows whose text contains what was typed, ignoring upper and lower case. TanStack Table documents fuzzy filtering, which also accepts looser matches, such as the letters of a word in the right order, and ranks rows by how closely they match; the tables here have not needed it so far.
Web 8 of 8
Back to Web