使用 Enter 键导航到 AG-Grid 中的下方单元格

Use Enter key to navigate to cell below in AG-Grid

我们需要编辑 A​​G-Grid 中的单元格导航,但我找不到满足我们需要的方法。这不是一个巨大的变化,但对我们的用户来说是一个至关重要的变化。我们需要的导航规则类似于 Google 电子表格单元格导航。

应适用以下规则:

我们正在使用 AngularJS。

EDIT:

This feature has been added to AG-Grid! Documentation here.

原始(过时)答案:

我们最后在 AG-Grid 论坛上提问。没有简单或干净的方法来做到这一点。基本上,您向网格添加一个事件,监听 'Enter' 被按下,然后手动将焦点向下移动一行。

当 'Enter' 事件触发时,您必须知道用户当前是否正在编辑,还要注意用户是否在最后一行。

gridDiv.addEventListener('keydown', function(evt) {
  if(editing && evt.key === 'Enter') {
      var currentCell = gridOptions.api.getFocusedCell();
      var finalRowIndex = gridOptions.api.paginationGetRowCount()-1;

      // If we are editing the last row in the grid, don't move to next line
      if (currentCell.rowIndex === finalRowIndex) {
          return;
      }
      gridOptions.api.stopEditing();
      gridOptions.api.clearFocusedCell();

      gridOptions.api.startEditingCell({
        rowIndex: currentCell.rowIndex + 1,
        colKey: currentCell.column.colId
      });
  }
});

编辑标志在网格选项中手动管理。

var editing = false;

var gridOptions = {
    columnDefs: columnDefs,
    rowData: students,
    onCellEditingStarted: function (evt) {
        editing = true;
    },
    onCellEditingStopped: function (evt) {
        editing = false;
    }
};

这是一个有效的 plunker 示例:
https://plnkr.co/edit/quhyS05SSVzmuDlCErZD?p=preview

您还可以使用 'keydown' 事件全局跟踪最后按下的键,这是您答案的一个变体。

let eventKey;
let validKeys = ['Enter'];

gridDiv.addEventListener('keydown', function(event) {
    eventKey = event.key;
});

onCellEditingStopped: function (event) {
    if(validKeys.includes(eventKey)){
        var currentCell = gridOptions.api.getFocusedCell();
        var finalRowIndex = gridOptions.api.paginationGetRowCount()-1;

        if (currentCell.rowIndex === finalRowIndex) {
            return;
        }

        gridOptions.api.startEditingCell({
            rowIndex: currentCell.rowIndex + 1,
            colKey: currentCell.column.colId
        });
    }
}