Dropdown Menu

This page shows how to use and configure dropdown menu with Handsontable:

Dropdown Menu with default options

Tu run the basic configuration of the Dropdown Menu, just set the dropdownMenu option to true.

 
A
B
C
D
E
1
KiaNissanToyotaHonda
2
200810111213
3
200920111413
4
201030151213
5
 
A
B
C
D
E
 
1
2
3
4
5
 

function getData() {
  return [
    ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
    ['2008', 10, 11, 12, 13],
    ['2009', 20, 11, 14, 13],
    ['2010', 30, 15, 12, 13]
  ];
}
var example1 = document.getElementById('example1'),
  settings1,
  hot1;

settings1 = {
  data: getData(),
  startRows: 5,
  startCols: 5,
  minRows: 5,
  minCols: 5,
  maxRows: 10,
  maxCols: 10,
  rowHeaders: true,
  colHeaders: true, // If you use `dropdownMenu` option you must enable colHeaders
  minSpareRows: 1,
  dropdownMenu: true
};
hot1 = new Handsontable(example1, settings1);

Dropdown Menu with specific options

You can limit options available in the dropdown menu using dropdownMenu option as an array of strings.

List of commands below are common for context menu and for dropdown menu.

  • row_above (designed for Context Menu)
  • row_below (designed for Context Menu)
  • hsep1
  • col_left (designed for both menus)
  • col_right (designed for both menus)
  • hsep2
  • remove_row (designed for Context Menu)
  • remove_col (designed for both menus)
  • hsep3
  • undo (designed for Context Menu)
  • redo (designed for Context Menu)
  • make_read_only (designed for both menus)
  • alignment (designed for both menus)
  • clear_column (designed for Dropdown Menu)
  • borders (only with Custom Borders turned on)
  • commentsAddEdit, commentsRemove (only with Comments turned on)
 
A
B
C
D
E
1
KiaNissanToyotaHonda
2
200810111213
3
200920111413
4
201030151213
5
 
A
B
C
D
E
 
1
2
3
4
5
 

var example2 = document.getElementById('example2');
var settings2 = {
  data: getData(),
  startRows: 5,
  startCols: 5,
  rowHeaders: true,
  colHeaders: true, // If you use `dropdownMenu` option you must enable colHeaders
  minSpareRows: 1,
  dropdownMenu: ['col_left', 'col_right']
};
var hot2 = new Handsontable(example2, settings2);

Dropdown Menu with with fully custom configuration

For greatest configurability, you use dropdownMenu option as a configuration object as described in jQuery contextMenu documentation.

This example shows how to set custom text, how to disable "Remove row" and "Insert row above" for the first row and how to add your own option.

 
A
B
C
D
E
1
KiaNissanToyotaHonda
2
200810111213
3
200920111413
4
201030151213
5
 
A
B
C
D
E
 
1
2
3
4
5
 

var example3 = document.getElementById('example3');
var settings3 = {
  data: getData(),
  startRows: 5,
  startCols: 5,
  rowHeaders: true,
  colHeaders: true, // If you use `dropdownMenu` option you must enable colHeaders
  minSpareRows: 1
};

var hot3 = new Handsontable(example3, settings3);

hot3.updateSettings({
  dropdownMenu: {
    callback: function (key, options) {
      if (key === 'about') {
        setTimeout(function () {
          // timeout is used to make sure the menu collapsed before alert is shown
          alert("This is a dropdown menu with default and custom options mixed");
        }, 100);
      }
    },
    items: {
      "col_left": {
        disabled: function () {
          // if first column, disable this option
          return this.getSelected() && this.getSelected()[1] === 0;
        }
      },
      "col_right": {},
      "hsep1": "---------",
      "remove_col": {
        name: 'Remove this column, ok?',
        disabled: function () {
          // if first column, disable this option
          return this.getSelected() && this.getSelected()[1] === 0
        }
      },
      "hsep2": "---------",
      "about": {name: 'About this menu'}
    }
  }
})