Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Sorting is not happening on the click of column headers. The "Loading..." shows up briefly over the data, but the columns are not getting sorted. Also, "OnSortCol" event gets triggered and I could see the alert popping up. But, the sorting is not happening. Any help on this is appreciated.

Please find below the code:

C#
jQuery("#jqTable").jqGrid({
            dataType: 'local',
            altRows: true,
            colNames: ["ID", "First name", "Last name", "Role", "Group", "Location", "Status", "<div class='selectgriddiv'>Select all</div>" + "<div class='selectgriddiv'><input type='checkbox' id='chkAll' name = 'chkSelectAll' onclick='checkBox(event)' /></div>", "Role Id", "Is Primary"],
            colModel: [
                {
                    name: "Id", index: "Id", align: "center", sortable: false, formatter: function (cellvalue, options, rowObject) {
                        if (_RoleId > rowObject.RoleId && rowObject.IsPrimary == false)
                            return "<img src='../images/ProcessImages.aspx.jpg' alt='my image' />" + "<br/>" + "<a id='linkToProfile' href='#' onclick='RedirectUsers(" + rowObject.Id + "," + rowObject.RoleId + "," + '"' + rowObject.RoleName + '"' + ")' >" + rowObject.Id + "</a>";
                        else if (_RoleId == rowObject.RoleId || _RoleId < rowObject.RoleId) {
                            return "<img src='../images/ProcessImages.aspx.jpg' alt='my image' />" + "<br/>" + rowObject.Id;
                        }
                        else if (_RoleId == rowObject.RoleId && _IsPrimary == false) {
                            return "<img src='../images/ProcessImages.aspx.jpg' alt='my image' />" + "<br/>" + rowObject.Id;
                        }
                    }
                },
                {
                    name: "FirstName", index: "FirstName", sortable: true, align: "left"
                },
                { name: "LastName", index: "LastName", sortable: true, align: "left" },
                {
                    name: "RoleName", index: "RoleName", sortable: true, align: "left", formatter: function (cellvalue, options, rowObject) {
                        return rowObject.RoleName
                    }
                },
                {
                    name: "MyGroups", index: "MyGroups", sortable: true, align: "left", formatter: function (cellvalue, options, rowObject) {
                        if (rowObject.MyGroups != null)
                            return rowObject.MyGroups
                        else
                            return ""
                    }
                },
                {
                    name: "LocationName", index: "LocationName", sortable: false, align: "left", formatter: function (cellvalue, options, rowObject) {

                        if (rowObject.RegionName != null && rowObject.LOName != null && rowObject.CentreName != null) {
                            return rowObject.RegionName + '.<br/>' + rowObject.LOName + '.<br/>' + rowObject.CentreName
                        }
                        else if (rowObject.RegionName == null && rowObject.LOName != null && rowObject.CentreName != null) {
                            return rowObject.LOName + '.<br/>' + rowObject.CentreName
                        }
                        else if (rowObject.RegionName != null && rowObject.LOName == null && rowObject.CentreName != null) {
                            return rowObject.RegionName + '.<br/>' + rowObject.CentreName
                        }
                        else if (rowObject.RegionName != null && rowObject.LOName != null && rowObject.CentreName == null) {
                            return rowObject.RegionName + '.<br/>' + rowObject.LOName
                        }
                        else if (rowObject.RegionName != null && rowObject.LOName == null && rowObject.CentreName == null) {
                            return rowObject.RegionName
                        }
                        else if (rowObject.RegionName == null && rowObject.LOName != null && rowObject.CentreName == null) {
                            return rowObject.LOName
                        }
                        else if (rowObject.RegionName == null && rowObject.LOName == null && rowObject.CentreName != null) {
                            return rowObject.CentreName
                        }
                        else if (rowObject.RegionName == null && rowObject.LOName == null && rowObject.CentreName == null) {
                            return rowObject.LocationName
                        }
                    }
                },
                {
                    name: "StatusDetail", index: "StatusDetail", sortable: false, align: "left", formatter: function (cellvalue, options, rowObject) {
                        return rowObject.StatusDetail
                    }
                },
                {
                    name: "Select", index: "Select", sortable: false, align: "center", resizable: false, sortable: false, formatter: function (cellvalue, options, rowObject) {
                        return "<input type='checkbox' id='chkFields' " + rowObject.Id + " class='fieldChk' onclick='fieldCheckBox(event)'/>";
                    }
                },
                {
                    name: "RoleId", index: "RoleId", hidden: true, formatter: function (cellvalue, options, rowObject) {
                        return rowObject.RoleId
                    }
                },
                {
                    name: "IsPrimary", index: "IsPrimary", hidden: true, formatter: function (cellvalue, options, rowObject) {
                        return rowObject.IsPrimary
                    }
                }
            ],
            sortorder: "desc",
            viewrecords: true,
            width: 728,
            height: 'auto',
            onSortCol: function (name, index) {
                alert(name);
            }
            
        });


Thanks,
B.R.S.K. Bharadwaj
Posted
Comments
Prasad Khandekar 19-Sep-13 10:00am    
Try adding sorttype to each of the sortable column. Also note taht client side sorting works for datatype as local for json a call is made to the specified url and server code needs to return the sorted data. The onSortCol gets called just before the call is made to to server or local sort routine. it basically allows you to stop the sorting.

Hello Ganesh,

For jQGrid if datatype other than "local" the sorting needs to get handled on the server side. For client side sorting the datatype must be "local" and each sortable column must have the sorttype property set (see this[^] link to know more about server side paging and client side sorting setup). The possible values for this property are

  • int/integer - for sorting integer
  • float/number/currency - for sorting decimal numbers
  • date - for sorting date
  • text - for text sorting
  • function - defines a custom function for sorting. To this function we pass the value to be sorted and it should return a value too.

The onSortCol function gets called immediately after the column header is clicked and just before sorting the data. If this event return 'stop' the sort processing is stopped and you can define your own custom sorting.

Regards,
 
Share this answer
 
Comments
bedathur_ganesh 20-Sep-13 6:30am    
Hi Prasad,

Thanks for the help. Sorting is not working even after I've added the sorttype property for the colModel. I've set the sorttype: "text".

I also tried to implement server side sorting by adding the below code:
loadComplete: function () {
jQuery("#jqTable").jqGrid('setGridParam', { datatype: 'json' });
}
Sorting is not working, yet. Any idea where it is going wrong?

Thanks,
B.R.S.K. Bharadwaj
Prasad Khandekar 20-Sep-13 9:27am    
Hello,

No client side implementation is required for server side sorting. You need to implement it on server side and just return the data. The jqgrid will post all the details for you to be able to sort the data on server side.
e.g. If your url parameter is 'server.php' then the sorting will have to be done in server.php page. This page will get following parameters
rows - Total number of rows per page
page - The page number being displayed
sidx - The name of the sort column
sord - The sort order (asc/desc)

Regards,
below solution worked for me. just include the loadComplete fucntion call
http://stackoverflow.com/questions/16378544/jquery-jqgrid-not-sorting-on-client-side
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900