Context menu

优质
小牛编辑
120浏览
2023-12-01
    function getData() {
      return [
['', 'Tesla', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'],
['2017', 10, 11, 12, 13, 15, 16],
['2018', 10, 11, 12, 13, 15, 16],
['2019', 10, 11, 12, 13, 15, 16],
['2020', 10, 11, 12, 13, 15, 16],
['2021', 10, 11, 12, 13, 15, 16]
      ];
    }

Context menu with default options

To run the basic configuration of the Context Menu, just set the contextMenu option to true.

From version 0.11, context menu also works for row and column headers. When the context menu for the row header is opened, the column options are disabled. Likewise, when the context menu for the column header is opened, the row options are disabled

var example1 = document.getElementById('example1');
var settings1 = {
  data: getData(),
  rowHeaders: true,
  colHeaders: true,
  contextMenu: true
};

var hot1 = new Handsontable(example1, settings1);

Context menu with specific options

You can limit options available in the context menu using contextMenu option as an array of keys which are strings:

KeyPurposeExtra conditions
row_aboveInsert row above action
row_belowInsert row below action
col_leftInsert column left action
col_rightInsert column right action
---------Separator
remove_rowRemove row(s) actions
remove_colRemove column(s) action
clear_columnClear column values action
undoUndo actionPlugin UndoRedo turned on
redoRedo actionPlugin UndoRedo turned on
make_read_onlyMake read only action
alignmentAlignment actions
cutCut actionPlugin CopyPaste turned on
copyCopy actionPlugin CopyPaste turned on
freeze_columnFreeze column actionPlugin ManualColumnFreeze turned on
unfreeze_columnUnfreeze column actionPlugin ManualColumnFreeze turned on
bordersCustom borders actionsPlugin CustomBorders turned on
commentsAddEditAdd and edit comment actionsPlugin Comments turned on
commentsRemoveRemove comment actionPlugin Comments turned on
commentsReadOnlyMake comment read only actionPlugin Comments turned on
mergeCellsMerge and unmerge cells actionsPlugin MergeCells turned on
add_childInsert child row actionPlugin NestedRows turned on
detach_from_parentDetach from parent row actionPlugin NestedRows turned on
hidden_columns_hideHide column(s) actionPlugin HiddenColumns turned on
hidden_columns_showShow hidden column(s) actionPlugin HiddenColumns turned on
hidden_rows_hideHide row(s) actionPlugin HiddenRows turned on
hidden_rows_showShow hidden row(s) actionPlugin HiddenRows turned on
filter_by_conditionFirst conditions select elementPlugin Filters turned on
filter_operatorsOperation select elementPlugin Filters turned on
filter_by_condition2Second conditions select elementPlugin Filters turned on
filter_by_valueValue select elementPlugin Filters turned on
filter_action_barAction bar elementPlugin Filters turned on
var example2 = document.getElementById('example2');
var settings2 = {
  data: getData(),
  rowHeaders: true,
  colHeaders: true,
  contextMenu: ['row_above', 'row_below', 'remove_row']
};

var hot2 = new Handsontable(example2, settings2);

Context menu with fully custom configuration

This example shows how to:

  • add common callback for all options
  • dynamically disable option
  • set custom text for predefined option
  • add own custom option
  • add callback for specific option

var example3 = document.getElementById('example3');
var settings3 = {
  data: getData(),
  rowHeaders: true,
  colHeaders: true,
  contextMenu: {
    callback: function (key, selection, clickEvent) {
      // Common callback for all options
      console.log(key, selection, clickEvent);
    },
    items: {
      "row_above": {
        disabled: function () { // `disabled` can be a boolean or a function
          // Disable option when first row was clicked
          return this.getSelectedLast()[0] === 0; // `this` === hot3
        }
      },
      "row_below": {
        name: 'Click to add row below' // Set custom text for predefined option
      },
      "about": { // Own custom option
        name: function () { // `name` can be a string or a function
          return 'Custom option'; // Name can contain HTML
        },
        hidden: function () { // `hidden` can be a boolean or a function
          // Hide the option when the first column was clicked
          return this.getSelectedLast()[1] == 0; // `this` === hot3
        },
        callback: function(key, selection, clickEvent) { // Callback for specific option
          setTimeout(function() {
            alert('Hello world!'); // Fire alert after menu close (with timeout)
          }, 0);
        }
      },
      "colors": { // Own custom option
        name: 'Colors...',
        submenu: {
          // Custom option with submenu of items
          items: [
            {
              // Key must be in the form "parent_key:child_key"
              key: 'colors:red',
              name: 'Red',
              callback: function(key, selection, clickEvent) {
                setTimeout(function() {
                  alert('You clicked red!');
                }, 0);
              }
            },
            { key: 'colors:green', name: 'Green' },
            { key: 'colors:blue', name: 'Blue' }
          ]
        }
      },
      "credits": { // Own custom property
        // Custom rendered element in the context menu
        renderer: function(hot, wrapper, row, col, prop, itemValue) {
          var elem = document.createElement('marquee');
          elem.style.cssText = 'background: lightgray;';
          elem.textContent = 'Brought to you by...';
          return elem;
        },
        disableSelection: true, // Prevent mouseoever from highlighting the item for selection
        isCommand: false // Prevent clicks from executing command and closing the menu
      }
    }
  }
};

var hot3 = new Handsontable(example3, settings3);