【问题标题】:JQGrid toolbar filter and restoring a row in edit state with formatted columnsJQGrid 工具栏过滤器并使用格式化的列恢复处于编辑状态的行
【发布时间】:2012-01-11 17:27:28
【问题描述】:

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

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

我浏览了 JQGrid 源代码并提出了解决此问题的方法 (JQGrid v4.3.1)。我更改了 restoreRow 函数中的一些代码来解决我的问题:

注视jquery.jqGrid.src.js 的第 9038 行(添加代码见 cmets):

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.savedRow 数据将行数据设置为,而是使用$grid.p.data 将行数据设置为恢复行时的数据。

我想我正在寻找有关我的修复的反馈 - 通过以这种方式更改源代码,我可能会遇到任何意想不到的后果吗?有没有一种方法可以在不更改源代码的情况下实现此修复(我的团队更容易维护和更新 JQGrid)?我没有正确理解某些东西吗? :)

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

【问题讨论】:

    标签: javascript jquery jqgrid jqgrid-formatter


    【解决方案1】:

    您的建议的一个重要问题是$grid.p.data 并不总是存在。如果您使用带有datatype: 'json'datatype: 'xml' 的“经典”网格并且您不使用loadonce: true,您将有未定义的data 参数。我建议 Tony 修改 addJSONDataaddXmlData 的代码以填充 data 参数(参见 the part of code),但无论如何,jqGrid 的当前实现并不总是填充 data。所以$t.p.data不能用在这种情况下。

    【讨论】:

    • 啊,目前我使用loadonce: truedatatype: 'json'。我可能会在我们的应用程序的特定网格中更改为loadonce: false,以提高性能,因为我们有可能从填充它的查询中检索数万个结果。由于在这种情况下过滤将在服务器端完成,所以restoreRow 中我需要$t.p.data 来修复“错误”的位置将不是必需的。我想我只需要检查 $t.p.data 是否为 null 并在这种情况下使用 $t.p.savedRow。非常感谢您让我知道 $t.p.data 没有被填充!
    猜你喜欢
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 2016-09-12
    • 1970-01-01
    • 2011-07-29
    • 2014-08-30
    • 1970-01-01
    相关资源
    最近更新 更多