Web component / Examples

<table-modifiable>

Let people choose which columns to read in a native HTML table. See the README for installation and API documentation.

Without JavaScript or if the component module fails to load, every authored column stays readable. Enhancement adds a button and native popover with labeled checkboxes. Focus a table region and use the arrow keys to scroll wider data.

Choose visible columns

Wrap a simple table in <table-modifiable> and list the headers people can toggle in removable. Names must match the first header row’s trimmed text exactly, including case. Use unique names and matching body cells, without colspan or rowspan.

All columns start visible here. Open “Modify Table,” use Tab to reach a checkbox, and press Space to hide or show its column. The component rejects unchecking the last checked checkbox. Press Escape to close the popover and return focus from its controls to the button.

<table-modifiable removable="Name,Email,Phone"
  role="region" aria-label="Contact table" tabindex="0">
  <table>
    <caption>Contact directory</caption>
    <thead>
      <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>John Doe</td>
        <td>john@example.com</td>
        <td>555-1234</td>
      </tr>
      <tr>
        <td>Jane Smith</td>
        <td>jane@example.com</td>
        <td>555-5678</td>
      </tr>
      <tr>
        <td>Bob Johnson</td>
        <td>bob@example.com</td>
        <td>555-9012</td>
      </tr>
    </tbody>
  </table>
</table-modifiable>
Contact directory
Name Email Phone
John Doe john@example.com 555-1234
Jane Smith jane@example.com 555-5678
Bob Johnson bob@example.com 555-9012

Start with fewer columns

Set start-with="Product,Price" to show only those removable columns after enhancement. Stock, Category, and Supplier remain available in the popover. Without JavaScript, all five columns stay visible.

Choose at least one valid removable header name. The last-checkbox safeguard applies to user changes, not initial configuration: an unmatched start-with list can hide every removable column. Hiding changes presentation, not access to data, and doesn’t hide footer cells. See table requirements and behavior for the full rules.

<table-modifiable
  removable="Product,Price,Stock,Category,Supplier"
  start-with="Product,Price"
  role="region" aria-label="Product table" tabindex="0">
  <table>
    <caption>Product pricing and stock</caption>
    <!-- table content -->
  </table>
</table-modifiable>
Product pricing and stock
Product Price Stock Category Supplier
Widget $9.99 42 Tools Acme Corp
Gadget $19.99 15 Electronics Tech Supply
Doohickey $4.99 128 Parts Parts Plus

Rewrite or translate labels

Match your application’s vocabulary or translate the button and panel labels using the button-label, button-aria-label, and tools-label attributes. Here, the longer button name includes the visible “Choisir les colonnes” label. Translate the headers, caption, and region name too, and keep both column lists matched to the headers. lang="fr" identifies the French content.

<table-modifiable lang="fr"
  removable="Nom,Courriel,Téléphone,Adresse"
  start-with="Nom,Courriel"
  button-label="Choisir les colonnes"
  button-aria-label="Choisir les colonnes du personnel"
  tools-label="Colonnes à afficher"
  role="region" aria-label="Tableau du personnel" tabindex="0">
  <table>
    <caption>Coordonnées du personnel</caption>
    <!-- table content -->
  </table>
</table-modifiable>
Coordonnées du personnel
Nom Courriel Téléphone Adresse
Alice Anderson alice@example.com 555-0101 123 Oak St
Bob Brown bob@example.com 555-0202 456 Pine Ave
Carol Clark carol@example.com 555-0303 789 Elm Rd

Respond to column changes

Listen to the table-modifiable:change event to record accepted checkbox changes. This log keeps the last five events and displays column text as text, not HTML. Initial hiding, rebuilds, and rejected attempts to uncheck the last checked checkbox don’t emit events. See the event reference.

<table-modifiable
  id="event-demo"
  removable="First Name,Last Name,Department"
  role="region" aria-label="Event example table" tabindex="0">
  <!-- table -->
</table-modifiable>

<div class="event-log" id="event-log">
  <h3>Event Log</h3>
  <p>Toggle columns above to see events:</p>
</div>

<script>
const eventDemo = document.querySelector('#event-demo');
const eventLog = document.querySelector('#event-log');

if (eventDemo && eventLog) {
  eventDemo.addEventListener('table-modifiable:change', (event) => {
    const pre = document.createElement('pre');
    pre.textContent = JSON.stringify({
      column: event.detail.column,
      visible: event.detail.visible,
      timestamp: new Date().toLocaleTimeString(),
    }, null, 2);
    eventLog.appendChild(pre);

    // Keep only last 5 events
    const pres = eventLog.querySelectorAll('pre');
    if (pres.length > 5) pres[0].remove();
  });
}
</script>
Staff departments
First Name Last Name Department
Alice Anderson Engineering
Bob Brown Sales
Carol Clark Marketing

Event Log

Toggle columns above to see events:

Add an optional palette

These are the same French labels with a blue button and a light popover. Set foreground and background together. Changing labels doesn’t require changing colors.

The component injects no CSS. The custom properties below work because this page’s CSS consumes them. See the styling reference for the control selectors and a complete stylesheet.

<style>
.custom-controls {
  --table-modifiable-tool-bg: #fff;
  --table-modifiable-tool-color: #333;
  color-scheme: light;
}
.custom-controls .modification-tools-toggle {
  background: #0969da;
  color: #fff;
}
.custom-controls .modification-tools-toggle:hover {
  background: #0860ca;
}
</style>
<table-modifiable class="custom-controls" lang="fr"
  removable="Nom,Courriel,Téléphone,Adresse"
  start-with="Nom,Courriel"
  button-label="Choisir les colonnes"
  button-aria-label="Choisir les colonnes du personnel"
  tools-label="Colonnes à afficher"
  role="region" aria-label="Tableau du personnel" tabindex="0">
  <table>
    <caption>Coordonnées du personnel</caption>
    <!-- same French table as above -->
  </table>
</table-modifiable>
Coordonnées du personnel
Nom Courriel Téléphone Adresse
Alice Anderson alice@example.com 555-0101 123 Oak St
Bob Brown bob@example.com 555-0202 456 Pine Ave
Carol Clark carol@example.com 555-0303 789 Elm Rd

API Reference

For complete documentation of attributes, events, and browser support, see the README.