ANP ANP - 1 year ago 150
Javascript Question

How do i sort a column that has multiple types of values? [Trirand JQGrid 4.6]

i have used q jqgrid, 'groupd header row config' version of jqgrid in the demo website.

enter image description here

it generates the grid. but assume the the senario where

column can contains numbers and string as in some rows there are numbers an in some rows there are string chars, sorting truns off and it only works for strings chars.

scenario:Assume the table has five rows and
column has the following data in five rows:

this is good


number 123 number



i have added the sorting option to colModel ussing sort:true but this only sorts if the notes column has string chars for all the rows but does not if it has a conbination of number rows and string char rows. how do i fix this?

Answer Source

jqGrid contains multiple possibilities to customize the sorting. First of all it's important to mention that all the below possibilities have sense only if you use datatype: "local" or some remote datatype ("json", "jsonp", "xml") in combination with loadonce: true. In the last case the datatype will be changed to the "local" after the first loading of data from the server.

To sort the local data in the column jqGrid have to compare the values. It does some preliminary steps. First of all if fills the array with items, which maps the content from one column to the rowid. Thus you can define

sorttype: function (cellValue, item) {
    return cellValue; // another value as cellValue can be returned

It gives you the first way of custom sorting. You can for example use RegEx to extract the "number"-part of the information from the callValue and to return it from sorttype (which you can define in colModel for the column notes). See the old demo created for the answer for more details.

The second way: defining of sortfunc callback in colModel. The callback has the prototype

sortfunc: function (a, b, direction) {
    // should return 0, 1 or -1

in old versions of jqGrid and

sortfunc: function (a, b, direction, aItem, bItem) {
    // should return 0, 1 or -1

in free jqGrid. The sortfunc allows you to implement any custom sorting behavior which you need. See the demo created for the issue for the code example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download