【问题标题】:Jqgrid showing page 1 of 0 on emptyJqgrid 在空时显示第 1 页,共 0 页
【发布时间】:2011-10-24 12:55:42
【问题描述】:

我有一个问题,当网格为空时,为什么它显示Page 1 of 0 不能是Page 1 of 1 或更合理的东西?

我的代码

var xml=client.responseText;
         var xmlDoc = $.parseXML(xml); 
         var $xml = $(xml);

         xml=xml.replace(/<productId>1/g, "<productId>"+productMap['1']);
         xml=xml.replace(/<productId>2/g, "<productId>"+productMap['2']);
         xml=xml.replace(/<productId>3/g, "<productId>"+productMap['3']);


            $('#configDiv').empty();
            $('#configDiv').html(  '<div id="configDetailsGrid" width="100%"><table id="list1" width="100%"></table><div id="gridpager"></div></div>');

            //var grid = jQuery("#list1");
            //var iconAlert;

            var getColumnIndexByName = function (grid, columnName) {
                var cm = grid.jqGrid('getGridParam', 'colModel'), i = 0, l = cm.length;
                for (; i < l; i += 1) {
                    if (cm[i].name === columnName) {
                        return i; // return the index
                    }
                }
                return -1;
            },
            grid = jQuery("#list1"),
            iconAlert = '<span class="ui-state-error" style="border:0"><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span></span>';
            grid.jqGrid({

              datastr : xml,
              datatype: 'xmlstring',
              //datatype: 'clientside',
              colNames:['cfgId','Name', 'Host','Operating System', 'Description','Product', 'Type', 'Last Updated Time','Last Updated By','',''],
              colModel:[
                  {name:'cfgId',index:'cfgId', width:90, align:"left", hidden:true},
                  //{name:'updateDate',index:'updateDate', width:12, align:'center', /*formatter: oldConfigurationWarning*/ },
                  {name:'cfgName',index:'cfgName', width:70, align:"left", formatter: 'showlink', formatoptions: {baseLinkUrl: '#'} },
                  {name:'hostname',index:'hostname', width:70, align:"left"},
                  {name:'osname',index:'osname', width:90, align:"left"},
                  {name:'cfgDesc',index:'cfgDesc', width:90, align:"left"},
                  {name:'productId',index:'productId', width:40, align:"left"},
                  {name:'cfgType',index:'cfgType', width:50, align:"left"},
                  {name:'updateDate',index:'updateDate',sorttype:'Date', width:120, align:"left"},
                  {name:'emailAddress',index:'emailAddress', width:120, align:"left"},
                  {name:'absolutePath',index:'absolutePath', width:90, align:"left", hidden:true},
                  {name:'fileName',index:'fileName', width:90, align:"left", hidden:true}
              ],
              pager : '#gridpager',
              rowNum:1000,
              rowList:[10,50,100],
              scrollOffset:0,
              height: 'auto',
              emptyrecords: 'No configurations loaded',
              autowidth:true,
              viewrecords: true,
              gridview: true,
              multiselect: true,
              xmlReader: {
                  root : "list",
                  row: "Response",
                  userdata: "userdata",
                  repeatitems: false
              },

              loadComplete: function () {
                    var count = grid.jqGrid('getGridParam');
                    var ts = grid[0];
                    if (ts.p.reccount === 0) {
                        grid.hide();
                        emptyMsgDiv.show();
                    } else {
                        grid.show();
                        emptyMsgDiv.hide();
                    }

                    //for showlink and icon alert having date difference more than 90 days
                    var iRow, row, trClasses, $cell,
                    icfgName = getColumnIndexByName(grid, 'cfgName'),
                    iupdateDate = getColumnIndexByName(grid, 'updateDate'),
                    mygrid = grid[0],
                    rows = mygrid.rows,
                    cRows = rows.length,
                    myLink = function (e) {
                        var $td = $(e.target).closest('td'),
                            text = $td.text(),
                            $tr = $td.closest('tr'),
                            rowid = $tr[0].id;
                            goToViewAllPage(rowid);
                    };
                for (iRow = 0; iRow < cRows; iRow += 1) {
                    row = rows[iRow]; // row.id is the rowid
                    trClasses = row.className.split(' ');
                    if ($.inArray('jqgrow', trClasses) > 0) {
                        // the row is a standard row (only if subGrid:true are used)
                        var cellvalue1,firstDate,secondDate;
                        $cell = $(row.cells[icfgName]);
                        cellvalue1=$(row.cells[iupdateDate]).text();
                        firstDate = new Date();
                        //console.info(cellvalue1+", "+cellvalue1.length);
                        //var cellvalue1="08-18-2011 11:49:01";
                        if(cellvalue1.length>25)
                        {
                            secondDate=new Date();
                            //secondDate = secondDate.substring(0, secondDate.length-3);

                            if(diffOf2Dates(firstDate,secondDate,true)>=expireCondition)
                            {
                                $cell.prepend(iconAlert);
                            }
                            $cell.click(myLink);
                        }
                        else
                        {

                            secondDate = cellvalue1.substring(0, cellvalue1.length-6);
                            if(diffOf2Dates(firstDate,secondDate,false)>=expireCondition)
                            {
                                $cell.prepend(iconAlert);
                            }
                            $cell.click(myLink);
                        }
                    }
                }

                },
              onSelectRow: function(id,status){

                  }
            });
            grid.jqGrid('navGrid','#gridpager',{edit:false,add:false,del:false});

            var myGrid = $("#list1");
            $("#cb_"+myGrid[0].id).hide();

            // place div with empty message insde of bdiv
            emptyMsgDiv.insertAfter(grid.parent());

            //$("#list1").setGridParam({rowNum:10});
            //$("#list1").trigger("reloadGrid");

            $("#list1").setGridParam({rowNum:10}).trigger("reloadGrid");
            $("#list1").sortGrid('updateDate', false, 'desc');

我的 Xmldata(为空时)

<Response>
<isSuccess>true</isSuccess>
<operation>viewall</operation>
<message>No configurations loaded</message>
</Response>

更新

我相应地更新了,但仍然没有运气,我仍然得到相同的旧 Page 1 of 0

 xmlReader: {
                  /*root : "list",
                  row: "Response",*/
                  root:"Response",
                  row:"list",
                  userdata: "userdata",
                  repeatitems: false
              },

我的 jquery.jqGrid.min.js 文件更改

if(locdata) {
            ts.p.records = gl;
            //Change after 1 of 0 (Oleg)ts.p.lastpage = Math.ceil(gl/ rn);
            ts.p.lastpage = Math.max(ts.p.page,Math.ceil(gl/ rn)); //line no 1181
}

我的回应

<Response>
  <isSuccess>true</isSuccess>
  <operation>viewall</operation>
  <message>No configurations loaded</message>
</Response>

更新,有用的东西

如果我们重新加载网格,它也可以解决问题

jQuery("#list1").setGridParam({rowNum:10}).trigger("reloadGrid");

【问题讨论】:

  • 结果取决于您使用的输入数据以及您使用的datatype。您能否发布datastr 中的测试 XML 数据?如果您使用jsonjsonstring 作为输入,您将实现您需要的任何行为。
  • @Oleg: 输入类型是 xml,来自这个location的相同输入
  • 我记得答案,但我不明白你为什么使用'xml'而不是'json'。 jqGrid 对于 JSON 数据具有非常灵活的可能性,而对于 XML 数据则相对受限。此外,XML 数据的大小比 JSON 更大,并且双方对 XML 数据的处理速度较慢。如果您遇到问题,您可以从 jsonReader as function (方法)实现 pagetotal 属性。因此,您可以根据任何数据返回 any 值。 xmlReader没有这种可能。
  • @Oleg:我做错什么了吗?还是当网格为空时,分页总是显示1 of 0?这也是一个旧项目,其中只有这个网格我使用 xml rest 使用 json,这种情况下的响应格式无法更改,还有其他方法吗?
  • 您应该在问题中包含您用作“空”XML 的 XML 数据,显示 1 of 0

标签: javascript jqgrid


【解决方案1】:

我没有看到 XML 数据对应于您使用的 xmlReader。可能你应该切换rootrow 属性的值:

xmlReader: {
    root : "list",
    row: "Response",
    userdata: "userdata",
    repeatitems: false
}

此外,输入 XML 数据不包含 pagetotalrecords 属性。如果将任何其他datatype 用作'xml''xmlstring',您可以将pagetotalrecords 定义为jsonReader 内部的函数。 xmlReader 参数不支持。

目前您没有定义pagetotalrecords 属性。所以使用默认值:page: "rows&gt;page"total: "rows&gt;total"records: "rows&gt;records"

我在the answerthe bug report 中描述的错误在jqGrid 4.2.0 的代码中仍未修复。因此,在您应用修复之前,您当前的数据将显示“1 of NaN”。

毕竟你可以考虑改一下jqGrid代码的1193这一行

ts.p.lastpage = Math.ceil(gl/ rn);

lastpage 中有其他值。

已更新:建议您将jqGrid代码的ts.p.lastpage = Math.ceil(gl/ rn);这一行修改为ts.p.lastpage = Math.max(ts.p.page,Math.ceil(gl/ rn));

如果您的输入数据不包含&lt;page&gt; 元素或包含&lt;Response&gt;&lt;page&gt;1&lt;/page&gt;...,您将看到1 of 1。如果您的 XML 数据将包含 &lt;Response&gt;&lt;page&gt;0&lt;/page&gt;...,您将看到 0 of 0

【讨论】:

  • 我尝试ts.p.lastpage=10000; 只是为了测试,但在我交换root : "Response", row: "list", 部分之后,它仍然没有在Page 1 of 0 中显示10000,但没有运气
  • @AbhishekSimon:我建议你把jqGrid源码的ts.p.lastpage = Math.ceil(gl/ rn)这一行修改为ts.p.lastpage = Math.max(ts.p.page,Math.ceil(gl/ rn))
  • 我不明白,我哪里出错了? In the case if your input data will contain no &lt;page&gt; element or if it contains &lt;Response&gt;&lt;page&gt;1&lt;/page&gt;... you will see 1 of 1我完全按照你说的做了。我已经更新了我的问题
  • @AbhishekSimon:从the answerthe demo。可能这就是您所需要的。
  • @AbhishekSimon:不客气!顺便说一句,如果某些答案有帮助,您不应该忘记投票。投票是搜索和排序中最重要的标准。投票率低的答案将很难找到。因此,投票主要不是让某人获得额外声誉积分的方式,该功能主要是帮助其他人在 stackoverflow 上找到有用的答案。您每天有权对大约 30 个问题或答案进行投票(请参阅 here)。所以请更频繁地使用你的权利。
【解决方案2】:

我看不出对 Oleg 的最新更新和 max() 的使用有任何积极影响,因为在我的使用上下文中,代码没有经过这部分。 但是,源代码中还有另一个 ts.p.lastpage = ... 的实例。

ts.p.lastpage = lp === 未定义? 1:lp;

替换为:

ts.p.lastpage = lp === 未定义 || lp === 0 ? 1:lp;

这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-13
    • 2011-04-15
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 2014-01-20
    • 2017-11-08
    • 1970-01-01
    相关资源
    最近更新 更多