JQGridtoolbar过滤并恢复具有格式化列的处于编辑状态的行

JQGrid toolbar filter and restoring a row in edit state with formatted columns

本文关键字:于编辑 编辑 状态 JQGridtoolbar 恢复 格式化 过滤      更新时间:2023-09-26

我注意到,当使用内联行编辑(通过$grid.jqGrid('editRow', rowId, ...etc)将行置于编辑状态),然后在不实际编辑行的情况下恢复行($grid.jqGrid('restoreRow',rowToRestore);)时,会将格式化列的$grid.p.data[{indexofrowrestored}][{columnname}]设置为格式化值,而不是列的原始值。

这样做的结果是,恢复的行的工具栏过滤器输入将根据格式化的值而不是未格式化的值来过滤该行(就像它对所有其他行所做的那样)。

我浏览了JQGrid的源代码,并为这个问题找到了解决方案(JQGridv4.3.1)。我更改了restoreRow函数中的一些代码,以解决我的问题:

盯着jquery.jqGrid.src.js的9038行(添加代码见注释):

restoreRow : function(rowid, afterrestorefunc) {
    // Compatible mode old versions
    var args = $.makeArray(arguments).slice(1), o={};
    if( $.jgrid.realType(args[0]) === "Object" ) {
        o = args[0];
    } else {
        if ($.isFunction(afterrestorefunc)) { o.afterrestorefunc = afterrestorefunc; }
    }
    o = $.extend(true, $.jgrid.inlineEdit, o );
    // End compatible
    return this.each(function(){
        var $t= this, fr, d, ind, ares={};  //UPDATED: added the variable 'd'
        if (!$t.grid ) { return; }
        ind = $($t).jqGrid("getInd",rowid,true);
        if(ind === false) {return;}
        for( var k=0;k<$t.p.savedRow.length;k++) {
            if( $t.p.savedRow[k].id == rowid) {fr = k; break;}
        }
        //----------------------------------------------------------------------------
        //ADDED: added this for-loop
        //      get a hold of the $t.p.data array row with the ID of the
        //      row being restored; this row contains the original, unformatted
        //      data that came from the server while $t.p.savedRow contains
        //      what appears to be formatted column data; this is messing
        //      up the toolbar filter accuracy (which filters on original, unformatted
        //      data) when the row is restored
        for( var k=0;k<$t.p.data.length;k++) {
            if( $t.p.data[k].id == rowid) {d = k; break;}
        }
        //END EDIT
        //----------------------------------------------------------------------------
        if(fr >= 0) {
            if($.isFunction($.fn.datepicker)) {
                try {
                    $("input.hasDatepicker","#"+$.jgrid.jqID(ind.id)).datepicker('hide');
                } catch (e) {}
            }
            $.each($t.p.colModel, function(i,n){
                if(this.editable === true && this.name in $t.p.savedRow[fr] && !$(this).hasClass('not-editable-cell')) {
                    //EDIT: this line was edited to set ares[this.name] to
                    //the original, unformatted data rather than the saved row data
                    //so, below, when setRowData method is called, it is being set
                    //to original data rather than formatted data
                    ares[this.name] = $t.p.data[d][this.name];
                    //END EDIT
                }
            });
            $($t).jqGrid("setRowData",rowid,ares);
            $(ind).attr("editable","0").unbind("keydown");
            $t.p.savedRow.splice(fr,1);
            if($("#"+$.jgrid.jqID(rowid), "#"+$.jgrid.jqID($t.p.id)).hasClass("jqgrid-new-row")){
                setTimeout(function(){$($t).jqGrid("delRowData",rowid);},0);
            }
        }
        if ($.isFunction(o.afterrestorefunc))
        {
            o.afterrestorefunc.call($t, rowid);
        }
    });
},

基本上,当恢复行时,我使用$grid.p.data来设置行数据,而不是使用$grid.p.savedRow数据将行数据设置为。

我想我正在寻找关于我的修复的反馈——以这种方式更改源代码是否会带来任何意想不到的后果?有没有一种方法可以在不更改源代码的情况下实现这个修复(对我的团队来说更容易维护和更新JQGrid)?我是不是理解不正确?:)

非常感谢您的帮助!如果这个bug的演示有帮助,我可以创建一个。我在这里无法访问我的个人网络服务器。

您的建议中的一个重要问题是$grid.p.data并不总是存在。如果将"经典"网格与datatype: 'json'datatype: 'xml'一起使用,而不使用loadonce: true,则会有未定义的data参数。我建议Tony修改addJSONDataaddXmlData的代码来填充data参数(参见代码部分),但无论如何,jqGrid的当前实现并不总是填充data。因此$t.p.data不能用于该情况。