为什么jqGrid在动态加载数据时不显示第二页?

时间:2018-02-06 16:38:56

标签: javascript jquery html jqgrid

我准备了一个jsFiddle,它模拟从echo页面将数据加载到jqGrid中。这是一个例子:

jqGrid Paging Example (click to run)

$(function() {
  var gridSampleData = [
      { id: 10, firstName: "Jane", lastName: "Doe1"},
      { id: 20, firstName: "Justin", lastName: "Time1" },
      { id: 30, firstName: "Jane", lastName: "Doe2"},
      { id: 40, firstName: "Justin", lastName: "Time2" },
      { id: 11, firstName: "Jane", lastName: "Doe3"},
      { id: 21, firstName: "Justin", lastName: "Time3" },
      { id: 31, firstName: "Jane", lastName: "Doe4"},
      { id: 41, firstName: "Justin", lastName: "Time4" }
    ];
  var rowsPerPage = 4;
  var numRows = gridSampleData.length;
  var pagedData = {
    page:1, total:numRows/rowsPerPage, records: numRows, rows: gridSampleData
  }

  // simulate AJAX request: use echoUrl instead of real web service 
  var jsonData = JSON.stringify(pagedData);
  var echoUrl = '/echo/js/?js=' + jsonData; 

  $("#Grid4").jqGrid({ scroll: false, gridview: true,
    pager: '#pagerGrid4', rowNum: rowsPerPage, viewrecords: true,  
    height: 90, width: 400,
    colNames: ['First name', 'Last name'],
    colModel: [{name: "firstName"}, {name: "lastName"}],
    datatype: "json",
        jsonReader: {
      page: "page",
      total: "total",
      records: "records",
      root: "rows",
      repeatitems: false
        },
    mtype: 'POST',
    url: echoUrl
  });
});

HTML:

<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<title>Canonical jqGrid example</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.15.2/css/ui.jqgrid.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.15.2/jquery.jqgrid.min.js"></script>


<table id="Grid4"></table>
<table id="pagerGrid4"></table>

一切看起来都很好,直到你点击导航按钮 - 它不会显示第二页。为什么是这样?如果您静态加载数据,如this example中的网格#4,则可以正常工作。

注意:我必须使用jsFiddle,因为代码段编辑器中有StackOverflow currently does not support an echo page

2 个答案:

答案 0 :(得分:1)

这是因为在您的示例中,您将返回所有数据行,但始终发送page: 1。每个请求网格都会告诉它它正在获取第1页的数据它永远不会获取第2页的数据。为了使您的示例工作,您可以将属性loadonce设置为true,就像我在原始的fork中所做的那样码。  Example Fiddle

或者您可以修改代码以发送前4行和page: 1,当请求到达第2页时,发送最后4行page: 2

答案 1 :(得分:1)

如果您想真正模拟从服务器加载 loadonce: true的数据,那么我建议您使用serializeGridData,其中发送json参数(请参阅相应的数据页面,查看Echo服务的the documentation。可以在源数据数组上使用slice

serializeGridData: function (request) {
    var start = (request.page - 1) * request.rows;

    return {
        json: JSON.stringify({
            page: request.page,
            total: gridSampleData.length/request.rows,
            records: gridSampleData.length,
            rows: gridSampleData.slice(start, start + request.rows)
        })
    }
}

查看相应的修改后的演示https://jsfiddle.net/OlegKi/bny6h1nz/2/。应该注意的是,服务器通常应该根据sidxsord参数(request.sidxrequest.sord)返回排序数据。也可以使用具有回调函数的sort数组方法来实现该功能,但我想,您不希望在JSFiddle中实现jqGrid所需的服务器行为的完全模拟。