ColumnSorting
Options
columnSorting
columnSorting.columnSorting : boolean | object
The columnSorting option configures the ColumnSorting plugin.
You can set the columnSorting option to one of the following:
| Setting | Description |
|---|---|
true | Enable the ColumnSorting plugin with the default configuration |
false | Disable the ColumnSorting plugin |
| An object | - Enable the ColumnSorting plugin- Modify the ColumnSorting plugin options |
If you set the columnSorting option to an object,
you can set the following ColumnSorting plugin options:
| Option | Possible settings |
|---|---|
indicator | true: Display the arrow icon in the column header, to indicate a sortable columnfalse: Don’t display the arrow icon in the column header |
headerAction | true: Enable clicking on the column header to sort the columnfalse: Disable clicking on the column header to sort the column |
sortEmptyCells | true: Sort empty cells as wellfalse: Place empty cells at the end |
compareFunctionFactory | A custom compare function |
If you set the columnSorting option to an object,
you can also sort individual columns at Handsontable’s initialization.
In the columnSorting object, add an object named initialConfig,
with the following properties:
| Option | Possible settings | Description |
|---|---|---|
column | A number | The index of the column that you want to sort at initialization |
sortOrder | 'asc' | 'desc' | The sorting order:'asc': ascending'desc': descending |
Read more:
Default: undefined
Example
// enable the `ColumnSorting` plugincolumnSorting: true
// enable the `ColumnSorting` plugin with custom configurationcolumnSorting: { // sort empty cells as well sortEmptyCells: true, // display the arrow icon in the column header indicator: true, // disable clicking on the column header to sort the column headerAction: false, // add a custom compare function compareFunctionFactory(sortOrder, columnMeta) { return function(value, nextValue) { // some value comparisons which will return -1, 0 or 1... } }}
// enable the `ColumnSorting` plugin with an initial sort order:// sort column 1 in ascending order at initializationcolumnSorting: { initialConfig: { column: 1, sortOrder: 'asc' }}Members
PLUGIN_KEY
ColumnSorting.PLUGIN_KEY
Returns the plugin key used to identify this plugin in Handsontable settings.
PLUGIN_PRIORITY
ColumnSorting.PLUGIN_PRIORITY
Returns the priority order used to determine the order in which plugins are initialized.
Methods
clearSort
columnSorting.clearSort()
Clear the sort performed on the table.
destroy
columnSorting.destroy()
Destroys the plugin instance.
disablePlugin
columnSorting.disablePlugin()
Disables the plugin functionality for this Handsontable instance.
enablePlugin
columnSorting.enablePlugin()
Enables the plugin functionality for this Handsontable instance.
getSortConfig
columnSorting.getSortConfig([column]) ⇒ undefined | object | Array
Get sort configuration for particular column or for all sorted columns. Objects contain column and sortOrder properties.
Note: Please keep in mind that returned objects expose visual column index under the column key. They are handled by the sort function.
| Param | Type | Description |
|---|---|---|
| [column] | number | optional Visual column index. |
isEnabled
columnSorting.isEnabled() ⇒ boolean
Checks if the plugin is enabled in the Handsontable settings. This method is executed in Hooks#beforeInit
hook and if it returns true then the ColumnSorting#enablePlugin method is called.
isSorted
columnSorting.isSorted() ⇒ boolean
Checks if the table is sorted (any column have to be sorted).
setSortConfig
columnSorting.setSortConfig(sortConfig)
Warn: Useful mainly for providing server side sort implementation (see in the example below). It doesn’t sort the data set. It just sets sort configuration for all sorted columns. Note: Please keep in mind that this method doesn’t re-render the table.
Example
beforeColumnSort: function(currentSortConfig, destinationSortConfigs) { const columnSortPlugin = this.getPlugin('columnSorting');
columnSortPlugin.setSortConfig(destinationSortConfigs);
// const newData = ... // Calculated data set, ie. from an AJAX call.
this.updateData(newData); // Update data set and re-render the table.
return false; // The blockade for the default sort action.}| Param | Type | Description |
|---|---|---|
| sortConfig | undefined object Array | Single column sort configuration or full sort configuration (for all sorted columns). The configuration object contains column and sortOrder properties. First of them contains visual column index, the second one contains sort order (asc for ascending, desc for descending). |
sort
columnSorting.sort(sortConfig)
Sorts the table by chosen columns and orders.
Emits: Hooks#event:beforeColumnSort, Hooks#event:afterColumnSort
Example
// sort ascending first visual columnhot.getPlugin('columnSorting').sort({ column: 0, sortOrder: 'asc' });| Param | Type | Description |
|---|---|---|
| sortConfig | undefined object | Single column sort configuration. The configuration object contains column and sortOrder properties. First of them contains visual column index, the second one contains sort order (asc for ascending, desc for descending). Note: Please keep in mind that every call of sort function set an entirely new sort order. Previous sort configs aren’t preserved. |