steve_o
steve_o

Reputation: 1243

jqGrid - sorting numbers and text

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!

Upvotes: 1

Views: 11033

Answers (2)

Albin Mathew
Albin Mathew

Reputation: 604

you can just set sorttype in colModel as

sorttype: 'number'

Upvotes: 6

Oleg
Oleg

Reputation: 222007

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.

Upvotes: 2

Related Questions