Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Resize by Cell Content

Demo

Demo / Demo Component

Description

The default of Slickgrid-Universal is to fit all columns in the container viewport and for the most part that is a good resize to use and it's fast. However if your grid has a lot of columns then doing a fit to viewport is not exactly great, you have lot of columns a few of these columns will become too small (we strongly suggest adding minWidth to every column) and you'll start seeing ellipsis to a few of these columns.

The Auto-Resize by Cell Content will fix the problem described in previous paragraph, however it will come with a cost. It will read through all the cell content of all your items (because this could slow down the grid a lot, there's a default to not go over a 1000 rows but that could be overridden if you wish). For that reason the resize by cell content is an opt-in feature.

How does it work? It loops through the dataset (spread evenly when there are more items than the max count to inspect) and keeps all distinct formatted texts in the sample for each column, it then measures these texts with a canvas (using the font and CSS text transformation of a cell in that column) to find the widest one and finally adds the cell padding (read from the .slick-cell CSS). You can optionally include the column header title width (via resizeByContentOptions.includeHeaderWidth) so that a column is never narrower than its title.

There are a few configuration options you can use

  • extra padding (resizeExtraWidthPadding)

  • padding (resizeByContentOptions.cellPaddingWidthInPx, resizeByContentOptions.formatterPaddingWidthInPx)

  • max width threshold (resizeMaxWidthThreshold)

  • include header width (resizeByContentOptions.includeHeaderWidth) ... and more

Usage

defineGrid() {
  this.columns = [
    { id: 'action', field: 'action', name: 'Action', width: 50, maxWidth: 50 },
    { id: 'firstName', field: 'firstName', name: 'First Name', mindWidth: 100 },
    { id: 'lastName', field: 'lastName', name: 'First Name', mindWidth: 100, resizeExtraWidthPadding: 10 },
    // ...
  ];

  this.gridOptions = {
    // ...
    enableAutoResize: true,

    // resizing by cell content is opt-in
    // we first need to disable the 2 default flags to disable autoFit/autosize
    autoFitColumnsOnFirstLoad: false,
    enableAutoSizeColumns: false,

    // then enable resize by content with these 2 flags
    autosizeColumnsByCellContentOnFirstLoad: true,
    enableAutoResizeColumnsByCellContent: true,
  };
}

When enableAutoResizeColumnsByCellContent is enabled, DataView row changes also trigger a content recalculation after the initial resize. Rapid changes are combined into one recalculation; filtering and sorting may trigger one as well. Set resizeByContentOnlyOnFirstLoad to disable later automatic recalculations.

Resize by Content - Grid Options

There a few grid options that you can change to override the default resize options. We listed the grid options below for the resize and they should all be there but just in case, you should also check the GridOption and ResizeByContentOption interface.

and

Resize by Content - Column Options

We listed the column definition options below for the resize and they should all be there but just in case, you should also check the Column interface.

Last updated