【问题标题】:jqGrid delete Row while in edit mode and pass additional datajqGrid 在编辑模式下删除行并传递附加数据
【发布时间】:2016-01-12 20:42:17
【问题描述】:

当行处于编辑模式(内联编辑)并且用户单击删除(导航网格)时,我在传递其他删除数据时遇到问题。

这是我的代码:

    .navGrid('#' + childGridPagerID, {add: false,edit: true,del: true,   cancel: true, refresh: true, view: true, search:false},{},
                {// edit options
                        editCaption: "Exercise Special Note",template: template,
                        errorTextFormat: function (data) {
                            return 'Error: ' + data.responseText
                        },
                },
                { // del options
                    mtype:"POST", reloadAfterSubmit:true, serializeDelData: function (postdata) {
                        var selRowId = $("#" + childGridID).jqGrid ('getGridParam', 'selrow');
                        //console.log(selRowID);

                        if ($("#"+selRowId).attr("editable") === "1") {
                            $("#" + childGridID).editRow(selRowId, false);
                            var rowdata = $("#" + childGridID).getRowData(selRowId);
                        }
                        console.log(rowdata);

                    // append postdata with any information
                    }
                }
    )

我知道文档说 getRowData 在编辑模式下不起作用,所以我尝试在调用 getRowData 之前禁用编辑模式,但仍然在控制台日志中获取 html 而不是真实数据。我还看到我可以先保存该行,然后在删除之前获取我的数据,问题是大多数字段都是必需的,用户可能会在不填写所有字段的情况下删除该行,这会提示用户输入必填字段.

【问题讨论】:

    标签: jquery jqgrid


    【解决方案1】:

    如果我正确理解您的问题,您应该调用restoreRowsaveRow 而不是serializeDelData 内部的用法editRow。顺便说一下postdata 包含删除行的id,因此您不需要使用.jqGrid ('getGridParam', 'selrow') 来获取相同的信息。修改后的代码可以是:

    serializeDelData: function (postdata) {
        var rowId = postdata.id; // or postdata[this.p.prmNames[id]]
    
        if ($("#"+rowId).attr("editable") === "1") {
            $(this).restoreRow(rowId);
            var rowdata = $(this).getRowData(selRowId);
            console.log(rowdata);
        }
    
        // append postdata with any information
        ...
    
        return postdata;
    }
    

    顺便可以考虑使用另一个回调来扩展信息,发布到服务器。例如,onclickSubmit 的目标正是提供此类信息。它可以返回{} 或将与主要信息合并的对象。代码可能类似于

    onclickSubmit: function (deleteOptions, rowids) {
        $(this).jqGrid("restoreRow", rowids);
        var rowData = $(this).jqGrid("getRowData", postdata);
        return {id1: rowData.id1, id2: rowData.id2};
    }
    

    【讨论】:

    • 谢谢 Oleg,但是我不得不提到这是一个三层子网格的一部分,当我使用 var rowID = postdata.id 时,我没有得到允许我获取 rowData 的真实 ID .使用此方法时,它返回行的索引 (1, 2, 3,...) 等等,但是当我使用 getGridParam 方法时,我得到了真实的 id,在我的情况下,它返回 progGrid_1_table_d_1_1,之后我可以检索行数据。感谢您的帮助,您是终极大师。
    • @boulepick:不客气!如果您有具有真实ID的列,那么您可以在该列中key: true。它通知 jqGrid 使用该值作为 rowid 和 postdata.id 将已经是您所需要的。在这种情况下,您将删除部分代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多