简体   繁体   English

handsontable 设置自定义 Tab 键顺序

[英]handsontable set custom tab order

I have a handsontable grid ( HandsonTable ) and would like to limit navigation to only the first two columns (A,B).我有一个可手持网格( HandsonTable ),并且希望将导航限制为仅前两列(A、B)。 So when the user starts on A1 and uses tab it will navigate to B1, A2, B2, A3, B3 etc. and when it reaches the end of the table go backup to A1.因此,当用户从 A1 开始并使用 tab 时,它将导航到 B1、A2、B2、A3、B3 等,当它到达表的末尾时,将备份到 A1。

Is there a way to do this?有没有办法做到这一点?

$(document).ready(function () {

  var container = document.getElementById('basic_example');

  var data = function () {
   return Handsontable.helper.createSpreadsheetData(100, 12);
  };

  var hot = new Handsontable(container, {
    data: data(),
    height: 400,
    colHeaders: true,
    rowHeaders: true,
    stretchH: 'all',
    columnSorting: true,
    contextMenu: true
  });

});

MY CODE我的代码

Used the link provided by mpuraria above and got it working.使用上面 mpuraria 提供的链接并使其正常工作。 The navigation order has been restricted to the first two columns when using the tab key.使用 Tab 键时,导航顺序被限制在前两列。 jsfiddle . jsfiddle

$(document).ready(function () {

  var container = document.getElementById('basic_example');

  var data = function () {
   return Handsontable.helper.createSpreadsheetData(10, 9);
  };

  var hot = new Handsontable(container, {
    data: data(),
    height: 400,
    colHeaders: true,
    rowHeaders: true,
    stretchH: 'all',
    columnSorting: true,
    contextMenu: true
  });


  Handsontable.Dom.addEvent(document.body, 'keydown', function(e) {

      if (e.keyCode === 9) {  


        e.stopPropagation();
        var selection = hot.getSelected();
        var rowIndex = selection[0];
        var colIndex = selection[1];          

        //rowIndex++;
        colIndex++;


          if (colIndex > 1) {
              rowIndex++;
              colIndex = 0;

          }

          hot.selectCell(rowIndex,colIndex);          
      }
    }); 
});

I solved this way:我是这样解决的:

afterDocumentKeyDown: function (event) {
    // getSelected() returns [[startRow, startCol, endRow, endCol], ...]
    const startRow = this.getSelected()[0][0];

    if (event.key === "Tab") {
        const nextRow = startRow === 6 ? 1 : startRow + 1; // if the last row (6), returns to the first one
        this.selectCell(nextRow, 0); // needs to be col 0 because Tab already changes to the right col
    }
}

In my case, I had a table 2 columns x 7 rows, and I have an intention to move only in column 1 from the row (zero-indexed) 1 to 6, returning to row 1.在我的例子中,我有一个 2 列 x 7 行的表,我打算只在第 1 列中从第 1 行(零索引)移动到第 6 行,返回到第 1 行。

References:参考:

  1. https://handsontable.com/docs/7.4.2/Hooks.html#event:afterDocumentKeyDown https://handsontable.com/docs/7.4.2/Hooks.html#event:afterDocumentKeyDown

  2. https://forum.handsontable.com/t/keyboard-tab-key-doesnt-enter-into-handsontable/3490 https://forum.handsontable.com/t/keyboard-tab-key-doesnt-enter-into-handsontable/3490

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM