bcm
bcm

Reputation: 5500

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

Is there a method I can use to over-write/insert a custom function "sorttype" for a specific column in the colModel after it has been populated using javascript (jQuery)?

I've found an example here: http://www.ok-soft-gmbh.com/jqGrid/CustomSorttype1.htm, where sorttype is implemented with the initial settings, but what I need to change it after.

Tried:

var attName = grid.getGridParam("colModel")[1].name;
        grid.setColProp(attName, { sorttype: function (cell) {
            if (cell == '<div>x</div>') { return '0' } else { return '1' };
        }
        });

but doesn't work.

Upvotes: 2

Views: 13884

Answers (1)

Oleg
Oleg

Reputation: 221997

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.

Upvotes: 9

Related Questions