【问题标题】:How to prevent closing of cell editing in ag-grid on "Other Cell Focus"如何防止在“其他单元格焦点”上的 ag-grid 中关闭单元格编辑
【发布时间】:2020-07-09 23:20:09
【问题描述】:

我正在使用 ag-grid 库在 Angular 应用程序中处理可编辑表。我想继续编辑单元格(在全行编辑模式下),直到我完成它,然后通过 API 手动关闭编辑器。问题是编辑器正在关闭其他单元格单击/聚焦(在其他行),如here 所述:

当发生以下任何情况时,网格将停止编辑:

  • 其他单元格焦点:如果网格中的焦点转到另一个单元格,编辑将停止。

如果可能的话,我不知道如何禁用它。安装onCellMouseDown() 挂钩没有帮助,因为cellFocused 事件在cellMouseDown 之前触发。因此,在我有机会拦截mousedown 事件之前,编辑就停止了。

这是我的stackblitz 小摘录以及相关代码。

这种情况的需要是我想验证条目,如果表单无效,则不允许使用退出编辑。到目前为止,我发现的唯一解决方法是,当编辑器关闭时,在编辑单元格之外的任何点击我都会立即在 onRowEditingStopped() 挂钩中重新打开它,除非编辑器已通过“确定”按钮关闭。

【问题讨论】:

  • 我用过reactjs,但是这个this.gridApi.redrawRows();对我有点帮助,虽然我还是无法避免关注另一个单元格。
  • 我有完全相同的要求...有什么更新吗?

标签: ag-grid ag-grid-angular


【解决方案1】:

毕竟,我已经设法提供了一个完全适合我也面临的这个问题的自定义解决方案。

第一件事是在当前正在编辑特定行时禁用指向未编辑行的指针事件。在 Ag-grid 的“cellEditingStarted”回调中,我添加了以下代码:

public cellEditingStarted(event: any): void {
    //not all rows are on dom ag-grid takes care of it
    const nonSelectedGridRows = document.querySelectorAll('.ag-grid-custom-row:not(.ag-row-selected):not(.ag-row-editing):not(.pointer-events-none)');

    forEach(nonSelectedGridRows, row => {
        row.classList.add("pointer-events-none");
    });
}

由于在编辑特定单元格时并非所有行都存在于 dom 上(Ag-grid 在您滚动时创建和销毁),所以我还添加了一个 rowClassRule,它在创建行时应用:

this.rowClassRules = {            
        'pointer-events-none': params => {
            if (params.api.getEditingCells().length > 0) {
                return true;
            }

            return false;
        }
    };

scss:

.pointer-events-none {
  pointer-events: none
}

通过禁用指针事件,当您单击未编辑的单元格时,该单元格将不会获得焦点,因此当前编辑的单元格仍将保持在编辑模式。您可以提供自己的自定义验证解决方案并通过 API 手动关闭编辑器。完成后,您必须再次启用指向所有网格行的指针事件:

private enablePointerEvents(): void {
    //not all rows are on dom ag-grid takes care of it
    const nonSelectedGridRows = document.querySelectorAll('.ag-grid-custom-row.pointer-events-none');

    forEach(nonSelectedGridRows, row => {
        row.classList.remove("pointer-events-none");
    });
}

【讨论】:

  • 我还不能实现我想要的,但它看起来是正确的方向
  • 实际上这对 Angular 来说是个糟糕的实现。首选的解决方案是集成叠加层。
【解决方案2】:

我在 Ag-Grid React 中实现了与上述相同的方法。 我使用 getRowStyle 回调在动态基础上添加 css pointerEvents: none。 它似乎对我有用。 请参考以下代码

const getRowStyle = (params) => {
// this is not initialized in read mode
// condition for me ==> currentEditRowIndex.current !== null && params.node.rowIndex !== currentEditRowIndex.current
if (someCondition for Row other than inline edit row) {
  return { pointerEvents: "none" };
}
return null;
};

在开始编辑时添加此内容后..您需要调用 redrawRows 以便可以应用 css 更改。

希望这会有所帮助。谢谢!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-05
    • 2018-12-25
    • 2018-08-22
    • 2019-10-26
    • 2021-09-10
    • 2019-09-12
    • 2018-05-29
    • 2020-02-19
    相关资源
    最近更新 更多