Column Visibility Plugin
The Visibility plugin gives users control over which columns are displayed. Hide less important columns by default, let users toggle them via a column chooser UI, or programmatically show/hide columns based on user preferences or screen size.
💡 Optional Enhancement: When ReorderPlugin is also loaded, columns in the visibility panel become draggable for reordering.
Installation
Section titled “Installation”import '@toolbox-web/grid/features/visibility';Basic Usage
Section titled “Basic Usage”Set hidden: true on columns you want hidden by default. The feature provides a UI and API for toggling column visibility at runtime.
import { queryGrid } from '@toolbox-web/grid';
const grid = queryGrid('tbw-grid');grid.gridConfig = { columns: [ { field: 'id', header: 'ID' }, { field: 'name', header: 'Name' }, { field: 'email', header: 'Email' }, { field: 'phone', header: 'Phone', hidden: true }, // Hidden by default { field: 'address', header: 'Address', hidden: true }, ], features: { visibility: true },};
// Toggle visibility programmaticallyconst plugin = grid.getPluginByName('visibility');plugin.showColumn('phone');plugin.hideColumn('email');import '@toolbox-web/grid-react/features/visibility';import { DataGrid } from '@toolbox-web/grid-react';
function ConfigurableGrid({ data }) { return ( <DataGrid rows={data} columns={[ { field: 'id', header: 'ID' }, { field: 'name', header: 'Name' }, { field: 'email', header: 'Email' }, { field: 'phone', header: 'Phone', hidden: true }, ]} visibility /> );}<script setup>import '@toolbox-web/grid-vue/features/visibility';import { TbwGrid, TbwGridColumn } from '@toolbox-web/grid-vue';
const data = [ { id: 1, name: 'Alice', email: 'alice@example.com', phone: '555-1234' }, { id: 2, name: 'Bob', email: 'bob@example.com', phone: '555-5678' },];</script>
<template> <TbwGrid :rows="data" visibility> <TbwGridColumn field="id" header="ID" /> <TbwGridColumn field="name" header="Name" /> <TbwGridColumn field="email" header="Email" /> <TbwGridColumn field="phone" header="Phone" :hidden="true" /> </TbwGrid></template>// Feature import - enables the [visibility] inputimport '@toolbox-web/grid-angular/features/visibility';
import { Component } from '@angular/core';import { Grid } from '@toolbox-web/grid-angular';import type { ColumnConfig } from '@toolbox-web/grid';
@Component({ selector: 'app-configurable-grid', imports: [Grid], template: ` <tbw-grid [rows]="rows" [columns]="columns" [visibility]="true" style="height: 400px; display: block;"> </tbw-grid> `,})export class ConfigurableGridComponent { rows = [];
columns: ColumnConfig[] = [ { field: 'id', header: 'ID' }, { field: 'name', header: 'Name' }, { field: 'email', header: 'Email' }, { field: 'phone', header: 'Phone', hidden: true }, ];}Toggle the controls to explore hidden columns, locked columns, and the allowHideAll option. Click the column visibility icon in the header to open the panel.
<tbw-grid style="height: 400px;"></tbw-grid>import '@toolbox-web/grid';import { queryGrid } from '@toolbox-web/grid';import '@toolbox-web/grid/features/visibility';
const allColumns = [ { field: 'id', header: 'ID', type: 'number' as const }, { field: 'name', header: 'Name' }, { field: 'email', header: 'Email' }, { field: 'department', header: 'Department' }, { field: 'salary', header: 'Salary', type: 'number' as const },];
const sampleData = [ { id: 1, name: 'Alice', email: 'alice@example.com', department: 'Engineering', salary: 95000 }, { id: 2, name: 'Bob', email: 'bob@example.com', department: 'Marketing', salary: 75000 }, { id: 3, name: 'Carol', email: 'carol@example.com', department: 'Engineering', salary: 105000 }, { id: 4, name: 'David', email: 'david@example.com', department: 'Sales', salary: 65000 }, { id: 5, name: 'Eve', email: 'eve@example.com', department: 'HR', salary: 70000 },];
const grid = queryGrid('tbw-grid')!;
function rebuild(opts: Record<string, unknown>) { const hiddenArr = (opts.hiddenColumns as string[]) ?? ['email']; const lockedArr = (opts.lockedColumns as string[]) ?? ['id'];
grid.gridConfig = { columns: allColumns.map((col) => ({ ...col, hidden: hiddenArr.includes(col.field), lockVisible: lockedArr.includes(col.field), })), features: { visibility: { allowHideAll: (opts.allowHideAll as boolean) ?? false } }, }; grid.rows = sampleData;}
rebuild({ allowHideAll: false, hiddenColumns: ['email'], lockedColumns: ['id'] });Configuration Options
Section titled “Configuration Options”See VisibilityConfig for the full list of options and defaults.
Programmatic API
Section titled “Programmatic API”const plugin = grid.getPluginByName('visibility');
// Panel controlplugin.show(); // Open the visibility panelplugin.hide(); // Close the panelplugin.toggle(); // Toggle panel open/closedconst open = plugin.isPanelVisible(); // Check if panel is open
// Column visibilityplugin.hideColumn('email');plugin.showColumn('email');plugin.toggleColumn('email');plugin.showAll(); // Show all hidden columnsplugin.setColumnVisible('email', false); // Set visibility directlyconst isVis = plugin.isColumnVisible('email');
// Query stateconst hidden = plugin.getHiddenColumns(); // List of hidden field namesconst visible = plugin.getVisibleColumns(); // List of visible field namesconst all = plugin.getAllColumns(); // Full column metadata with visibilityStyling
Section titled “Styling”The visibility panel supports CSS custom properties for theming. Override these on tbw-grid or a parent container:
CSS Custom Properties
Section titled “CSS Custom Properties”| Property | Default | Description |
|---|---|---|
--tbw-visibility-hover | var(--tbw-color-row-hover) | Row hover background |
--tbw-panel-padding | var(--tbw-spacing-md, 0.5rem) | Panel content padding |
--tbw-panel-gap | var(--tbw-spacing-md, 0.5rem) | Gap between items |
--tbw-menu-item-padding | 0.375rem 0.25rem | Row padding |
--tbw-font-size-sm | 0.8125rem | Label font size |
--tbw-font-size-2xs | 0.625rem | Drag handle size |
--tbw-color-fg-muted | #888 | Muted text (locked items) |
--tbw-border-radius | 0.25em | Row border radius |
Example
Section titled “Example”tbw-grid { /* Custom visibility panel styling */ --tbw-visibility-hover: #e8f4fd; --tbw-panel-padding: 1rem; --tbw-panel-gap: 0.75rem;}CSS Classes
Section titled “CSS Classes”The visibility panel uses these class names for advanced customization:
| Class | Element |
|---|---|
.tbw-visibility-content | Panel container |
.tbw-visibility-list | Scrollable column list |
.tbw-visibility-row | Individual column row |
.tbw-visibility-row.locked | Non-toggleable column |
.tbw-visibility-row.reorderable | Can be dragged (with ReorderPlugin) |
.tbw-visibility-row.dragging | Currently being dragged |
.tbw-visibility-handle | Drag handle element |
.tbw-visibility-label | Checkbox + text wrapper |
See Also
Section titled “See Also”- Column Reorder — Drag-to-reorder columns
- Pinned Columns — Sticky columns