3

I have a column in my JQgrid which says 'size'. The return type is integer. I have added:

{
  name:'sizeKloc',
  index:'sizeKloc', 
  width:60, 
  editable:false, 
  sorttype: 'int', 
  align:'right'
},

There are some cases where the value is passed as null.

My column has following values - 0, 3, 5, null (which is blank or space), 1, null (which is blank or space), null (which is blank or space), 2, 3

But when I try to sort ASC, the blanks should be first followed by actual number sorting which doesn't happen.

Any help is appreciated.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
komal salvi
  • 33
  • 1
  • 6

1 Answers1

3

If you really want to hold null and distinguish it from 0 you can use custom sorting instead of usage of sorttype: 'int'. The usage is very simple. You need just define replacements of the values which can be used for sorting instead of the original values of your data.

In your case it can be for example

{name:'sizeKloc',index:'sizeKloc', width:60, editable:false, align:'right'
    sorttype: function (cellValue) {
        return cellValue === null ? -1000 : Number(cellValue);
    }},

or

{name:'sizeKloc',index:'sizeKloc', width:60, editable:false, align:'right'
    sorttype: function (cellValue) {
        var num = parseInt(cellValue, 10);
        return isNaN(num) ? -1000 : num;
    }},

The exact code depend more from the format and type of the data which you use.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Hi Oleg. This worked. Thank u so much, But i am having another concern with this. When i do an ascending sort, the blanks are displayed after the numbers, but i want the blanks to come before the numbers. Any help is appreciated. Thank you – komal salvi May 15 '12 at 14:33
  • @komalsalvi: You can test `cellValue` not only for `null`, but for empty string too. You can returns either small number (like -1000) or large number in the case from `sorttype`. I think it should solve the problem. – Oleg May 15 '12 at 14:40
  • @komalsalvi: It seems that your last comment is cut. I think one can implement any local sorting by usage `sorttype` as function. About the usage ranking I can you recommend to read [the answer](http://stackoverflow.com/a/4842450/315935) and the last version of the corresponding demo [here](http://www.ok-soft-gmbh.com/jqGrid/Ranking2.htm). – Oleg May 16 '12 at 07:44