Forum



11:17

07/03/2012

Hi,
I am a newcomer to jqGrid and I am stuck up with sorting in my grid.
Below is my code for jqGrid.The data is displayed properly but still i am not able to sort the columns.
$(function () {
$("#usrsearchgrid").jqGrid({
datatype: function (pdata) { getData(pdata); },
height: 250,
viewrecords: true,
pager: jQuery('#pager'),
loadtext: 'Loading data...',
recordtext: "{0} - {1} of {2} records",
emptyrecords: 'No User exists',
pgtext: 'Page {0} of {1}', //Paging input control text format.
colNames: ['country', 'emailid', 'firstName', 'lastname'],
colModel: [
{ name: 'country', width: 150, index: 'country', sortable: true },
{ name: 'emailID', width: 200, index: 'emailID', key: true, sortable: true,sorttype:"string"},
{ name: 'firstName', width: 200, index: 'firstName', sortable: true },
{ name: 'lastName', width: 200, index: 'lastName', sortable: false }
],
sortname: 'emailID',
sortorder: "desc",
rowNum: "2", // PageSize.
caption: "JSON Example"
});
});
function getData(pData) {
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'JQGrid.aspx/getUsers',
data: '{}',
dataType: "json",
success: function (data, textStatus) {
if (textStatus == "success")
ReceivedClientData(JSON.parse(getMain(data)));
},
error: function (data, textStatus) {
alert('An error has occured retrieving data!');
}
});
}
function ReceivedClientData(data) {
var thegrid = $("#usrsearchgrid");
thegrid.clearGridData();
for (var i = 0; i < data.records; i++)
//grid.jqGrid('addRowData', myData.length + i, newData[newData.length - i - 1], "first");
thegrid.addRowData(i + 1, data.rows[i]);
}
function getMain(dObj) {
if (dObj.hasOwnProperty('d'))
return dObj.d;
else
return dObj;
}
Can you please suggest what additions should i make in this..??
15:34

Moderators
30/10/2007

Hello,
In your case you should done the sorting server side.
See into the docs which parameters are posted when the user click to sort .
Regards
For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.
05:54

07/03/2012

Thanks Tony for your help.
I am able to achieve this through server side.
Can you refer a good article or explain what approach I should folow for making the client side sorting work..??
Dear uriel,
In order to get the column name and the sortorder the you need to mention the column in the ajax call in the below format:
function getData(pData) {
$.ajax({
type:
'POST',
contentType:
"application/json; charset=utf-8",
url:
'JQGrid.aspx/getUsers',
data:
"{'pSortColumn':'" + $('#usrsearchgrid').getGridParam("sortname") +
"','pSortOrder':'" + $('#usrsearchgrid').getGridParam("sortorder") + "'}",
dataType:
"json",
success:
function (data, textStatus) {
if (textStatus == "success")
ReceivedClientData(JSON.parse(getMain(data)));
},
error:
function (data, textStatus) {
alert(
'An error has occured retrieving data!');
}
});
}
On server side the pSortColumn and pSortOrder will be accessible.
public static string getUsers(string pSortColumn, string pSortOrder);
I think this will help.
Most Users Ever Online: 715
Currently Online:
52 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
OlegK: 1255
markw65: 179
kobruleht: 144
phicarre: 132
YamilBracho: 124
Renso: 118
Member Stats:
Guest Posters: 447
Members: 11373
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 8
Topics: 10592
Posts: 31289
Newest Members:
, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66