This page covers a next version of Handsontable, and is not published yet.

This page covers a non-latest version of Handsontable.

# MultiColumnSorting

# Description

This plugin sorts the view by columns (but does not sort the data source!). To enable the plugin, set the Options#multiColumnSorting property to the correct value (see the examples below).

Example

// as boolean
multiColumnSorting: true

// as an object with initial sort config (sort ascending for column at index 1 and then sort descending for column at index 0)
multiColumnSorting: {
  initialConfig: [{
    column: 1,
    sortOrder: 'asc'
  }, {
    column: 0,
    sortOrder: 'desc'
  }]
}

// as an object which define specific sorting options for all columns
multiColumnSorting: {
  sortEmptyCells: true, // true = the table sorts empty cells, false = the table moves all empty cells to the end of the table (by default)
  indicator: true, // true = shows indicator for all columns (by default), false = don't show indicator for columns
  headerAction: true, // true = allow to click on the headers to sort (by default), false = turn off possibility to click on the headers to sort
  compareFunctionFactory: function(sortOrder, columnMeta) {
    return function(value, nextValue) {
      // Some value comparisons which will return -1, 0 or 1...
    }
  }
}

// as an object passed to the `column` property, allows specifying a custom options for the desired column.
// please take a look at documentation of `column` property: [Options#columns](/api/options/#columns)
columns: [{
  multiColumnSorting: {
    indicator: false, // disable indicator for the first column,
    sortEmptyCells: true,
    headerAction: false, // clicks on the first column won't sort
    compareFunctionFactory: function(sortOrder, columnMeta) {
      return function(value, nextValue) {
        return 0; // Custom compare function for the first column (don't sort)
      }
    }
  }
}]

# Options

# multiColumnSorting

Source code (opens new window)

multiColumnSorting.multiColumnSorting : boolean | object

The multiColumnSorting option configures the MultiColumnSorting plugin.

You can set the multiColumnSorting option to one of the following:

Setting Description
true Enable the MultiColumnSorting plugin with the default configuration
false Disable the MultiColumnSorting plugin
An object - Enable the MultiColumnSorting plugin
- Modify the MultiColumnSorting plugin options

If you set the multiColumnSorting option to an object, you can set the following MultiColumnSorting plugin options:

Option Possible settings
indicator true: Display an arrow icon in the column header, to indicate a sortable column
false: Don't display the arrow icon in the column header
headerAction true: Enable clicking on the column header to sort the column
false: Disable clicking on the column header to sort the column
sortEmptyCells true: Sort empty cells as well
false: Place empty cells at the end
compareFunctionFactory A custom compare function

If you set the multiColumnSorting option to an object, you can also sort individual columns at Handsontable's initialization. In the multiColumnSorting 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 `MultiColumnSorting` plugin
multiColumnSorting: true

// enable the `MultiColumnSorting` plugin with custom configuration
multiColumnSorting: {
  // sort empty cells as well
  sortEmptyCells: true,
  // display an 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 `MultiColumnSorting` plugin
multiColumnSorting: {
  // at initialization, sort column 1 in ascending order
  initialConfig: {
    column: 1,
    sortOrder: 'asc'
  },
  // at initialization, sort column 2 in descending order
  initialConfig: {
    column: 2,
    sortOrder: 'desc'
  }
}

# Methods

# clearSort

Source code (opens new window)

multiColumnSorting.clearSort()

Clear the sort performed on the table.

# disablePlugin

Source code (opens new window)

multiColumnSorting.disablePlugin()

Disables the plugin functionality for this Handsontable instance.

# enablePlugin

Source code (opens new window)

multiColumnSorting.enablePlugin()

Enables the plugin functionality for this Handsontable instance.

# getSortConfig

Source code (opens new window)

multiColumnSorting.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

Source code (opens new window)

multiColumnSorting.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 than the MultiColumnSorting#enablePlugin method is called.

# isSorted

Source code (opens new window)

multiColumnSorting.isSorted() ⇒ boolean

Checks if the table is sorted (any column have to be sorted).

# setSortConfig

Source code (opens new window)

multiColumnSorting.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('multiColumnSorting');

  columnSortPlugin.setSortConfig(destinationSortConfigs);

  // const newData = ... // Calculated data set, ie. from an AJAX call.

  this.loadData(newData); // Load new 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

Source code (opens new window)

multiColumnSorting.sort(sortConfig)

Sorts the table by chosen columns and orders.

Emits: Hooks#event:beforeColumnSort, Hooks#event:afterColumnSort Example

// sort ascending first visual column
hot.getPlugin('multiColumnSorting').sort({ column: 0, sortOrder: 'asc' });

// sort first two visual column in the defined sequence
hot.getPlugin('multiColumnSorting').sort([{
  column: 1, sortOrder: 'asc'
}, {
  column: 0, sortOrder: 'desc'
}]);
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). Note: Please keep in mind that every call of sort function set an entirely new sort order. Previous sort configs aren't preserved.
Last Updated: Apr 19, 2024