JavaScript Data Grid Grid size

Overview

Grid size is used to define the initial size of the grid by setting the width, height and dimensions.

Setting the size

You need to define the grid's container as a starting point to initialize it. Usually, the div element becomes this container. This container should have defined dimensions as well as the rest of your layout. Handsontable supports relative units such as %, rem, em, vh, vw, and px.

Define the size in CSS styles

Both width and height could be defined as inline styles or as a CSS class property. In this case, it's important to define what should be an overflow parent properly. Handsontable looks for the closest element with overflow: auto or overflow: hidden to use it as a scrollable container. If no such element is found, a window will be used.

TIP

Handsontable doesn't observe CSS changes for containers out of the box. If you'd like to observe it, you can define the dimensions in the configuration object or create your own observer.

Pass the size in the configuration

You can pass width and height values to Handsontable as numbers or possible CSS values for the "width"/"height" properties:

{
  width: '100px',
  height: '100px',
}

or

{
  width: '75%',
  height: '75%',
}

or

{
  width: 100,
  height: 100,
}

These dimensions will be set as inline styles in a container element, and overflow: hidden will be added automatically.

If container is a block element, then its parent has to have defined height. By default block element is 0px height, so 100% from 0px is still 0px.

Changes called in updateSettings() will re-render the grid with the new properties.

What if the size is not set

If you don't define any dimensions, Handsontable generates as many rows and columns as needed to fill the available space.

If the grid's content doesn't fit inside the viewport, the browser's native scrollbars are used for scrolling. For this to work properly, Handsontable's layout direction (e.g., layoutDirection: 'rtl') must be the same as your HTML document's layout direction (<html dir='rtl'>). Otherwise, horizontal scrolling doesn't work.

Autoresizing

Handsontable observes window resizing. If the window's dimensions have changed, then we check if Handsontable should resize itself too. Due to the performance issue, we use the debounce method to respond on window resize.

You can easily overwrite this behaviour by returning false in the beforeRefreshDimensions hook.

{
  beforeRefreshDimensions() { return false; }
}

Manual resizing

The Handsontable instance exposes the refreshDimensions() method, which helps you to resize grid elements properly.

hot.refreshDimensions();

You can listen for two hooks, beforeRefreshDimensions and afterRefreshDimensions.