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

Date given as string doesn't sort properly

5 Answers 951 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Madzie
Top achievements
Rank 1
Madzie asked on 09 Jan 2014, 05:11 AM
I am supplying a date in M/d/yyyy as string in a template for the column :
But when you sort the OrderDate column, it doen't seem to sort the dates properly - it puts the column values arbitrarly. Any clues why ??

model: {
                id: "MSOrderNumber",
                fields: {
                    OrderNumber: { type: 'string' },
                    OrderDate_String: { type: 'string' },

columns: [
            { field: "OrderNumber", title: "Order Number" },
            { field: "OrderDate_String", title: "Order Date", template: '#= kendo.toString(ConvertStringToKendoDate(OrderDate_String), "M/d/yyyy" ) #' },

Using this little function to get a formatted Date from string
function ConvertStringToKendoDate(value)
{
    var escapeRegExp = function (str) {
        return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
    }
    var replaceAll = function (find, replace, str) {
        return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
    }
    var find = '-';
    if (value != null && value != undefined && value != '') {
        value = replaceAll(find, '/', value)
        value = new Date(value);
    }
      return value;

}

5 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 09 Jan 2014, 08:38 AM
Hi Madhuri,

The data source will sort those fields as strings because you have configured it that way. To sort them properly you must set the type of the field to "date". Here is a live demo: http://jsbin.com/iHUJeXEq/1/edit


Regards,
Atanas Korchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
David
Top achievements
Rank 2
answered on 09 Jan 2015, 10:38 PM
Sorry for the thread resurrecting...

I'm not sure why I'm not able to sort my date. I looked over the example you posted. My data has dates that are formatted "M/d/yy"... but I can't for the life of me get Kendo to know that it's not a string.

$("#giving-history-grid").kendoGrid({
            dataSource: {
                sort: { field: "date", dir: "asc" },
                schema: {
                    model: {
                        fields:{
                            date: {type: "date"},
                            fund: {type: "string"},
                            desc: {type: "string"},
                            name: {type: "string"},
                            type: {type: "string"},
                            amt: {type: "number"}
                        }
                    }
                },
                aggregate:[
                    {field:"amt", aggregate:"sum"}
                ]
            },
            sortable: true,
            filterable: true,
            scrollable: false,
            mobile: true,
            columns: [{
                field: "date",
                title: giveModel.local.giving.date,
                format: "{0:M/d/yy}",
                width: 100
            }, {
                field: "fund",
                title: giveModel.local.giving.fund
            }, {
                field: "name",
                title: giveModel.local.giving.name
            }, {
                field: "type",
                title: giveModel.local.giving.type,
                filterable: false
            }, {
                field: "amt",
                title: giveModel.local.giving.amount,
                attributes:{
                        style:"text-align:right;"
                    },
                format: "{0:c}",
                footerTemplate: giveModel.local.giving.total+": #= kendo.toString(sum,'c') #"
            }]
        });
0
David
Top achievements
Rank 2
answered on 12 Jan 2015, 02:21 PM
I just realized after tinkering more with that jsbin above that if you change the format to my format ("M/d/yy") then it breaks in the jsbin as well.

Is this a bug or is this format not supported?
0
Kiril Nikolov
Telerik team
answered on 13 Jan 2015, 01:46 PM

Hello David,

Can you please open a separate support request, with some sample data from your service response, where we can see the format of the date returned to the Grid? A working example will also help.

Thanks for the cooperation.

Regards,
Kiril Nikolov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
David
Top achievements
Rank 2
answered on 13 Jan 2015, 03:48 PM
Will do. Thanks.
Tags
Grid
Asked by
Madzie
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
David
Top achievements
Rank 2
Kiril Nikolov
Telerik team
Share this question
or