jqgrid + sorting of number with blanks

2019-05-17 10:57发布

问题:

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.

回答1:

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.



标签: jqgrid