Skip to content

Sortable table in WordPress

A data table where every column sorts with a click, ascending or descending, with no library.

Here is an excerpt of this component's code in WordPress. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in WordPress

pattern.html
<!-- wp:group {"className": "dtab", "layout": {"type": "default"}} -->
<div class="wp-block-group dtab"><!-- wp:html -->
<div class="dtab__scroll" role="region" aria-labelledby="dtab-caption" tabindex="0">
          <table data-sortable>
            <caption id="dtab-caption">Logements disponibles à la location<span>Choisissez un en-tête de colonne pour trier le tableau.</span></caption>
            <thead>
              <tr>
              <th scope="col" data-sort="text" aria-sort="ascending"><button type="button" class="dtab__sort">Commune<svg class="dtab__icon" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 5v14m-5-5 5 5 5-5"/></svg></button></th>
              <th scope="col" data-sort="text"><button type="button" class="dtab__sort">Logement<svg class="dtab__icon" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 5v14m-5-5 5 5 5-5"/></svg></button></th>
              <th scope="col" data-sort="number"><button type="button" class="dtab__sort--num dtab__sort">Surface<svg class="dtab__icon" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 5v14m-5-5 5 5 5-5"/></svg></button></th>
              <th scope="col" data-sort="number"><button type="button" class="dtab__sort--num dtab__sort">Loyer<svg class="dtab__icon" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 5v14m-5-5 5 5 5-5"/></svg></button></th>
              <th scope="col" data-sort="text"><button type="button" class="dtab__sort">Disponible<svg class="dtab__icon" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 5v14m-5-5 5 5 5-5"/></svg></button></th>
script.js
(() => {
  if (!document.querySelector(".dtab")) return;
  document.querySelectorAll("[data-sortable]").forEach((table) => {
    const headers = [...table.querySelectorAll("th[data-sort]")];
    const body = table.tBodies[0];
    const collator = new Intl.Collator("fr", { numeric: true, sensitivity: "base" });
    const read = (row, index) => row.cells[index].dataset.value ?? row.cells[index].textContent.trim();

    headers.forEach((header) => {
      header.querySelector("button").addEventListener("click", () => {
        const ascending = header.getAttribute("aria-sort") !== "ascending";
        const index = header.cellIndex;
style.css
:root {
  --dtab-card: #ffffff;
  --dtab-text: #0b1f3a;
  --dtab-muted: #4a5d78;
  --dtab-line: #dbe4f0;
  --dtab-head: #eef3fa;
  --dtab-ring: #1e3a8a;
  --demo-tableau-triable-bg: #f4f7fb;
  --demo-tableau-triable-text: #0b1f3a;
}

@media (prefers-color-scheme: dark) {
tableau-triable.php
<?php
/**
 * Plugin Name: Forge, Tableau triable
 * Description: Un tableau de données dont chaque colonne se trie d'un clic, par ordre croissant ou décroissant, sans bibliothèque. Ajoute la composition « Tableau triable » à l'éditeur de blocs.
 * Version: 1.0.0
 * Requires at least: 6.6
 * Requires PHP: 8.1
 * Author: Forge
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: forge-tableau-triable
 *

Free component: copy it freely, including for commercial projects.

Data tables components in WordPress
  • A documentation table showing where a kit or component has been tested (static HTML, WordPress, Elementor, Webflow, v0…), with status, last test date and a remark, filterable by status.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A table of the towns and postcodes a tradesperson covers, with response time and travel fee, filtered by search and by zone.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A timetable table sorted by column and filtered with level and free-place chips, whose rows each become a card on phones.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A filterable table whose visible rows export in one click to a CSV file that opens cleanly in Excel or LibreOffice.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A price table by service: one package to pick, options to tick, and the total with the estimated time recalculated on every click.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A product spec sheet as two-column tables in native collapsible sections, with a button to open or close them all.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component