SlickGrid cannot delete added rows, but only existing ones. What am I doing wrong?

最后都变了- 提交于 2019-12-10 19:04:15

问题


Here is my code for adding a row:

grid.onAddNewRow.subscribe(function (e, args) {
    var item = args.item;
    id=id+1;
    item["id"] = "id_"+id;
    grid.invalidateRow(data.length);
    data.push(item);
    dataView.beginUpdate();
    dataView.endUpdate();
    grid.updateRowCount();
    grid.render();
});

And here is my code for deleting a row:

if ((event.which == 46)) {
    $(".slick-cell.selected").parent().each(function() {
        var item = dataView.getItem($(this).attr("row"));
        var rowid = item.id;
        dataView.deleteItem(rowid);
        grid.invalidate();
        grid.render();
    });
}

This works for already existing rows but not for the added ones. for some reasons THE item variable is undefined for new rows. What am I doing wrong?

Edited Thanks, Tin! So I`ve got a solution :

      grid.onAddNewRow.subscribe(function (e, args) {
        var item = args.item;
        id=id+1;
        item["id"] = "id_"+id;
        data.push(item);
        dataView.beginUpdate();
        dataView.setItems(data);
        dataView.endUpdate();
      });

.

       if ((event.which == 46)){
            var rows = grid.getSelectedRows();
            for (var i = 0, l = rows.length; i < l; i++) {
                var item = dataView.getItem(rows[i]);
                var rowid = item.id;
                dataView.deleteItem(rowid);
            }
        }

回答1:


You seem to be doing a lot of things in your code that don't quite make sense to me:

In your onAddNewRow handler, you are:

  1. Invalidating the row being added. Why? You don't need to do that.
  2. You update the "data" array directly but then do a no-op call on the "dataView". What are you using as a data source - data or dataView?
  3. You don't need to tell the grid to updateRowCount() or render().

In your delete handler:

  1. DO NOT access SlickGrid DOM directly (i.e. $(".slick-cell.selected"))! Use grid.getSelectedRows() instead.
  2. If you are using "dataView" as your data source, you should already have events wired up to listen to DataView changes and update the grid as needed. Calls to grid.invalidate() and grid.render() are not needed.


来源:https://stackoverflow.com/questions/9126772/slickgrid-cannot-delete-added-rows-but-only-existing-ones-what-am-i-doing-wron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!