简体   繁体   English

Ag-grid:使用“agSelectCellEditor”时清除单元格

[英]Ag-grid: clear cell when "agSelectCellEditor" is used

As stated in the ag-grid documentation :如 ag-grid文档中所述:

The default editor will clear the contents of the cell if Backspace or Delete are pressed.如果按下 Backspace 或 Delete,默认编辑器将清除单元格的内容。

But this doesn't work when the "agSelectCellEditor" is used.但这在使用“agSelectCellEditor”时不起作用。 If you press Delete or Backspace the cell will enter in EDIT mode and you can choose only the values that are provided as options.如果您按 Delete 或 Backspace 键,单元格将进入编辑模式,您只能选择作为选项提供的值。

Any idea how can I achieve the same behavior?知道如何实现相同的行为吗?

I found an article that explain how to write the delete cells logic.我发现一篇文章解释了如何编写删除单元格逻辑。 This works also for multiple cells.这也适用于多个单元格。 Please check this article: https://blog.ag-grid.com/deleting-selected-rows-and-cell-ranges-via-key-press/请查看这篇文章: https://blog.ag-grid.com/deleting-selected-rows-and-cell-ranges-via-key-press/

Basically you override the default behavior of the DELETE or BACKSPACE keys using suppressKeyboardEvent callback in our default column definition:基本上,您在我们的默认列定义中使用suppressKeyboardEvent回调覆盖 DELETE 或 BACKSPACE 键的默认行为:

defaultColDef: {
    suppressKeyboardEvent: params => {
        if (!params.editing) {
            let isBackspaceKey = params.event.keyCode === 8;
            let isDeleteKey = params.event.keyCode === 46;
            
            if (isBackspaceKey || isDeleteKey) {
              params.api.getCellRanges().forEach(range => {
              let colIds = range.columns.map(col => col.colId);

              let startRowIndex = Math.min(
                  range.startRow.rowIndex,
                  range.endRow.rowIndex
              );
              let endRowIndex = Math.max(
                  range.startRow.rowIndex,
                  range.endRow.rowIndex
              );

              clearCells(startRowIndex, endRowIndex, colIds, params.api);
            }
        }
        return false;
    }
}

And the delete method:和删除方法:

function clearCells(start, end, columns, gridApi) {
  let itemsToUpdate = [];

  for (let i = start; i <= end; i++) {
    let data = gridApi.rowModel.rowsToDisplay[i].data;
    columns.forEach(column => {
      data[column] = "";
    });
    itemsToUpdate.push(data);
  }

  gridApi.applyTransaction({ update: itemsToUpdate });
}

This works as expected.这按预期工作。

This was fixed in 28.2.0, now delete key always clears the cell without entering the "editing mode".这在 28.2.0 中已修复,现在删除键总是清除单元格而不进入“编辑模式”。

Reference: https://github.com/ag-grid/ag-grid/releases/tag/v28.2.0 , AG‑4801参考: https://github.com/ag-grid/ag-grid/releases/tag/v28.2.0,AG-4801

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

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