Part of: Dashboard Starter

The Data Grid: Sorting, Filters, and Columns, Built by Hand

Step 10 is live. Open dashboard.mvmorten.com, click "Try the demo", and go to Customers. Click a column header. Click it again. Then pick "Inactive" and look at the address bar.

Post 9 left the Customers list plain on purpose: one search box, 25 to a page, always sorted by name. This step adds what a real list needs. Sorting, a filter, and a choice of columns, as one grid that Members uses too.

The Customers page on a desktop, light theme, sorted by Updated with a down arrow on the Updated header. A search box, a Status filter set to All, a Columns button and a Clear button sit above the table. Copperleaf Studio, just edited, is at the top, followed by Willow Creek Kennels, Thistle & Thorn Tattoo and others, each with an email, a phone, a status and the date Nov 19, 2026.

The obvious answer was a library

The plan going in leaned toward TanStack Table. The app already uses TanStack Query, the table library is headless, so there's no styling to fight with plain CSS, and plenty of people use it.

Claude Code read the code first and recommended building it by hand. Its reasoning: TanStack Table's main job is sorting and filtering rows in the browser. Customers sorts on the server, so that work isn't needed here. What's left is small: a list of columns, header buttons, a sort select for phones, a column picker, and the switch between a table and cards.

A library would put its own API between a header click and the query string. This project's rule is "no magic": a reader should be able to follow a click all the way to the SQL. So no new dependency.

It isn't a forever decision. A library starts to earn its place with column resizing or reordering, sorting by more than one column, or long lists sorted in the browser. None of those are in this step.

Follow a header click to the SQL

Here's the whole path for "sort by Updated, newest first":

  1. The header button calls sortBy() in grid/listState.ts.
  2. The URL becomes /customers?sort=updated&dir=desc.
  3. The Customers page sends the same query to the API.
  4. CustomersController checks updated against a fixed list of sort columns.
  5. The SQL ends ORDER BY c.UpdatedAt DESC, c.Id DESC.

The fixed list is a dictionary:

public static readonly IReadOnlyDictionary<string, string> SortColumns =
  new Dictionary<string, string>(StringComparer.Ordinal)
  {
    ["name"] = "c.Name",
    ["email"] = "c.Email",
    ["status"] = "c.Status",
    ["updated"] = "c.UpdatedAt",
    ["created"] = "c.CreatedAt",
  };

The request's text only picks a key. The SQL is built from constants: the column comes from the dictionary, and ASC or DESC comes from a bool. Everything else, the tenant, the search, the status and the page, is still a parameter. A column name can't be a parameter in SQL, which is why sorting is a classic place for SQL injection. A dictionary closes it.

A few rules that came out of it:

  • Ties go by Id, in the same direction. Two customers updated at the same moment always come back in the same order, so pages never overlap or skip a row.
  • An unknown value is a 400 that names it. ?sort=updatd is an error, not the default order. A link with a typo should say so, not quietly show something it didn't ask for.
  • The front end is forgiving, the API is strict. An old link or a typo in the address bar reads as the default on the page, so the page only ever asks the API for views that exist.

Two indexes, and a plan worth checking

Sorting 30 customers is free. Sorting a tenant with 50,000 isn't, if SQL Server has to read and sort all of them for every page.

Name and Email already had indexes from step 9. Email's index didn't even need the Id added: SQL Server puts the clustered key into every non-unique index's key, so it's already in Email, then Id, order. Updated and Created got new ones, led by TenantId like every other index, and filtered to live rows. Status got none. It has two values, so sorting by it is close to filtering by it.

Claude Code checked the plans, and the first answer was a surprise. At demo size, SQL Server ignored the new index and sorted anyway, because reading a 39-row table whole is cheaper. So it made a scratch database with 50,000 customers in one tenant and asked again. "Updated, newest first" became a backward seek on the new index and 25 lookups, with no sort. That's the plan you want.

The view lives in the URL

Search, status, sort and page are all in the address. A reload, the Back button and a shared link all show the same rows. Defaults are left out, so the plain list is still just /customers.

Changing the sort or a filter goes back to page 1. Page 3 of a different order is a different set of rows, and landing on it would be confusing.

One "Clear" button shows up when anything differs from the default. The search box used to have its own Clear, so there were two. Now there's one.

The first click on a header sorts text A to Z, but dates newest first. "Updated" showing the oldest change first would feel broken.

The Customers page on a desktop, dark theme, with the Status filter set to Inactive. The line above the table reads 3 inactive customers, and a Clear button shows next to Columns. The table lists Copperleaf Studio, Hollow Oak Furniture and Quarry Stone Masonry, all Inactive.

Column choices stay in your browser

On a wide screen you can hide columns. Name can't be hidden, because it's the link. Created starts hidden.

The choice is saved in localStorage, per user and per grid, so two people sharing a browser keep their own. The alternative was a preferences table on the server, which would follow you to another device. That's a table, a migration and an endpoint for something you set once, so we went with the browser.

Only what you changed is saved. If a later version adds a column, it still follows its default. If localStorage fails, in a private window for example, you just get the default columns.

Column choices aren't in the URL on purpose. A link you share shows the same rows, not your columns.

The picker is a plain <details> element. It opens with a tap, a click or the keyboard, and nothing depends on hover.

The Columns menu open over the Customers table on a desktop, light theme, with checkboxes for Email, Phone, Status and Updated checked and Created unchecked. In the table, the address frontdesk@brightwater.example wraps onto a second line before .example.

On a phone

A phone keeps the cards from post 9. Header taps don't make sense on cards, so sorting is a select on its own row, with the status filter and Clear side by side under it.

Each card shows the date it's sorted by, so the order makes sense when you scroll.

Column choices don't apply to cards. A card already shows what fits.

Two phone screens side by side. Left, dark theme: Customers with a Sort select reading Updated: newest first on its own row, a Status select set to All beside a Clear button, and a card for Copperleaf Studio, Inactive, ending in Updated Nov 19, 2026. Right, light theme: Members with a Sort select reading Member: A to Z, and the visitor's own card, Demo Guest (you), Admin, with the note You can't change your own role or access.

Members moves onto the grid

Members uses the same grid, with one difference: it sorts in the browser. The Members API sends the whole tenant at once, so asking again for a different order would fetch the same rows. sortRows.ts follows the API's rules (one column, then the id), so both lists behave the same. When invites make member lists long, Members can switch to server paging without changing the grid.

Email moved out from under the name into its own column, which you can hide.

And one old bug got fixed on the way. Your own row has no role controls, and the reason was only a tooltip, which needs a mouse to see. Now the row says it: "You can't change your own role or access." The cleanup PR before this step found it, and this step fixed it.

The Members page on a desktop, dark theme, with a Columns button and sortable headers for Member, Email, Role, Status and Last sign-in. Long sandbox addresses such as jordan.blake-72ffeb2ac5bb@demo.invalid wrap at the hyphen and after the @, never mid-word. The visitor's own row reads You can't change your own role or access.

Long emails, wrapped where you'd expect

Members had a known bug: on a narrow desktop, long emails wrapped in the middle of a word. The browser gives you two bad choices. It either won't wrap an address at all, because it's one long word, or with overflow-wrap: anywhere, it breaks at any letter: "sam.rivera@exam" then "ple.com".

So a small pure function splits the address before each dot and after the @:

[email protected]  ->  "sam" ".rivera@" "example" ".com"

The component puts a <wbr>, a line-break opportunity, between the parts. The address can now only break where a person would break it.

Why not cut it short with an ellipsis? Because then the only way to read the whole address is a hover tooltip, and the app doesn't rely on hover.

What went wrong

The phone's sort select was cut off. It read "Updated: newest f". It got its own row.

Short emails wrapped into three lines on Members at 1280 px, because the last sign-in date and time took the room. The table now shows the date only. The cards keep the time.

A CSS rule that was too clever. The first version used :has() and nth-child to widen the sort select. It worked, but you'd need to stop and think to read it. It's a plain class now.

QUOTED_IDENTIFIER. Inserting into Customers from sqlcmd failed without its -I flag. Filtered indexes need QUOTED_IDENTIFIER ON, and that's been true since step 9. CI already passed -I, so nobody noticed until someone typed an insert by hand.

NULLs sort first. An ascending Email sort puts customers with no email at the top, because that's what SQL Server does. Putting them last would stop the index from serving the sort, so it stays. Members follows the same rule, so the two lists agree.

Cleanup before the step

A small PR went in first, for two leftovers.

The "Deleted" notice came back on reload. Browsers keep history state across a reload, and that's where the notice was. Customers now copies it into its own state, then replaces the history entry with one that has none. Claude Code didn't use window.history.replaceState directly. It's one line shorter, but it edits React Router's entry behind its back.

A failed deploy now says why. When App Service can't start the new code, GitHub's log only said the worker didn't start. The real reason is in the container's logs. A new step prints the last 60 lines of them, but only when the deploy fails. It reads them with the deploy's existing Azure login, so there's no new secret. It replaces az webapp log tail, which only streams new lines, so a container that had already died usually showed nothing.

Working with AI on this

Same setup: one AI to plan and write the brief, Claude Code to build.

The brief's lean was wrong, and it said so. The brief leaned toward TanStack Table but asked for a recommendation first. Claude Code read the code and argued for hand-built, with the trade-off written out. That's the reason to ask before building: the plan was written before anyone looked at what the code needed.

It checked the plan at a size that matters. At demo size the indexes didn't get used, which could have looked like they were useless. It built a 50,000-row tenant, checked again, and then dropped the scratch database.

It tested phones without a phone. Chrome wouldn't resize its window, so, as in the last two steps, it loaded the app in frames exactly 390 and 1280 px wide, in light and dark.

It left the layout alone. The content column is 768 px on every desktop, from step 4, so long customer names wrap with Email, Status and Updated showing. It's readable. A wider layout for lists would be its own change, so it wrote that down and moved on.

/security-review found nothing. Sort, direction and status come from fixed lists, every value is a parameter, the tenant and deleted-row filters always apply, and nothing renders as HTML.

What's next

Post 11 is forms. The customer form is still fields written out one by one with server-side errors only, so there's plenty to improve.

The code for this post is tagged v0.10. Try the demo, sort by Updated, change a customer, and watch it jump to the top.