是否可以在掌上电脑中禁用删除键?

时间:2015-02-01 07:57:04

标签: handsontable

我尝试使用禁用删除键 stopImmediatePropagation()函数,就像文档中描述的那样,但它不会阻止delete键的默认行为。

参见示例http://jsfiddle.net/d226o64r/

1 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,文档错了。检查this issue。 您需要启用立即传播:

Handsontable 0.16:

beforeKeyDown: function (event) {
    if (e.keyCode === 46) {
        Handsontable.Dom.enableImmediatePropagation(event);
        event.stopImmediatePropagation();
    }
}

注意,在Handsontable 0.17中,语法已更改为:

beforeKeyDown: function (event) {
    if (e.keyCode === 46) {
        Handsontable.Dom.stopImmediatePropagation(event);
    }
}

下面是一个基于你的小提琴的工作示例:

$(document).ready(function()
{
  var data = [
      ['Nissan', 2009, 'black', 'black'],
      ['Nissan', 2006, 'blue', 'blue'],
      ['Chrysler', 2004, 'yellow', 'black'],
      ['Volvo', 2012, 'yellow', 'gray']
    ],
    container = document.getElementById("example1"),
    lastChange = null,
    hot;
  
  hot = new Handsontable(container, {
    data: data,
    colHeaders: true,
    rowHeaders: true,
    minSpareRows: 1,
    beforeChange: function (changes, source) {
      lastChange = changes;
    }
  });
  
  hot.updateSettings({
      beforeKeyDown: function (e) {
        if (e.keyCode === 46) {
            Handsontable.Dom.enableImmediatePropagation(e);
            e.stopImmediatePropagation();
        }
      }
  });
});
body { background: white; margin: 20px; }
h2   { margin: 20px 0; }
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://docs.handsontable.com/0.16.0/bower_components/handsontable/dist/handsontable.full.js"></script>
<link rel="stylesheet" media="screen" href="http://handsontable.com/dist/handsontable.full.css">
<link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">
<link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">
<link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">

<h2>beforeKeyDown callback</h2>
<p>The following demo uses <code>beforeKeyDown</code> callback to modify some key bindings:</p>
<div id="example1" class="handsontable"></div>