PDA

View Full Version : showing date in a grid from JSON Store



loadbrain
27 Jul 2009, 12:09 AM
Hello,
I have the following getting back from a database in JSON Format:


{"total":"117","results":[{"subscriber_id":"29","user_id":"0","subscriber_name":"Y. XXXXX","subscriber_email":"[email protected]","confirmed":"1","subscribe_date":"2008-06-10 00:48:26"}....
Everything works fine, all data is displayed in my EditorGrid but the date column stays empty...
Do I have to define something special to render this?
I tried different date, string options, nothing is showed...

Do not know any further...

mystix
27 Jul 2009, 12:16 AM
What's your Record definition?

steffenk
27 Jul 2009, 12:19 AM
the format you use isn't a valid date format for conversion.

If you prepare data in php or other language try to use date('r') which is RFC 2822 formatted date

mystix
27 Jul 2009, 12:23 AM
the format you use isn't a valid date format for conversion.

If you prepare data in php or other language try to use date('r') which is RFC 2822 formatted date

that is incorrect.

the date string from the server is parsed according to the Record definition's dateFormat config for that particular field (i.e. the "subscribe_date" field in this case, assuming it was defined as type: 'date'). in other words, as long as the server sends a date string which can be parsed by one of the Ext Date class's format specifiers, it shouldn't matter what format that string arrives in.

which is why i asked to see the OP's Record definition.

loadbrain
27 Jul 2009, 12:24 AM
My Record:


var subscriberRecord = Ext.data.Record.create([
{ name : "subscriber_id", mapping : "subscriber_id" },
{ name : "user_id", mapping : "user_id" },
{ name : "subscriber_name", mapping : "subscriber_name" },
{ name : "subscriber_email", mapping : "subscriber_email" },
{ name : "confirmed", mapping : "confirmed" },
{ name : "Datum", mapping : "subscribe_date" }
]);

mystix
27 Jul 2009, 12:28 AM
My Record:


var subscriberRecord = Ext.data.Record.create([
{ name : "subscriber_id", mapping : "subscriber_id" },
{ name : "user_id", mapping : "user_id" },
{ name : "subscriber_name", mapping : "subscriber_name" },
{ name : "subscriber_email", mapping : "subscriber_email" },
{ name : "confirmed", mapping : "confirmed" },
{ name : "Datum", mapping : "subscribe_date" }
]);

you're missing the type config from the Field definition for "subscribe_date", which means the Record will simply treat that field as a plain old string.

use this Field definition instead:


{ name : "Datum", mapping : "subscribe_date", type: 'date', dateFormat: 'c' }

loadbrain
27 Jul 2009, 12:39 AM
Sorry, still not showing...

steffenk
27 Jul 2009, 12:50 AM
mystix - your right.
c is the wrong format (its like 2004-02-12T15:19:21+00:00)
try dateFormat: 'Y-m-d H:i:s'

mystix
27 Jul 2009, 12:54 AM
Sorry, still not showing...

what's your ColumnModel definition?

loadbrain
27 Jul 2009, 12:55 AM
nop, not working...

loadbrain
27 Jul 2009, 12:56 AM
{header: 'Datum', width: 250, dataIndex: 'subscribe_date'}

mystix
27 Jul 2009, 12:58 AM
c is the wrong format (its like 2004-02-12T15:19:21+00:00)
try dateFormat: 'Y-m-d H:i:s'

that is once again incorrect.

as per the docs, the c format specifier recognises all of the following formats:


1991
1992-10 or
1993-09-20 or
1994-08-19T16:20+01:00 or
1995-07-18T17:21:28-02:00 or
1996-06-17T18:22:29.98765+03:00 or
1997-05-16T19:23:30,12345-0400 or
1998-04-15T20:24:31.2468Z or
1999-03-14T20:24:32Z or
2000-02-13T21:25:33
2001-01-12 22:26:34



you can try this out yourself in the Firebug console:


Date.parseDate('2008-06-10 00:48:26', 'c')

mystix
27 Jul 2009, 1:02 AM
{header: 'Datum', width: 250, dataIndex: 'subscribe_date'}



{header: 'Datum', width: 250, dataIndex: 'subscribe_date', renderer: Ext.util.Format.dateRenderer('c')}


but the grid should still show something in that column without the above renderer config.

are you sure you're not doing anything funky in your code?

i think it's best you post your full test case at this point in time to avoid any second-guessing (you should've done this from that start).

loadbrain
27 Jul 2009, 1:17 AM
It is working now, thanks a lot to all!!! You are my heros!

This code did it:

{ name : "subscribe_date", mapping : "subscribe_date", type: 'date', dateFormat: 'Y-m-d H:i:s' }

mystix
27 Jul 2009, 1:19 AM
It is working now, thanks a lot to all!!! You are my heros!

This code did it:

{ name : "subscribe_date", mapping : "subscribe_date", type: 'date', dateFormat: 'Y-m-d H:i:s' }

are you sure you're on Ext 3.0 (or 2.3.0)?

this should have worked in the first place with a server-returned date string of "2008-06-10 00:48:26":


{ name : "Datum", mapping : "subscribe_date", type: 'date', dateFormat: 'c' }

loadbrain
27 Jul 2009, 1:21 AM
I think the latter problem was, that I confused some

{ name : "subscribe_date", ...
on one side I had "Datum" on the other "subscribe_date", so also

{ name : "Datum", mapping : "subscribe_date", type: 'date', dateFormat: 'c' }
is now working.

mystix
27 Jul 2009, 1:23 AM
oh wait i see the original problem now.

your ColumnModel's dataIndex config for the "Datum" column must match the Record's Field definition's name config:

Record config:


{ name : 'subscribe_date' /* Datum */, mapping : 'subscribe_date', type: 'date', dateFormat: 'c' }

or, since the mapping = name, just omit the mapping altogether:


{ name : 'subscribe_date', type: 'date', dateFormat: 'c' }



and here's the correct ColumnModel config:


{header: 'Datum', width: 250, dataIndex: 'subscribe_date', renderer: Ext.util.Format.dateRenderer('c')}



[edit]
yay. you spotted the problem correctly :)

loadbrain
27 Jul 2009, 1:24 AM
Yeah, really dumb... to much copy & paste... ;-)

But again, thanks for your hints, this solved the problem!

steffenk
27 Jul 2009, 2:19 AM
ah - learned something new about Date.parseDate, the Ext function is much more flexible as the php date function which i always use as reference for format. Thx!