Skip to content

colVis ​

Adds a Columns button to the toolbar. Its panel lists every column with a checkbox; the badge shows how many are visible.

js
import { Datagrid, column, colVis } from "@akinozgen17/datagrids";
import { agents, statusLabels, statusTones } from "./data.js";

new Datagrid("#mod-colvis-basic", {
  label: "Field agents (column menu)",
  columns: [
    column.text("ref", { title: "Ref" }),
    column.text("name", { title: "Name", hideable: false }),
    column.text("unit", { title: "Unit" }),
    column.text("task", { title: "Task", visible: false }),
    column.text("place", { title: "Place", visible: false }),
    column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
    column.money("amount", { title: "Amount", currency: "USD", locale: "en-US" }),
    column.date("date", { title: "Date" }),
  ],
  data: agents(60),
  pageSize: 10,
  use: [colVis()],
});
html
<table id="mod-colvis-basic"></table>

When to use it ​

Use it when a list has more columns than most people need at once: define the rarely used ones with visible: false and let users opt in. Combine it with stateSave so the choice survives the next visit. Skip it on small grids where every column matters.

Usage ​

js
import { Datagrid, column, colVis } from "@akinozgen17/datagrids";

new Datagrid("#agents", {
  label: "Field agents",
  columns: [
    column.text("name", { title: "Name", hideable: false }), // locked: always visible
    column.text("unit", { title: "Unit" }),
    column.text("task", { title: "Task", visible: false }),  // hidden until the user opts in
  ],
  data: rows,
  use: [colVis()],
});

Move the button to the end of the toolbar and rename it:

js
colVis({ slot: "end", messages: { button: "Fields" } });

Options ​

OptionTypeDefaultDescription
searchAbovenumber12Shows a "Find column…" field in the panel when the menu lists more columns than this.
slot"start" | "end""start"Toolbar slot.
ordernumber10Position inside the slot (lower first).
messagesPartial<ColVisMessages>locale (en / tr)button, legend, showAll, reset, search, searchLabel, noMatch, locked.
canbooleantruePermission flag: false → no button.

Column options that matter here: visible (initial visibility, also what "Reset to default" restores) and hideable: false (the box is disabled and shows a lock).

API added to the grid ​

Available as grid.colVis (typed: grid.module(colVis)).

MemberReturnsDescription
open()voidOpens the panel and focuses the first checkbox (or the search field).
close()voidCloses the panel and returns focus to the button.
toggle()voidOpens or closes.
reset()voidRestores the visibility from the column definitions.
count(){ visible, total }Visible / total of the columns the menu lists.

To change visibility from code, use the core method grid.setColumnVisible(key, visible).

Events ​

None of its own. Every change emits the core columnchange event.

Pipelines it plugs into ​

  • Toolbar: start slot, order 10.
  • State: nothing of its own — hidden columns are part of the core state that stateSave stores.

Works with ​

  • Enhanced by stateSave (remembers the choice) and button.resetState.
  • Export: excel, csv and print include visible columns only.
  • Server grids: hidden columns are still sent in columns[], in their original order, so index-based controllers keep working.

Accessibility ​

  • The button's accessible name includes the count ("Columns 5/6"), even when a narrow grid hides the text. It has aria-haspopup="dialog" and aria-controls.
  • The panel is a labelled role="dialog" with a <fieldset>/<legend> of native checkboxes. Locked columns are disabled and say why ("This column can't be hidden").
  • Keys: arrows, Home and End move between checkboxes; Space toggles; ↓ in the search field jumps to the list; Esc clears the search, then closes; Tab past either end closes the panel and returns to the button.

Replaces in DataTables ​

DataTablesdatagrids
Buttons colvis button (1.10 / 2.x / 3.x), the retired ColVis extension (1.10)colVis()
columns.visible: falsevisible: false on the column
className: "noVis" + columns: ":not(.noVis)"hideable: false
colvisRestore button"Reset to default" in the panel, grid.colVis.reset()

Gotchas ​

  • A column is missing from the menu. Columns without a title, and the special select / actions / row-number columns, are not listed.
  • A checkbox can't be unchecked. The column has hideable: false; the row shows a lock.
  • A hidden column is not in the Excel file. Exports follow what is visible; set the column visible (or use exportValue on a visible one).
  • Only the icon and "5/6" are visible. On narrow grids the button text collapses; the accessible name and the tooltip keep the full label.

Released under the MIT License.