Skip to content

Column Stretch

RevoGrid provides the ability to automatically adjust the width of columns to fit the tableโ€™s width. This is particularly useful when you have fewer columns than needed to enable the horizontal scrollbar, and you want to distribute the available space evenly or according to specific criteria.

Source code
src/components/column-stretch/columnStretch.ts
import { defineCustomElements } from '@revolist/revogrid/loader';
defineCustomElements();
import { currentTheme, useRandomData } from '../composables/useRandomData';
const { createRandomData } = useRandomData();
const { isDark } = currentTheme();
import { ColumnStretchPlugin } from '@revolist/revogrid-pro';
function createForm(container: HTMLElement, onStretchChange: (value: string) => void) {
const form = document.createElement('form');
form.id = 'stretch';
const options = [
{ value: 'all', label: 'All', checked: true },
{ value: 'last', label: 'Last' },
{ value: '2nd', label: '2nd' },
{ value: 'none', label: 'None' }
];
options.forEach(option => {
const label = document.createElement('label');
const input = document.createElement('input');
input.type = 'radio';
input.name = 'stretch';
input.value = option.value;
input.checked = option.checked || false;
input.addEventListener('change', () => onStretchChange(option.value));
label.appendChild(input);
label.appendChild(document.createTextNode(' ' + option.label));
form.appendChild(label);
});
container.appendChild(form);
}
function createGrid(container: HTMLElement) {
const grid = document.createElement('revo-grid');
grid.setAttribute('resize', '');
grid.setAttribute('hide-attribution', '');
grid.style.minHeight = '250px';
container.appendChild(grid);
return grid;
}
const container = document.querySelector('.not-content') as HTMLElement;
if (container) {
// Create form and grid elements
const grid = createGrid(container);
createForm(container, (value) => {
grid.additionalData = {
stretch: value === '2nd' ? 1 : value
};
});
// Configure grid
grid.source = createRandomData(100);
grid.columns = [
{
name: '๐Ÿ†” ID',
prop: 'id',
},
{
name: '๐ŸŽ Fruit',
prop: 'name',
},
{
name: '๐Ÿ’ฐ Price',
prop: 'price',
},
];
grid.additionalData = {
stretch: 'all'
};
grid.plugins = [ColumnStretchPlugin] as any;
grid.theme = isDark() ? 'darkCompact' : 'compact';
}

In this article, weโ€™ll walk you through the process of implementing column stretching in RevoGrid using the ColumnStretchPlugin. Weโ€™ll also explore the different configurations available for stretching the columns.

What is it?

Column stretching is a feature which allows you to automatically adjust the width of columns to fit the entire width of the table. The width of each column is calculated based on the number of columns and their respective sizes, ensuring the gridโ€™s width is fully utilized. This feature is especially useful when working with a small number of columns.

Stretch Configurations

Important Note

The Column Stretch plugin may conflict with other column size configurations. The following cases will prevent column stretching:

  • Columns with explicit size property set
  • Columns with autoSize enabled
  • When the total width of all columns exceeds the available space (negative remaining width)

In these cases, the stretch configuration will be ignored for those specific columns to preserve their intended sizing behavior.

RevoGrid supports different types of stretch configurations through the StretchConfig type:

  • 'none': No column stretching will be applied. Columns retain their default width.

  • 'last': Only the last column will stretch to fill the remaining width.

  • 'all': All columns will stretch evenly to fill the tableโ€™s width.

  • number: A specific column (by index) will stretch to fill the remaining width.

  • To stretch only the last column:

    grid.additionalData = { stretch: 'last' };
  • To stretch a specific column by index (e.g., the second column):

    grid.additionalData = { stretch: 1 };
  • To stretch all columns evenly:

    grid.additionalData = { stretch: 'all' };

By using the ColumnStretchPlugin, you can easily manage the width of columns in RevoGrid, making sure your table layout looks clean and is fully utilized. Whether you want to stretch all columns evenly or just the last one, this plugin offers flexibility to suit your needs.

Experiment with the different stretch configurations to see which works best for your data grid!