This plugin allows to set column widths based on their widest cells.
By default, the plugin is declared as undefined, which makes it enabled (same as if it was declared as true).
Enabling this plugin may decrease the overall table performance, as it needs to calculate the widths of all cells to
resize the columns accordingly.
If you experience problems with the performance, try turning this feature off and declaring the column widths manually.
Column width calculations are divided into sync and async part. Each of this parts has their own advantages and
disadvantages. Synchronous calculations are faster but they block the browser UI, while the slower asynchronous
operations don't block the browser UI.
To configure the sync/async distribution, you can pass an absolute value (number of columns) or a percentage value to a config object:
// as a number (300 columns in sync, rest async)
autoColumnSize: {syncLimit: 300},
// as a string (percent)
autoColumnSize: {syncLimit: '40%'},
To configure this plugin see Options#autoColumnSize.
Example
const hot = new Handsontable(document.getElementById('example'), {
date: getData(),
autoColumnSize: true
});
// Access to plugin instance:
const plugin = hot.getPlugin('autoColumnSize');
plugin.getColumnWidth(4);
if (plugin.isEnabled()) {
// code...
}
Members
-
inProgressBoolean
-
trueif the size calculation is in progress. -
widthsArray.<Number>
-
Cached columns widths.
Methods
-
calculateAllColumnsWidth(rowRange)
-
Calculates all columns width. The calculated column will be cached in the
AutoColumnSize#widthsproperty.
To retrieve width for specyfied column useAutoColumnSize#getColumnWidthmethod.Parameters:
Name Type Description rowRangeObject | Number Row index or an object with
fromandtoproperties which define row range. -
calculateColumnsWidth(colRange, rowRange, force)
-
Calculates a columns width.
Parameters:
Name Type Default Description colRangeNumber | Object Column index or an object with
fromandtoindexes as a range.rowRangeNumber | Object Row index or an object with
fromandtoindexes as a range.forceBoolean false optional If
truethe calculation will be processed regardless of whether the width exists in the cache. -
clearCache(columns)
-
Clears cache of calculated column widths. If you want to clear only selected columns pass an array with their indexes.
Otherwise whole cache will be cleared.Parameters:
Name Type Description columnsArray.<Number> optional List of physical column indexes to clear.
-
destroy()
-
Destroys the plugin instance.
-
disablePlugin()
-
Disables the plugin functionality for this Handsontable instance.
-
enablePlugin()
-
Enables the plugin functionality for this Handsontable instance.
-
getColumnWidth(column, defaultWidth, keepMinimum){Number}
-
Gets the calculated column width.
Parameters:
Name Type Default Description columnNumber Column index.
defaultWidthNumber optional Default column width. It will be picked up if no calculated width found.
keepMinimumBoolean true optional If
truethen returned value won't be smaller then 50 (default column width).Returns: {Number}
-
getFirstVisibleColumn(){Number|null}
-
Gets the first visible column.
Returns: {Number|null} Returns column index, -1 if table is not rendered or null if there are no columns to base the the calculations on.
-
getLastVisibleColumn(){Number}
-
Gets the last visible column.
Returns: {Number} Returns column index or -1 if table is not rendered.
-
getSyncCalculationLimit(){Number}
-
Gets value which tells how many columns should be calculated synchronously (rest of the columns will be calculated
asynchronously). The limit is calculated based onsyncLimitset toautoColumnSizeoption (seeOptions#autoColumnSize).Returns: {Number}
-
isEnabled(){Boolean}
-
Checks if the plugin is enabled in the handsontable settings. This method is executed in
Hooks#beforeInit
hook and if it returnstruethan theAutoColumnSize#enablePluginmethod is called.Returns: {Boolean}
-
isNeedRecalculate(){Boolean}
-
Checks if all widths were calculated. If not then return
true(need recalculate).Returns: {Boolean}
-
recalculateAllColumnsWidth()
-
Recalculates all columns width (overwrite cache values).
-
updatePlugin()
-
Updates the plugin state. This method is executed when
Core#updateSettingsis invoked.