Columns
Configure column widths, constraints, reordering, pinning, and visibility.
ZenGrid provides comprehensive column management with support for resizing, reordering, pinning, and visibility control.
Column Definition
Define columns with the ColumnDef interface:
const grid = new ZenGrid(container, { columns: [ { id: 'name', field: 'name', header: 'Name', width: 200, minWidth: 100, maxWidth: 400, sortable: true, filterable: true, resizable: true, reorderable: true, editable: true, renderer: 'text', editor: 'text' } ]});ColumnDef Interface
interface ColumnDef { id: string; // Unique column identifier field: string; // Data field to display header: string; // Header text width?: number; // Column width in pixels minWidth?: number; // Minimum width constraint maxWidth?: number; // Maximum width constraint sortable?: boolean; // Enable sorting filterable?: boolean; // Enable filtering resizable?: boolean; // Enable resizing reorderable?: boolean; // Enable reordering editable?: boolean; // Enable editing renderer?: string; // Cell renderer type editor?: string; // Cell editor type}The id and field properties are required. All other properties are optional with sensible defaults.
Column Resizing
Enable Resizing
// Enable resizing on specific columns{ id: 'name', field: 'name', resizable: true, minWidth: 100, maxWidth: 500}
// Attach resize handles (done automatically by default)grid.attachColumnResize(headerElement);Programmatic Resizing
// Resize a specific columngrid.resizeColumn('name', 300);
// Auto-fit a column to its contentgrid.autoFitColumn('name');
// Auto-fit all columnsgrid.autoFitAllColumns();
// Set width constraintsgrid.setColumnConstraints('name', { minWidth: 150, maxWidth: 450});Use autoFitColumn() to automatically size columns based on content. This is especially useful after data changes.
Resize Events
grid.on('column:resize', (event) => { console.log('Column resized:', { column: event.columnId, oldWidth: event.oldWidth, newWidth: event.newWidth });});
grid.on('column:resizeStart', (event) => { console.log('Resize started:', event.columnId);});
grid.on('column:resizeEnd', (event) => { console.log('Resize ended:', event.columnId);});Column Reordering
Enable Reordering
const grid = new ZenGrid(container, { enableColumnDrag: true, columns: [ { id: 'name', field: 'name', reorderable: true } ]});
// Attach drag handlesgrid.attachColumnDrag(headerElement);Programmatic Reordering
// Move a column to a new positiongrid.moveColumn('name', 2); // Move to index 2
// Swap two columnsgrid.swapColumns('name', 'age');
// Get current column orderconst order = grid.getColumnOrder();// Returns: ['id', 'name', 'age', 'email']
// Set column ordergrid.setColumnOrder(['email', 'name', 'id', 'age']);Column reordering does not modify the underlying data. It only changes the visual order of columns.
Reorder Events
grid.on('column:reorder', (event) => { console.log('Column reordered:', { column: event.columnId, fromIndex: event.fromIndex, toIndex: event.toIndex });});Column Pinning
Pin columns to the left or right side of the grid:
// Pin a column to the leftgrid.pinColumn('name', 'left');
// Pin a column to the rightgrid.pinColumn('actions', 'right');
// Unpin a columngrid.unpinColumn('name');
// Check if a column is pinnedconst isPinned = grid.isColumnPinned('name');const pinSide = grid.getColumnPinSide('name'); // 'left' | 'right' | null
// Get all pinned columnsconst leftPinned = grid.getPinnedColumns('left');const rightPinned = grid.getPinnedColumns('right');Pinned columns remain visible during horizontal scrolling, making them ideal for key identifier columns or action buttons.
Column Visibility
Show and hide columns dynamically:
// Hide a columngrid.hideColumn('email');
// Show a columngrid.showColumn('email');
// Toggle column visibilitygrid.toggleColumn('email');
// Check if a column is visibleconst isVisible = grid.isColumnVisible('email');
// Get all visible columnsconst visibleColumns = grid.getVisibleColumns();
// Get all hidden columnsconst hiddenColumns = grid.getHiddenColumns();
// Set multiple columns' visibility at oncegrid.setColumnsVisible(['name', 'age'], true);grid.setColumnsVisible(['internal_id', 'debug_field'], false);Column State Management
Save and restore complete column state:
// Get column state snapshotconst columnState = grid.getColumnState();// Returns: [// { id: 'name', width: 200, visible: true, order: 0, pinned: 'left' },// { id: 'age', width: 100, visible: true, order: 1, pinned: null },// ...// ]
// Apply column stategrid.applyColumnState(columnState, { applyWidth: true, applyVisibility: true, applyOrder: true, applyPinned: true});
// Persist to localStoragelocalStorage.setItem('gridColumnState', JSON.stringify(columnState));
// Restore from localStorageconst savedState = JSON.parse(localStorage.getItem('gridColumnState'));if (savedState) { grid.applyColumnState(savedState);}Use column state management to preserve user preferences across sessions.
ColumnStateSnapshot Interface
interface ColumnStateSnapshot { id: string; width: number; visible: boolean; order: number; pinned: 'left' | 'right' | null;}Full Grid State
Save and restore complete grid state including columns, sorting, and filtering:
// Get complete grid stateconst gridState = grid.getStateSnapshot();// {// columns: [...],// sort: [...],// filters: {...},// pagination: {...}// }
// Apply complete grid stategrid.applyStateSnapshot(gridState);
// Persist complete statelocalStorage.setItem('gridState', JSON.stringify(gridState));
// Restore complete stateconst savedGridState = JSON.parse(localStorage.getItem('gridState'));if (savedGridState) { grid.applyStateSnapshot(savedGridState);}Example: Column Chooser
Create a column visibility chooser:
function createColumnChooser(grid) { const allColumns = grid.getColumns(); const container = document.createElement('div'); container.className = 'column-chooser';
allColumns.forEach(col => { const label = document.createElement('label'); const checkbox = document.createElement('input'); checkbox.type = 'checkbox'; checkbox.checked = grid.isColumnVisible(col.id);
checkbox.addEventListener('change', () => { if (checkbox.checked) { grid.showColumn(col.id); } else { grid.hideColumn(col.id); } });
label.appendChild(checkbox); label.appendChild(document.createTextNode(col.header)); container.appendChild(label); });
return container;}
// Add to UIdocument.getElementById('column-chooser-container') .appendChild(createColumnChooser(grid));Example: Responsive Columns
Adjust columns based on viewport size:
function adjustColumnsForViewport() { const width = window.innerWidth;
if (width < 768) { // Mobile: show only essential columns grid.hideColumn('description'); grid.hideColumn('created_date'); grid.hideColumn('modified_date'); } else if (width < 1024) { // Tablet: show more columns grid.showColumn('description'); grid.hideColumn('modified_date'); } else { // Desktop: show all columns grid.showColumn('description'); grid.showColumn('created_date'); grid.showColumn('modified_date'); }}
window.addEventListener('resize', adjustColumnsForViewport);adjustColumnsForViewport();Combine column visibility with media queries to create responsive grid layouts that adapt to different screen sizes.