为什么每次单击列标题时都无法对 jqgrid 中的网格进行排序?
我真的很困惑,每次我单击列标题时,使用 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没有问题。唯一的问题是我无法按降序对它们进行排序。每次我单击列标题时,它只会按升序排序,如果我再次单击,则不会发生降序排序。有什么问题吗?
I was really confused that my program using jqgrid won't sort (descending) everytime I clicked the column header? I tried creating a program where I use local data (.json data) and it work great in sorting when I clicked the column header. So what's the problem with the first one? I am using the data from client server....
Here's my javascript code:
$("#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');
that's my code in javascript where i can't sort my jqgrid...
my process.php code:
<?php
print(file_get_contents("http://localhost/" .... "?json=" . $_GET["json"]));
?>
There's no problem in loading of data to the jqgrid. The only problem is that I cannot sort them in descending order. Everytime i clicked a column header, it only sorts ascending, and if i clicked again, no descending happen. What's the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在必填字段的 colModel 中使用 sortable: true,如下所示:
您现在应该能够正确排序。
You should use sortable: true in your required fields' colModel as follows:
You should now able to sort properly.
使用来自服务器的数据时,您必须提供随时可用的数据:有序数据和分页数据。
为此,jqgrid 在请求中发送变量
sidx
和sord
,其中包含列名称和排序(“desc”表示降序)。请参阅教程以获取更多帮助和 PHP 示例。
When using data from server, you must provide ready-to-use data: both ordered and paginated.
To do so, jqgrid sends in the request the variables
sidx
andsord
, containing the name of the column and the ordering ('desc' for descending).See the tutorial for further help and a PHP example.
尝试使用
loadonce:true
;,您正在使用loadonce:false。
它在这里说,
如果设置了这个标志设置为 true 时,网格仅从服务器加载数据一次(使用适当的数据类型)。第一次请求后,数据类型参数会自动更改为本地,所有进一步的操作都在客户端完成。寻呼机的功能(如果有)被禁用。
Try using
loadonce:true
;,You are using loadonce:false.
It says in 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.