How do I implement custom sort to a specific column after jqgrid has been generated?

The usage of sorttype as the function can be useful for any local datatype of jqGrid or in case of the usage loadonce:true jqGrid paremter with the “remote” datatypes ‘json’ or ‘xml’. If it is needed you can change the sorttype of any column dynamically.

I made the new demo for you to demonstrate the feature. At the begining the grid will be sorted by ‘Client’ column and the column contain will be interpret as the text string. The results are displayed below

enter image description here

Wenn we check the checkbox “Set custom sorttype function” the grid will be sorted as displayed on the next picture

enter image description here

To implement such sorting I defined the function

var myCustomSort = function(cell,rowObject) {
    if (typeof cell === "string" && /^test(\d)+$/i.test(cell)) {
        return parseInt(cell.substring(4),10);
    } else {
        return cell;
    }
}

and the ‘change’ event handler on the checkbox

$("#customsorttype").change(function() {
    var isChecked = $(this).is(':checked');
    if (isChecked) {
        cm.sorttype = myCustomSort;
    } else {
        cm.sorttype = "text";
    }
    grid.trigger("reloadGrid");
});

where grid is $("#list").

If one click on the checkbox one more time the original sorting method with sorttype:"text" will be used.

Leave a Comment