jqGrid插件不会在URL中设置页码

时间:2012-10-22 13:46:00

标签: jqgrid

我的jqGrid插件有问题。如果我在第2页,我想更改任何列的排序方向,服务器作为URL参数page=1(但应该是page=2)出了什么问题?

<table id="list" class="scroll " cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>
$(function () {

    $("#list").jqGrid({
        url: '/Control/UsersGridData/',
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Name', 'User ID', 'Is Active', 'Last Action',  'Country'],
        colModel: [
  { name: 'Name', index: 'Name', width: 120, align: 'center', sortable: true, search: true },
  { name: 'UserID', index: 'UserID', width: 120, align: 'center', sortable: true, search: true },
  { name: 'IsActive', index: 'IsActive', width: 50, align: 'center', sortable: true, search: true },
  { name: 'LastAction', index: 'LastAction', width: 90, align: 'center', sortable: true, search: true },
  { name: 'Country', index: 'Country', width: 40, align: 'center', sortable: true, search: true }],
        pager: jQuery('#pager'),
        rowNum: 20,
        rowList: [5, 10, 20, 50],
        sortname: 'Name',
        sortorder: "desc",
        viewrecords: true,
        imgpath: '/scripts/themes/coffee/images',
        width: '800'
    });
});

1 个答案:

答案 0 :(得分:2)

将明确更改页码(请参阅列标题上the line事件处理程序内部使用的sortData代码click

例如,您可以更改onSortCol事件处理程序中的值。您可以直接从寻线者的相应<input>字段中获取当前页面的值:$(pagerIdSelector).find("input.ui-pg-input").val()其中var pagerIdSelector = $(this).jqGrid("getGridParam", "pager")

在我看来,将pager值重置为1是正确的行为。让我们有一个20页的网格,数据按“asc”顺序按第一列排序。如果显示第二页并且用户单击第一列。在这种情况下,排序顺序应该反转为“desc”。在当前页面的页码大约为18的情况下(仅当所有页面都满行时才为18)。如果用户单击任何其他列并且网格应按另一列排序,则当前页面上显示的行可以在20个页面上具有任何位置,并按新标准。因为jqGrid在每次网格排序时将page的值重置为1。

更新:您的代码的其他一些评论对您的主要问题并不重要:

  • jqGrid在初始化期间将pager: jQuery('#pager')替换为pager: '#pager'。所以它无法通过id进行不必要的元素搜索。最好总是使用pager选项的id-selector语法。
  • imgpath选项标识deprecated,它将被jqGrid忽略。所以你应该删除它。
  • 以同样的方式将jqGrid所需的HTML片段调用为<table id="list"></table><div id="pager"></div>。所有其他属性均已弃用,jqGrid将不会使用。
  • 属性sortable: true, search: true是默认属性(请参阅the documentation),应删除它。同样,如果您使用与index相同的name,则可以删除index属性的定义。如果您希望在大多数列中使用align: 'center',则可以更改和简化代码,如果您要更改align属性的默认值。在使用jqGrid的cmTemplate: {align: 'center'}选项后(有关详细信息,请参阅here),您可以删除所有列中的align: 'center'。完成所有更改后,colModel的所有项目的定义都会简短明了,如{ name: 'Name', width: 120 }