Skip to content

Column Hide

RevoGrid provides the ability to hide specific columns in your grid, allowing you to focus on the most relevant data for your users. This feature is particularly useful when working with large datasets where not all columns need to be visible at once.

Source code
import { defineCustomElements } from '@revolist/revogrid/loader';
import { ColumnHidePlugin, RowOddPlugin } from '@revolist/revogrid-pro';
import { currentTheme } from '../composables/useRandomData';
defineCustomElements();
export function load(parentSelector: string) {
const grid = document.createElement('revo-grid');
const { isDark } = currentTheme();
// Sample data
grid.source = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 },
{ name: 'Bob', age: 35 },
];
// Define columns
grid.columns = [
{ prop: 'name', name: 'Name' },
{ prop: 'age', name: 'Age' },
];
// Set grid properties
grid.className = 'cell-border';
grid.theme = isDark() ? 'darkMaterial' : 'material';
grid.style.minHeight = '200px';
grid.style.width = '100%';
grid.hideAttribution = true;
grid.plugins = [ColumnHidePlugin, RowOddPlugin];
// Track hidden columns
let hiddenColumns: string[] = [];
// Create checkboxes for column visibility
const controls = document.createElement('div');
controls.className = 'controls';
const checkboxGroup = document.createElement('div');
checkboxGroup.className = 'checkbox-group';
['name', 'age'].forEach(prop => {
const label = document.createElement('label');
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.checked = true;
checkbox.addEventListener('change', () => {
const index = hiddenColumns.indexOf(prop);
if (index === -1) {
hiddenColumns.push(prop);
} else {
hiddenColumns.splice(index, 1);
}
// Update the grid's additionalData
grid.additionalData = { hiddenColumns };
});
label.appendChild(checkbox);
label.appendChild(document.createTextNode(prop === 'name' ? 'Name' : 'Age'));
checkboxGroup.appendChild(label);
});
controls.appendChild(checkboxGroup);
// Create container
const container = document.createElement('div');
container.className = 'column-hide-example';
container.appendChild(controls);
container.appendChild(grid);
document.querySelector(parentSelector)?.appendChild(container);
}

Column hiding is a feature that allows you to hide specific columns in your grid, making them invisible to users. This is useful for:

  • Focusing on specific data points
  • Reducing visual clutter
  • Creating different views of the same dataset
  • Implementing column visibility toggles

The ColumnHidePlugin supports two main approaches for hiding columns:

Using the hide-column Attribute

You can specify which columns to hide using the hide-column attribute on your RevoGrid element:

<!-- Vue/Svelte -->
<revo-grid hide-column="1,2,3"></revo-grid>
<!-- or with dynamic binding -->
<revo-grid :hide-column="[1,2,3]"></revo-grid>
<!-- React -->
<RevoGrid hideColumn="1,2,3" />
<!-- Angular -->
<revo-grid [hideColumn]="[1,2,3]"></revo-grid>

Using additionalData

You can also hide columns programmatically using the additionalData property:

const grid = document.querySelector('revo-grid');
grid.additionalData = {
hiddenColumns: [1, 2, 3] // Hide columns with indices 1, 2, and 3
};

This approach is particularly useful when you need to dynamically update which columns are hidden based on user interactions or application state.

Events

The ColumnHidePlugin emits a hiddencolumnsupdated event whenever the hidden columns are updated:

grid.addEventListener('hiddencolumnsupdated', (event) => {
const { hiddenColumns } = event.detail;
console.log('Hidden columns updated:', hiddenColumns);
});

This event can be used to synchronize the UI with the current state of hidden columns.

Implementation Details

The ColumnHidePlugin works by:

  1. Tracking which columns should be hidden in a Set
  2. Updating the column visibility when the configuration changes
  3. Preserving the hidden state when columns are updated
  4. Supporting both attribute-based and programmatic configuration