为什么每次单击列标题时我都无法在jqgrid中对网格进行排序?

时间:2011-03-15 08:25:27

标签: jquery sorting jqgrid-php client-side

我真的很困惑,每次点击列标题时,使用jqgrid的程序都不会排序(降序)?我尝试创建一个程序,我使用本地数据(.json数据),当我点击列标题时,它在排序方面很有用。那么第一个问题是什么?我正在使用来自客户端服务器的数据....

这是我的javascript代码:

  $("#btnQueryMainAccountGroups").click( function() {
    var params = {
      "SessionID": $("#eSessionID3").val(),
      "dataType": "data"
    }
    $('#tblData').setGridParam({
      url:'process.php?path=' + encodeURI('masterData/data') + '&json=' + encodeURI(JSON.stringify(params)), 
      datatype: olSettings.ajaxDataType,  
    });
    $('#tblData').trigger('reloadGrid');
    }); 

    $("#tblData").jqGrid({
    url: '',
    datatype: '',
    jsonReader : {
      root: function(obj) {
        var root = [];

    if  ('error' in obj) 
    {
      showMessage(obj.error['class'] + ' error: ' + obj['error']['msg']);
    }
    else
    {
      $.each(obj['result']['main']['rowdata'], function(rowIndex, rowDataValue) {
        var row = {};
        $.each(rowDataValue, function(columnIndex, rowArrayValue) {
          var fldName = obj['result']['main']['metadata']['fields'][columnIndex].name;
          row[fldName] = rowArrayValue;                
        });
        root[rowIndex] = row;
      });
    };
    return root;
  },
  page: "result.main.page",
  total: "result.main.pageCount",
  records: "result.main.rows",
  repeatitems: false,
  id: "0"
},
serializeGridData: function(postData) {
  var jsonParams = {
    'SessionID': $('#eSessionID3').val(),
    'dataType': 'data',
    'recordLimit': postData.rows,
    'recordOffset': postData.rows * (postData.page - 1),
    'rowDataAsObjects': false,
    'queryRowCount': true,
    'sort_fields': postData.sidx
  };

  return 'json=' + JSON.stringify(jsonParams);
},

},
colNames:['ID','Code', 'Description','Type'],
colModel:[
  {name:'group_id'},
  {name:'group_code',align:'center',width:100},
  {name:'group_desc'},
  {name:'type'}
],

viewrecords: true,
rowList:[5,10,50,100],
pager: '#tblDataPager',
sortname: 'group_desc',
sortorder: 'asc',
rowNum:5,
loadonce:false,
caption: "MainGroup"
});

$("#tblData").setGridWidth($(window).width() - 70);
$("#tblData").jqGrid('sortableRows');

这是我在javascript中的代码,我无法对jqgrid进行排序... 我的process.php代码:

 <?php 
     print(file_get_contents("http://localhost/" .... "?json=" . $_GET["json"]));
 ?>

将数据加载到jqgrid没有问题。唯一的问题是我无法按降序对它们进行排序。每次我点击一个列标题,它只会按升序排序,如果我再次点击,则不会发生降序。有什么问题?

3 个答案:

答案 0 :(得分:1)

您应该在必填字段'colModel中使用 sortable:true ,如下所示:

colModel:[
 {name:'group_id', sortable: true},
 {name:'group_code',align:'center',width:100, sortable: true},
 {name:'group_desc', sortable: true},
 {name:'type', sortable: true}
],

您现在应该能够正确排序。

答案 1 :(得分:0)

使用来自服务器的数据时,您必须提供现成的数据:有序数据和分页数据。

为此,jqgrid在请求中发送变量sidxsord,其中包含列的名称和排序(降序的'desc')。

请参阅tutorial以获取进一步的帮助和PHP示例。

答案 2 :(得分:0)

尝试使用 loadonce:true; , 您使用的是 loadonce:false

here,

中说

If this flag is set to true, the grid loads the data from the server only once (using the appropriate datatype). After the first request, the datatype parameter is automatically changed to local and all further manipulations are done on the client side. The functions of the pager (if present) are disabled.