问题
I want to load a grid with default sorting on it's one of field. I done this by adding sortname and sortorder to my grid,but when grid is loaded sorting sign is shown on a header of that field.But records are sorted when i click on pagination next button not on grid load.
Is anyone know why it is not sorting on grid load?
@UPDATE: hi kees, I am using my datatype as XML and my config. is as follows:
jQuery("#userList").jqGrid({
url: '<%=request.getContextPath()%>/userJqGrid?q=1&action=fetchData&userCookie=<%= encodedCookie%>',
datatype: 'xml',
mtype: 'GET',
colNames:['<%= userProp.getProperty(userColNames[0])%>',
'<%= userProp.getProperty(userColNames[1])%>',
'<%= userProp.getProperty(userColNames[2])%>',
'<%= userProp.getProperty(userColNames[3])%>',
'<%= userProp.getProperty(userColNames[4])%>',
'<%= userProp.getProperty(userColNames[5])%>'
],
colModel:[
{name:'<%= userColNames[0]%>',index:'<%= userColNames[0]%>',
width:120,sortable:true,editable:true,editrules:{required:true},formoptions:{rowpos:1, elmprefix:'*'}},
{name:'<%= userColNames[1]%>',index:'<%= userColNames[1]%>',
width:130,sortable:true,editable:true},
{name:'<%= userColNames[2]%>',index:'<%= userColNames[2]%>',
width:100,sortable:true,editable:true,editrules:{required:true},formoptions:{rowpos:3, elmprefix:'*'}},
{name:'<%= userColNames[3]%>',index:'<%= userColNames[3]%>',
width:180,sortable:true,editable:true,editrules:{email:true,required:true},formoptions:{rowpos:4, elmprefix:'*'}},
{name:'<%= userColNames[4]%>',index:'<%= userColNames[4]%>',
width:100,sortable:true,editable:true},
{name:'<%= userColNames[5]%>',index:'<%= userColNames[5]%>',
width:100,sortable:true,editable:true},
],
pager:'#pager1',
rowNum:'<%=appProp.getProperty("per_page_records")%>',
height:'auto',
viewrecords:true,
loadonce:true,
sortable:true,
width:'100%',
gridview: true,
autowidth:true,
shrinkToFit:false,
ignoreCase:true,
editurl:'<%=request.getContextPath()%>/userJqGrid?q=1&action=addData&userCookie=<%=encodedCookie%>',
caption: 'User Records',
sortname:'<%=userColNames[14]%>',
sortorder:'asc',
onSelectRow: function (id){
checkAndSetCookie();
},
onSortCol : function(){
checkAndSetCookie();
},
onPaging : function(){
checkAndSetCookie();
},
onSearch : function(){
checkAndSetCookie();
},
loadComplete: function(){
checkAndSetCookie();
}
});
回答1:
If you use remote datatype
(datatype: 'xml'
or datatype: 'json'
) the server is responsible for sorting of the data at the first load. If you use loadonce: true
and want to sort the data only on the client side you have to reload jqGrid directly after the first loading. The corresponding code could be about the following
loadComplete: function (data) {
var $this = $(this),
datatype = $this.getGridParam('datatype');
if (datatype === "xml" || datatype === "json") {
setTimeout(function () {
$this.trigger("reloadGrid");
}, 100);
}
}
UPDATE: Free jqGrid fork has the option forceClientSorting: true
, which can be used in combination with loadonce: true
option. The option forceClientSorting: true
force client side sorting and filtering. It makes the code described in the answer unneeded.
来源:https://stackoverflow.com/questions/8118816/default-sorting-of-jqgrid-on-particular-field-when-grid-loads