1

My jqGrid contains a special column whose value is generated by another value, like the Duration Span column, its real value is in seconds like 3690s, and I should transform it to a formmatted string 'Hour : Minute : Second', in this cause it is '1:1:30'. Now, I want to it works well in sortting, so I write a customize function to handle this:

1, Following code fragment is column definition:

{
    name : 'time',
    index : 'time',
    align: 'center',
    width :  '12%',
    sorttype : sortTimeFuc
} 

2, Following code fragment is sort function:

var sortTimeFuc = (function(cell) {
    var a = cell.split(':');
    var value = parseInt(a[2]) + parseInt(a[1]) * 60 + parseInt(a[0]) * 3600;
    return value;
});

But it work incorrectly as below:

enter image description here

Can anybody gives me help? Thanks so much.

Brady Zhu
  • 1,305
  • 5
  • 21
  • 43

2 Answers2

1

It seems to me that you make some errors. First of all you should remove parentheses over the function. Unneeded parentheses can follow to invocation of the function. The second and the most important error which I see in your code is the usage of parseInt without the second parameter 10. The last error is the usage of width : '12%'. jqGrid don't support '%' inside of width. Instead of that you can use just width: 12 and specify width of the total grid or use autowidth: true. Because the option shrinkToFit: true is default option if will follow to scaling the width of the columns based on the total width of jqGrid and width property of the column, which will define the proportion between the columns.

So the resulting code could be as the following

{
    name: 'time',
    align: 'center',
    width: 12,
    sorttype: function (cell) {
        var a = cell.split(':');
        return parseInt(a[2], 10) + parseInt(a[1], 10) * 60 +
            parseInt(a[0], 10) * 3600;
    }
}

See the answer as an example of very close implementation of custom sorting.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks all the same. I firgue it out in other way. – Brady Zhu Feb 05 '13 at 07:23
  • @BradyChu: You are welcome! Next time you should better invest more your time before posting new question. You spend time of other people by posting unneeded questions. – Oleg Feb 05 '13 at 07:54
  • I'm sorry for that, your suggestion is good that I will take a serious thinking before I post any question on this platform. – Brady Zhu Feb 05 '13 at 09:48
  • @BradyChu: I think that in your case the usage of no `sorttype` which means `sorttype: "text"` is the best choice. – Oleg Feb 05 '13 at 10:15
  • Thanks! 'text' is sort type by default. – Brady Zhu Feb 06 '13 at 08:50
0

Because the merged time string meet the sortting condition of string, so just set the sorttype of this column to 'text'. Problem solved.

Brady Zhu
  • 1,305
  • 5
  • 21
  • 43