简体   繁体   English

从jqgrid删除拖放的行

[英]Remove dragged and dropped row from jqgrid

I have two jqgrids setup and can drag from 'master' to 'target' grids. 我有两个jqgrids设置,可以将其从“主”网格拖到“目标”网格。 The row I drop into the 'target' grid is not saved to the database and has a generated, unique ID (prefixed with "new_"). 我放入“目标”网格的行未保存到数据库,而是具有生成的唯一ID(以“ new_”为前缀)。 I have a custom pager button that deletes the selected row. 我有一个自定义寻呼机按钮,可删除所选行。 For saved rows, the event deletes the item from the database and reloads the grid. 对于保存的行,该事件从数据库中删除该项目并重新加载网格。 This works fine. 这很好。 However when I want to remove a dragged row that is NOT saved to the DB the row is not removed. 但是,当我要删除未保存到数据库的拖动行时,该行也不会删除。

Here is my button function: 这是我的按钮功能:

onClickButton: function () {
                var deleteId = tgtGrid.getRowData(tgtGrid.getGridParam('selrow'))['ID'];
                if (!deleteId) {
                    return false;
                }
                // remove an added row, not saved in DB
                if (deleteId.indexOf('new_') != -1) {
                    tgtGrid.jqGrid('delRowData', deleteId);
                } else {
                    // remove account saved in DB
                    $.post( url, { id: deleteId }, function (responseData) {
                        if (!responseData.success) {
                            // error here
                        } else {
                           // success here
                           tgtGrid.trigger('reloadGrid');
                        }
                    });
                }

            }

If I set up the button to use 'delRowData' on the saved row tgtGrid.jqGrid('delRowData', deleteId); 如果我将按钮设置为在保存的行tgtGrid.jqGrid('delRowData', deleteId);上使用'delRowData' tgtGrid.jqGrid('delRowData', deleteId); it returns true and the row is removed from the grid (not the DB, as expected). 它返回true,并且该行从网格中删除(不像预期的那样是DB)。

How do I remove a dropped row? 如何删除删除的行?

I found the problem lies within jqgrid and how it looks for the element to remove. 我发现问题出在jqgrid之内,以及它如何查找要删除的元素。 The tgtGrid.getRowData(pager.getGridParam('selrow'))['ID'] will return the ID or column you set as the key in the column model. tgtGrid.getRowData(pager.getGridParam('selrow'))['ID']将返回您设置为列模型中键的ID或列。 However, jqgrid looks at the <tr> element's ID for removing rows and this does not match in dropped rows. 但是,jqgrid会查看<tr>元素的ID以删除行,这与删除的行不匹配。 For data you load from a DB, the <td> element containing the ID/key will have a matching id attribute as the enclosing <tr> element. 对于从数据库加载的数据,包含ID /密钥的<td>元素将具有匹配的id属性作为封闭的<tr>元素。 For dropped rows the <td> element is what you would expect but the enclosing <tr> element has a generated id attribute with the value "dnd_#" where # is some generated random number. 对于丢弃的行, <td>元素是您所期望的,但是封闭的<tr>元素具有一个生成的id属性,其值为“ dnd_#”,其中#是一些生成的随机数。 Therefore you need to get this id to remove dragged and dropped rows that have not been saved to the data source. 因此,您需要获取此id才能删除尚未保存到数据源的拖放行。 Below is the solution I came up with 以下是我想出的解决方案

var trId = $('td[title="' + deleteId + '"]').closest('tr').attr('id');
tgtGrid.jqGrid('delRowData', trId);

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

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