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

Date types from Remote JSON datasource

5 Answers 679 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 01 Feb 2012, 09:16 PM
Hi, hopefully a quick solution here.

I have a grid showing data from a remote data source, code is below inc JSON sample. All works fine apart from filtering on the date column, which then gives a JS error "no method getTime()". I believe this is because the dates returned in the JSON datasource are actually in string format rather than a date object.

If this is the case, or if there is something else I'm missing can someone please help out?

thanks.


//  models
var ftModel_EventLog = kendo.data.Model.define({
        id: "EventLog_ID",
        fields: {
            EventLog_ID: { type: "number" },
            EventLog_EntryDate: { type: "date" },
            EventLog_Message: { type: "string" }
        }
    });
 
//  dataSource
    var ftDataSource_EventLog = new kendo.data.DataSource({
        transport: {
            read: {
                url: "Services/Grid.aspx",
                data: {
                    src: "eventlog"
                },
                dataType: "json"
            }
        },
        schema: {
            data: function (data) {
                return eval(data.data);
            },
            model: ftModel_EventLog
        }
    });
 
//  start of functions
 
function ftLoadContent_EventLog() {
    $(".grid").kendoGrid({
        dataSource: ftDataSource_EventLog,
        filterable: true,
        height: 380,
        sortable: true,
        columns: [
            { field: "EventLog_Message", title: "Message" },
            { field: "EventLog_EntryDate", title: "Entry Date" }
        ]
    });
}

A sample of the JSON data is:

{"data":[{"EventLog_ID":"1","EventLog_Message":"This is an event.","EventLog_EntryDate":"01/02/2012 17:34:22"},{"EventLog_ID":"2","EventLog_Message":"This is another event.","EventLog_EntryDate":"01/02/2012 17:34:45"}]}


5 Answers, 1 is accepted

Sort by
0
Accepted
Andrew
Top achievements
Rank 1
answered on 03 Feb 2012, 04:12 PM
You are on the right track.   Your JSON is returning everything as strings (even the ID field - note the quotes around the values).   Your date values (also strings) need to be converted to Javascript Date objects once retrieved using parse function in the Schema definition, something like this (note, I use DateJS which makes this work, but however you want to convert the string to a date object is up to you):

 parse : function(data) {
   $.each(data.recs, function(i, val){
     val.datefld = Date.parse(val.datefld);
   });
   return data;
 }

likewise, if you need to put the data back thru the data source (after an edit / create) you need to convert the Java Date object back to whatever form your server side process accepts.   I have a simple PHP script that is accepting dates in the stock MySQL format - something this this (again, I use DateJS so you might have to adjust the actual conversion call a bit to fit your needs):

 parameterMap: function(options, operation) {
                  if ( (typeof options.datefld === "object") && (options.datefld instanceof Date) ) { 
    var d = new Date(options.datefld);
                    options.datefld = d.toString("yyyy-MM-dd");
 }
            return $.extend(options);
}

** Note that I do the type checking because in use I found that on an UPDATE, if a user changes data but not the actual datefld, that field reports back in my format string.  However on an CREATE or an UPDATE where they actually change the datefld, it reports back as a date object.   Thus, I only do the conversion to a string if I need to.   YMMV depending on how you have your grid and date format configured...

0
Richard
Top achievements
Rank 1
answered on 03 Feb 2012, 07:49 PM
Great. thanks Andrew.

Exactly what I was looking for - all working now.
0
Andrew
Top achievements
Rank 1
answered on 04 Feb 2012, 06:11 PM
Sure thing.  Tag it as answered - maybe I'll get a T-Shirt (LOL)...
0
RMC
Top achievements
Rank 1
answered on 09 Feb 2012, 08:00 PM
thanks
0
Andre
Top achievements
Rank 1
answered on 23 Mar 2012, 01:39 PM
Wow this seems like a big oversite? surely the date should revert back and submit as per the format requested in the format specified for the widget, I cant ever see a situation where a javascript date object is what the server side would be expecting. Not to mention the "UI" now reverting to a date object when changed instead of back to the required format?

This just should not happen by default, default should be requested format, and then if that needs changing then we can do some fancy extra work, but now just to get default to work we have to do all this extra work.
Tags
Data Source
Asked by
Richard
Top achievements
Rank 1
Answers by
Andrew
Top achievements
Rank 1
Richard
Top achievements
Rank 1
RMC
Top achievements
Rank 1
Andre
Top achievements
Rank 1
Share this question
or