0

是否有任何解决方法可以userData在 jqGrid 中添加自定义“格式化程序”?我发现了这个问题,它对我有很大帮助。下面是我用来填充 jqGrid 的代码。请注意,我userData在其中填充了一个自定义对象jsonReader并将其设置为网格,loadComplete我需要将单独的“格式化程序”添加到总列中。请让我知道是否有办法。提前致谢。

var userDataTotals;
jq("#testGrid").jqGrid({
    url:'local',
    datatype: 'local',
    mtype: 'GET',
    colNames:[
              'rowId','unitId',
              '<fmt:message key="report.col1"/>',
              '<fmt:message key="report.col2"/>',
    ],
    colModel :[ 
        {name:'rowId', index:'rowId',hidden: true,sortable: true,key:true}, 
        {name:'unitId', index:'unitId',hidden: true,sortable: true,key:true}, 
        {name:'outboundReadyDate', index:'outboundReadyDate', width:80,align:"center",sorttype:'integer',formatter:dateOnlyFmatter,datefmt:'Y M d'},
        {name:'outboundDate', index:'outboundDate', width:80,align:"center",sorttype:'integer',formatter:dateOnlyFmatter,datefmt:'Y M d'},
    ],
    // this will enable the footer row to display totals
    footerrow : true,
    //userDataOnFooter : true,
    altRows : true,
    //to hide pager buttons
    pgbuttons:false,
    recordtext:'',
    pgtext:'',
    gridview: true,
    height:270,
    loadonce: true,
    sortname: 'rowId',
    sortorder: 'asc',
    viewrecords: true,
    rowNum:30000,
    loadComplete: function() {
        // This will increase the column header height ( to show two rows in the header)
        jq(".ui-jqgrid-sortable").css('white-space', 'normal');
        jq(".ui-jqgrid-sortable").css('height', 'auto');
        //Set the total values after load complete,otherwise
        // custom formatter will format the total value as well.

        jq("#mainReportGrid").jqGrid("footerData","set",userDataTotals,false);

        //check the data type to avoid this code to  execute when the pageload
        var checkDatatype = myGrid.jqGrid("getGridParam","datatype");
        if(checkDatatype =='json' && myGrid.getGridParam('records') == 0){
            // when no records are displaying alert it to the user
            alert(noRecordsMsg);
        }

    },

    jsonReader : {
        root: "dtos",
        records: "records",
        repeatitems: false,
        cell: "cell",
        id: "rowId",
        userdata :function(obj) {
            userDataTotals = {"outboundReadyDate":obj.totalOutBounded,
                "outboundDate":obj.totalOutBoundReady};
        }

    },


    //This will format the date of the grid (without displaying time)
    function dateOnlyFmatter (cellvalue, options, rowObject){
        var opts = options.colModel.formatoptions;
        if(cellvalue==null || cellvalue=='undefined'){
            return '-';
        }else{
            if(opts != undefined && rowObject.projectTypeName =='IOD'){
                return 'N/A';   
            }
            var now = new Date(cellvalue);
            return now.format('M j, Y');
        }
    }

我使用自定义dateFormat.js来格式化日期。

json响应是-

{
    "dtos": [
        {
            "unitId": 1068,
            "outboundDate": null,
            "outboundReadyDate": 1317619303000,
            "rowId": 13,
        },
        {
            "unitId": 1105,
            "outboundDate": 1317616970000,
            "outboundReadyDate": 1317617213000,
            "rowId": 14,
        }
    ],
    "totalOutBounded": 0,
    "totalOutBoundReady": 4,
    "rowTotal": 15,
    "returnCode": 0,
    "msg": ""
}

我使用sortTypeasinteger因为从服务器我将“java”Date对象直接传递给网格。为了对其进行排序,我需要设置sortTypeinteger

我遇到的基本问题是在 IE8 中我看不到“userData”总值。但在其他浏览器中我可以看到它。我需要将userData总值格式化为“超链接”。

没有userData格式化我可以看到 IE8 中的总数。所以我认为不使用列'formatter'将自定义格式化程序添加到总值(userData)。

4

1 回答 1

2

您有许多小的语法错误:

  • 使用尾随逗号(如 ',}')是语法错误。您必须从 JSON 数据以及从colNames和中删除尾随逗号colModel。和"rowId": 13,}无法"rowId": 14,}读取。
  • 您定义jQuery("#testGrid"),但用于jQuery("#mainReportGrid")设置页脚。
  • url: 'local'或任何其他参数在的url情况下没有意义datatype: 'local'。在 的情况下,该url参数将被忽略(不使用)datatype: 'local'
  • 您使用myGrid未在发布的代码中定义的。您应该var myGrid = jQuery("#testGrid");在代码开头的某个地方定义,或者在事件处理程序var myGrid = $(this);的开头定义。loadComplete
  • 您使用而now.format('M j, Y')不是发布format. Date您可以改用 jqGrid 方法:return $.fmatter.util.DateFormat(undefined, now, 'M j, Y', $.jgrid.formatter.date);.
  • 我建议您始终使用严格相等===来代替==and!==而不是!=. 在此处阅读示例以获取更多信息。
  • 我建议您使用height: 'auto'或者scrollOffset: 0如果您使用没有滚动条的 jqGrid。
  • 我建议您阅读答案。如果您使用描述的错误修复,您可以将行修改为jq("#mainReportGrid").jqGrid("footerData","set",userDataTotals,false);

    myGrid.jqGrid("footerData", "set", myGrid.jqGrid('getGridParam', 'userData'), false);

    userDataTotals不需要该变量,并且可以将方法userdata中的方法userdata定义为

    userdata: function (obj) { return { outboundReadyDate: obj.totalOutBounded, outboundDate: obj.totalOutBoundReady }; }

您可以在此处查看代码的修改版本。

于 2011-10-05T09:05:44.950 回答