Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 28 ยท Projects

Project: Live Search and Filterable Table

Build a live-search table with HTMX and Alpine.js: debounced search, sortable columns, pagination with hx-get, column toggles and an empty state.

Almost every admin panel and catalog has the same screen: a search box, a table you can sort, and page links at the bottom. It is a perfect HTMX job, because the server owns the data and is good at searching, sorting and slicing it. Alpine adds the parts the server does not need to know about: which sort column is active, which columns are visible, and a friendly "clear search" button when nothing matches.

You will build the search first, then grow it into the full table. Related lessons: Verbs, Triggers and Parameters explains hx-trigger modifiers such as delay, and Common Interaction Patterns introduces live search.

What each part does

Search box Debounced, sends the query after you pause.
Sort and pages Server sorts and slices, HTMX swaps the table.
Column toggles Alpine hides cells instantly.
1 / 2

Design: one endpoint, one form

The cleanest design is a single route, GET /rows, that accepts q (the search text), sort, dir and page, and returns the whole results block: a summary line, the table and the pager. Every interaction sends the same form values, so the server never has to guess the current state.

Replacing the whole results block each time keeps the server simple. The header cells come back with the right sort arrow already in place, and the pager shows the right page. The search input stays outside the swapped block, so typing is never interrupted and the cursor never jumps.

hx-trigger="input changed delay:300ms" waits 300 ms after the last keystroke, and changed skips requests when the text is the same as before. The result is far fewer requests than one per key.

Live search with a debounce

Type "gr" quickly: only one request is sent, 300 ms after you stop. The search trigger also fires when someone presses Enter or clicks the clear button of a type="search" field. The request counter in the message proves how few requests were used.

Step 2: sort, pages, toggles and an empty state

The full project combines everything:

  • The form #f holds q plus two hidden fields, sort and dir. The hidden fields are bound to Alpine state with :value.
  • Triggers. The form listens for the search box (input changed delay:300ms from:#q), for the load event, and for a custom refresh event on the body.
  • Sorting. Header buttons live in the server response. Clicking one updates Alpine's sort and dir, waits one tick so the hidden inputs update, then dispatches refresh. Without $nextTick, HTMX would read the old hidden values.
  • Pagination. Page buttons use hx-get="/rows" with hx-vals for the page number and hx-include="#f" to send the current search and sort. The hidden fields do not include a page, so any new search or sort naturally starts at page 1.
  • Column toggles. Checkboxes set cols.city and cols.plan in Alpine. Cells in the swapped table read them with x-show, so hiding a column is instant and survives new search results.
  • Empty state. When nothing matches, the server returns a message with an Alpine button that clears the box and refreshes.
Searchable, sortable, paged table

Try this sequence: sort by City, search "o" (the sort stays), go to page 2 (the search and sort are still applied), hide the Plan column and change the search again (the column stays hidden), then type something that matches nobody and use the Alpine "Clear search" button.

Why the server-rendered header?

The sort arrows are drawn by the server, because the server is the one that actually sorted the data. That keeps the arrow honest: it always matches what you see. The Alpine state sort and dir exists only to tell the next request what to ask for.

Spot the duplicated header code in the demo? In a real project the table would be one partial template, and the header buttons would be a loop over a list of columns. The demo keeps it spelled out so you can read it all in one place.

History-safe URLs

In this sandbox, searching changes the table but not the address bar. On a real site, that is a problem: people expect to bookmark a search, share it, and use the back button. HTMX fixes this with hx-push-url="true" on the element that sends the request (read-only, because the preview has no history):

HTML
<form id="f" hx-get="/people" hx-target="#results" hx-push-url="true"
      hx-trigger="input changed delay:300ms from:#q, search from:#q">

With that attribute, a search for "oslo" updates the address to /people?q=oslo&sort=name&dir=asc. For this to be history-safe, the same URL must also work when typed into the browser directly. So the server route /people should return the whole page (layout, search box with the value filled in, results) for a normal request, and only the results fragment when the HX-Request header is present, as shown in Server-Side Setup. Also set Vary: HX-Request, so a cache never hands the fragment to a normal visit. The details of history snapshots are in URLs, History and Navigation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does hx-trigger="input changed delay:300ms" do?

    delay debounces the trigger, and changed ignores events where the value is the same as last time.
  2. 2. Why does the sort header button call $nextTick before dispatching the refresh event?

    Alpine updates bound values asynchronously. Waiting one tick means the form already contains the new sort and dir when HTMX collects its values.
  3. 3. Why is the page number sent with hx-vals instead of a hidden form field?

    Because the form has no page value, only pager buttons add one. Any other trigger sends no page, and the server defaults to page 1.
  4. 4. What makes a pushed URL "history-safe"?

    People can bookmark, reload or share the URL, so it must work without HTMX. The server returns the full page when there is no HX-Request header.

Summary

  • One route plus one form carries the whole state (query, sort, direction), and every interaction re-sends it.
  • delay:300ms and changed make live search efficient; from:#q lets a form listen to one input.
  • Alpine owns sort state and column visibility; $nextTick makes sure bound hidden inputs are current before HTMX reads them.
  • Pager buttons use hx-get, hx-include and hx-vals, so each page keeps the current search and sort.
  • Swapped cells can read the wrapper's x-data, so column toggles survive every new result set.
  • Use hx-push-url plus a server that serves full pages for real, shareable URLs.

Next project: Shopping Cart and Product Quick View.