简体   繁体   English

使用多行和自动保存的 ag-grid

[英]Using ag-grid with many rows and Autosave

I'm using ag-grid (javascript) to display a large amount of rows (about 3,000 or more) and allow the user to enter values and it should auto-save them as the user goes along.我正在使用 ag-grid (javascript) 来显示大量行(大约 3,000 或更多)并允许用户输入值,并且它应该随着用户的进行自动保存它们。 My current strategy is after detecting that a user makes a change to save the data for that row.我当前的策略是在检测到用户进行更改以保存该行的数据之后。

The problem I'm running into is detecting and getting the correct values after the user enters a value.我遇到的问题是在用户输入值后检测并获取正确的值。 The onCellKeyPress event doesn't get fired for Backaspace or Paste.不会为 Backaspace 或 Paste 触发onCellKeyPress事件。 However if I attach events directly to DOM fields to catch key presses, I don't know how to know what data the value is associated with.但是,如果我将事件直接附加到 DOM 字段以捕获按键,我不知道如何知道该值与哪些数据相关联。 Can I use getDisplayedRowAtIndex or such to be able to reliably do this reliably?我可以使用getDisplayedRowAtIndex等来可靠地可靠地做到这一点吗? What is a good way to implement this?什么是实现这一点的好方法?

EDIT: Additional detail编辑:附加细节

My current approach is to capture onCellEditingStopped and then getting the data from the event using event.data[event.column.colId].我目前的方法是捕获onCellEditingStopped ,然后使用 event.data[event.column.colId] 从事件中获取数据。 Since I only get this event when the user moves to a different cell and not just if they finish typing I also handle the onCellKeyPress and get the data from event.event.target (since there is no event.data when handling this event).因为我只在用户移动到不同的单元格时才得到这个事件,而不仅仅是在他们完成输入时,我还处理onCellKeyPress并从 event.event.target 获取数据(因为在处理这个事件时没有 event.data)。 Here is where I run into a hard-to-reproduce problem that event.event.target is sometimes undefined.在这里,我遇到了一个难以重现的问题,即 event.event.target 有时未定义。

I also looked at using forEachLeafNode method but it returns an error saying it isn't supported when using infinite row model.我还查看了使用forEachLeafNode方法,但它返回一个错误,指出在使用无限行模型时不支持它。 If I don't use infinite mode the load time is slow.如果我不使用无限模式加载时间很慢。

It looks like you can bind to the onCellKeyDown event.看起来您可以绑定到 onCellKeyDown 事件。 This is sometimes undefined because on first keydown the edit of agGrid will switch from the cell content to the cell editor.这有时是未定义的,因为在第一次按下键时,agGrid 的编辑将从单元格内容切换到单元格编辑器。 You can wrap this around to check if there is a cell value or cell textContent.您可以将其环绕以检查是否有单元格值或单元格 textContent。

function onCellKeyDown(e) {
  console.log('onCellKeyDown', e);
  if(e.event.target.value) console.log(e.event.target.value)
  else console.log(e.event.target.textContent)
}

See https://plnkr.co/edit/XhpVlMl7Jrr7QT4ftTAi?p=previewhttps://plnkr.co/edit/XhpVlMl7Jrr7QT4ftTAi?p=preview

As been pointed out in comments, onCellValueChanged might work, however正如评论中指出的那样, onCellValueChanged可能会起作用,但是

After a cell has been changed with default editing (ie not your own custom cell renderer), the cellValueChanged event is fired.在使用默认编辑(即不是您自己的自定义单元格渲染器)更改单元格后,将触发 cellValueChanged 事件。

var gridOptions = {
    rowData: null,
    columnDefs: columnDefs,
    defaultColDef: {
        editable: true, // using default editor
        width: 100
    },
    onCellEditingStarted: function(event) {
        console.log('cellEditingStarted', event);
    },
    onCellEditingStopped: function(event) {
        console.log('cellEditingStopped', event);
    },
    onCellValueChanged: function(event) {
      console.log('cellValueChanged', event);
    }
};

another option could be to craft your own editor and inject it into cells:另一种选择是制作自己的编辑器并将其注入单元格:

function MyCellEditor () {}

// gets called once before the renderer is used
MyCellEditor.prototype.init = function(params) {
    this.eInput = document.createElement('input');
    this.eInput.value = params.value;
    console.log(params.charPress);  // the string that started the edit, eg 'a' if letter a was pressed, or 'A' if shift + letter a
    this.eInput.onkeypress = (e) => {console.log(e);} // check your keypress here
};

// gets called once when grid ready to insert the element
MyCellEditor.prototype.getGui = function() {
    return this.eInput;
};

// focus and select can be done after the gui is attached
MyCellEditor.prototype.afterGuiAttached = function() {
    this.eInput.focus();
    this.eInput.select();
};

MyCellEditor.prototype.onKeyDown = (e) => console.log(e);

// returns the new value after editing
MyCellEditor.prototype.getValue = function() {
    return this.eInput.value;
};
//// then, register it with your grid:
var gridOptions = {
    rowData: null,
    columnDefs: columnDefs,
    components: {
        myEditor: MyCellEditor,
    },
    defaultColDef: {
        editable: true,
        cellEditor: 'myEditor',
        width: 100
    },
    onCellEditingStarted: function(event) {
        console.log('cellEditingStarted', event);
    },
    onCellEditingStopped: function(event) {
        console.log('cellEditingStopped', event);
    }
};

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

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