1

Using jqGrid 4.5.2. In my dataset, I have a column that can have either integers or text in it. I have the sorttype set to text.

The data that's returned going to be mixed in the grid, and could contain both characters or numbers. It may contain only letters, only numbers or a mixture of both. If I click on the column to sort it in descending order, it goes:

400
350
300
200
1100
1020
1010
1000
100

The numbers can vary, as can the letters. Is there a way to define a custom sorttype function to properly sort a number like a number and a string like a string in the same column? If so, how?

I've found examples where a CASE type statement was used, but since the contents of the cells will not be known, I can't do that. Would appreciate any thoughts.

EDIT

Based on @Oleg's answer, I've tried a couple different ways to implement the sorttype as a function. However, I can't seem to make it fire. It will sort the data, but appears to sort everything as a string. I've got some console.log statements in the code that should dump out whatever values are there, but nothings being shown.

First attempt was to have the function inside the colModel.

{name:"MySource",
index:"MySource",
width:40,
align:"left",
sorttype: function (cell, rowObject) {
    if (typeof cell === "string" && /^test(\d) + $/i.test(cell)) {
        console.log("inside if custom sort - cell = " + cell );
        return parseInt(cell);
        }
    else {
        console.log("else - cell = " + cell );
        return cell;
        }
    },

Second attempt was after looking at another code example you had on a similar question and creating a function & then calling that function from the sorttype.

Here is the colModel:

{name:"MySource",
index:"MySource",
width:40,
align:"left",
sorttype: function (cell) {
        return myCustSort (cell) ;
        }
    }
}   

and the myCustSort function:

function myCustSort (myCell) {
    if (typeof myCell === "string" && /^test(\d) + $/i.test(myCell)) {
        console.log("inside if custom sort - cell = " + myCell );
        return parseInt( myCell);
        }
    else {
        console.log("else - cell = " + myCell );
        return myCell;
        }
} // end myCustSort

When I click on that column header, I can see the data in the grid sorting, but neither of the displays puts anything in the console log. Shouldn't it fire & do so when the column header is clicked to sort it, or am I missing something?

Thanks!

steve_o
  • 1,243
  • 6
  • 34
  • 60

2 Answers2

6

you can just set sorttype in colModel as

sorttype: 'number'
sohaiby
  • 1,168
  • 3
  • 24
  • 39
Albin Mathew
  • 604
  • 9
  • 19
2

You can use sorttype defined as function. Inside of the function you can use parseInt to test whether the value is integer or not. In case of integer value the function can return the integer value. In case of non-integer the function can return original string value. See the answer for the code example.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @oleg..can you please give your sugesstion on this..when you have time..i am really worried about my approach to mvc. http://stackoverflow.com/questions/20514446/how-to-call-model-methods-in-controller-in-asp-net-mvc – Sai Avinash Dec 11 '13 at 08:48
  • @oleg - I looked at your example, but am having issues trying to make it work. I don't believe it is firing the `sorttype` function. I've edited the question to show what I've attempted. – steve_o Dec 11 '13 at 13:49
  • @steve_o: You don't posted JavaScript code which creates the grid. Some parts of the code could be very important. You wrote "I can't seem to make it fire." It's important to understand that `sorttype` will be used only if you use **client side sorting**. If `sorttype` function not called at all it means that you probably use `datatype: "json"` or `datatype: "json"` and you don't set `loadonce: true`. In the case jqGrid just send the sorting column name and the direction of the sorting to the server and the server is responsible for sorting. So you should implement sorting in your server code. – Oleg Dec 11 '13 at 22:58