简体   繁体   English

Ag-grid React:按下 Escape 时如何保存新值?

[英]Ag-grid React: how can i save new value when i press Escape?

Ag-grid React I need to save the value of my input when pressing the escape key, but I don't know how to do it in ag -grid. Ag-grid React我需要在按下转义键时保存输入的值,但我不知道如何在 ag-grid 中执行此操作。

When i press Enter - i can save new value, But with Esc - i cant, it just discard the changes Where and how can I change this behavior?当我按 Enter - 我可以保存新值,但是使用 Esc - 我不能,它只是放弃更改我在哪里以及如何更改此行为?

To do this you'll have to implement your own Custom Editor Component, and inside there you can add logic to listen to the keyPresses inside the input element, and save the value before telling the Grid to stop editing.为此,您必须实现自己的自定义编辑器组件,并且可以在其中添加逻辑以侦听输入元素内的 keyPresses,并在告诉 Grid 停止编辑之前保存该值。

Please see this implemented in the following plunkr请在以下 plunkr中查看此实现

First, implement suppressKeyboardEvent to tell the grid to ignore the escape key:首先,实现suppressKeyboardEvent告诉网格忽略转义键:

Documentation on Suppressing Keyboard Events有关抑制键盘事件的文档

            {
                field: 'number',
                suppressKeyboardEvent: ({ editing, event }) =>
                    event.code === KEY_ESCAPE && editing,
                cellEditor: DoublingEditor,
                editable: true,
                width: 300,
            }

Then, inside the custom editor component, you can add a grid event listener to listen to when keys are pressed (you can also use the browser default keydown ):然后,在自定义编辑器组件中,你可以添加一个网格事件监听器来监听按键按下的事件(你也可以使用浏览器默认的keydown ):

        useEffect(() => {
            props.api.addEventListener('cellKeyDown', onKeyDown);
            return () => {
                props.api.removeEventListener('cellKeyDown', onKeyDown);
            };
        }, [onKeyDown]);

And finally, inside the listener, check if the escape key is pressed and stop editing the cell if it is:最后,在侦听器内部,检查是否按下了转义键,如果是,则停止编辑单元格:

        const onKeyDown = (params) => {
            if (params.event.code === KEY_ESCAPE) {
                params.api.stopEditing();
            }
        };

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

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