This is a migrated thread and some comments may be shown as answers.

Kendo Grid sorting a number type column when there are nulls, 0's, positive and negative values

3 Answers 190 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 24 Jun 2014, 04:42 PM

I have a currency field set up on a KendoUI Grid like so:

DailyPriceChange: { type: 'number' },

It's column definition is as follows (as suggested by numerous other posts available here and elsewhere):
                {
                    title: 'Daily Price Change',
                    field: 'DailyPriceChange',
                    template: '#= IA.Kendo.Renderers.renderCurrency(DailyPriceChange) #',
                    attributes: { 'class': 'numeric' },
                    sortable: {
                        compare: function(a, b) {
                            return a === b ? 0 : ((a > b) ? 1 : -1);
                        }
                    }
                }

As an aside, the columns for the grid are actually built up in a private function that is used to set the .columns property on the datasource. The template just adds some formatting, and is not of course the underlying value that is being sorted.

My issue is that with or without the custom sortable function object, the data does not sort ASC correctly in IE9+ or FireFox (even recent), but does quite well in Chrome (any recent version). I would like to debug into the custom sortable object in Chrome Developer Tools, but a breakpoint on the compare function body is never hit when sorting, which makes me wonder if the code is even being executed. I even tried adding an alert() inside the function, but it is never seen. However, I can see the function definition when I examine the sortable property of the column in Developer Tools.

I have made a work-around to set all null values to 0 when the data is fetched (by using the parse override in the fields spec), but that not acceptable to the user community. 

3 Answers, 1 is accepted

Sort by
0
CS
Top achievements
Rank 2
answered on 25 Jun 2014, 03:21 PM
If I'm not mistaken you have to adjust that compare function to fit your property name
{
    title: 'Daily Price Change',
    field: 'DailyPriceChange',
    template: '#= IA.Kendo.Renderers.renderCurrency(DailyPriceChange) #',
    attributes: { 'class': 'numeric' },
    sortable: {
        compare: function(a, b) {
             return a.DailyPriceChange === b.DailyPriceChange ? 0 : ((a.DailyPriceChange > b.DailyPriceChange) ? 1 : -1);
            }
        }
    }
0
CS
Top achievements
Rank 2
answered on 25 Jun 2014, 03:22 PM
Onabai's example never gets old Custome Sorting algorithms
0
Petur Subev
Telerik team
answered on 26 Jun 2014, 03:33 PM
Hello Jonathan,

In case you still struggle please create a small demo to demonstrate your case so we can have a look and see what goes wrong.

Regards,
Petur Subev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Jonathan
Top achievements
Rank 1
Answers by
CS
Top achievements
Rank 2
Petur Subev
Telerik team
Share this question
or