简体   繁体   English

(Ag-Grid)在编辑时,如何立即更改值,而不是在单元格失去焦点后?

[英](Ag-Grid) While editing, how to get value changed immediately, not after cell loses focus?

In ag-grid, while entering some values in table, I need to receive updated values on input or change event.在 ag-grid 中,在表中输入一些值时,我需要接收inputchange事件的更新值。

But onCellValueChanged is triggered only when cell loses focus, and I cannot figure out what is the way to get values immediately.但是onCellValueChanged仅在单元格失去焦点时触发,我无法弄清楚立即获取值的方法是什么。 The only restriction I have - I can't add custom cellEditor, it needs to be grid default.我唯一的限制 - 我不能添加自定义 cellEditor,它需要是网格默认值。

Can someone, please, advise how to reach such behavior?有人可以建议如何达到这种行为吗?

According to ag-grid you can use a custom callback onCellEditingStopped .根据ag-grid,您可以使用自定义回调onCellEditingStopped

This will trigger every time a cell editing is ended.这将在每次单元格编辑结束时触发。

You define it directly on the grid options object and you can also access the edited values (and its row and column) like this:您可以直接在网格选项对象上定义它,您还可以像这样访问编辑过的值(及其行和列):

const gridOptions = {
  columnDefs: [/* whatever */],
  defaultColDef: {/* whatever */},
  rowData: rowData,
  onCellEditingStopped: function(event) {
    console.log(event);
    console.log(event.oldValue); // cell's previous value
    console.log(event.newValue); // cell's new value
    console.log(event.column); // the column that was edited
    console.log(event.data); // the row that was edited
  },
};

// setup the grid after the page has finished loading
document.addEventListener('DOMContentLoaded', function() {
  var gridDiv = document.querySelector('#myGrid');
  new agGrid.Grid(gridDiv, gridOptions);
});

Here you have a detailed explanation of whenever this onCellEditingStopped is triggered. 这里有关于何时触发onCellEditingStopped的详细说明。

You can also inspect the full example .您还可以检查完整示例

You can get the current value of the cell whilst editing by leveraging the Grid Event onCellKeyDown :您可以在编辑时通过利用 Grid Event onCellKeyDown获取单元格的当前值:

https://www.ag-grid.com/javascript-grid/keyboard-navigation/#keyboard-events https://www.ag-grid.com/javascript-grid/keyboard-navigation/#keyboard-events

var gridOptions = {
    onCellKeyDown: params => {
        console.log('current value', params.event.target.value)
    },
}

Please see this sample which showcases this: https://plnkr.co/edit/GbMglD1fxTTeSZFj请查看展示此示例的示例: https : //plnkr.co/edit/GbMglD1fxTTeSZFj

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

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