2

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.

bcm
  • 5,470
  • 10
  • 59
  • 92
  • (I've upgrade the jqgrid version to version 3.8.2 in the hopes of getting this to work) – bcm Mar 14 '11 at 04:53

1 Answers1

9

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I'm using a remote json datatype, which cannot be loadonce (grid needs to refresh at times). Basically I want to check for null and the cell data is html. I tried this... but still trying to get it to work whenever I click on the sort header text. annotCustomSort = function (cell, rowObject) { return parseInt(cell.length); } annotCol = grid.getGridParam("colModel")[2]; annotCol.sorttype = annotCustomSort; – bcm Mar 15 '11 at 22:31
  • @Brandon: How I wrote you before the `sorttype` will **be used only for the local datatype** of jqGrid or in case of the usage `loadonce:true` jqGrid parameter with the "remote" datatypes 'json' or 'xml'. For remote datatypes **without** `loadonce:true` it will be just ignored. In case of remote datatypes the server is full responsible for the data sorting. the server receive parameters `sidx` and `sord` which defines the sorting required. So in your case the any implementation of the sorting must be done on the server or you have to use `loadonce:true`. – Oleg Mar 15 '11 at 22:47
  • @Oleg why is it that for default sort of cell value done on the server, it is able to check null correctly (my subject column for example works fine), but for the cell with html value, the default sort doesn't sort by null. What is the best sorttype to use then, do I need to use custom sorttype? – bcm Mar 15 '11 at 22:58
  • @Brandon: Sorry, but I really don't understand what you mean. The `sorttype` is designed **only for local datatypes** and I can find this only in `addLocalData` method. If in your case the function `sorttype` will do called with remote datatype it must be a bug. Why you make any experiments with `sorttype` and remote datatypes? Why you not make full sorting of data inclusive all NULL values on the server how it should be done? – Oleg Mar 15 '11 at 23:11
  • @Oleg oic... I thought sorttype could be implemented via front-end or back-end depending on data source. I'm not experimenting... I'm just trying to sort a column with null/not null html values by header-click-sort, but it is not working. – bcm Mar 15 '11 at 23:15
  • @Brandon: With remote datatype all is very clear. The client say what page, and how sorted the data need and the server should make all the work. The jqGrid just fill the grid row by row reading there from the server response. – Oleg Mar 15 '11 at 23:18
  • @Oleg I would have thought sorting can be overwritten once data is refreshed and retrieved from remote. – bcm Mar 15 '11 at 23:22
  • @Brandon: Sorry, but I see it is possible only with temporary changing of datatype to 'local' and then sorting. It is exactly what 'loadonce:true' do. If you need refresh the data from the server you should just change the `datatype` back to 'json' and reload the grid. – Oleg Mar 15 '11 at 23:25