PDA

View Full Version : How to format date in JSON



cougar83
21 Oct 2010, 3:37 AM
Hi Forum,

I have an edit grid with some text fields and a Ext.form.DateField and want to put JSON data in there. How do I have to format the data right? The text fields are displayed properly.

This is the edit field:


var date_edit = new Ext.form.DateField({
format: 'd.m.Y'
});


This is my column field in the grid:


{
header: "Date",
dataIndex: 'date',
editor: date_edit,
renderer: Ext.util.Format.dateRenderer('d.m.Y')
}


JSON data in the format "date":"21.10.2010" will result into: NaN.NaN.0NaN

Thank you in advance!

StuartAshworth
21 Oct 2010, 3:53 AM
Have you specified a dateFormat in the store's reader?



// something like..
{
name: 'date',
type: 'date',
dateFormat: 'd.m.Y'
}

laurentParis
21 Oct 2010, 3:54 AM
dateFormat is for your specific declaration, put if you would modif all your application for reconize this format, I suggest to modify altformats (declare this line after loaded localized file)


Ext.form.DateField.prototype.altFormats += '|d.m.Y';

cougar83
21 Oct 2010, 3:59 AM
I haven't. After this change the field is blank ... no displayed data anymore ...

laurentParis
21 Oct 2010, 4:03 AM
You give us
* json format : good
* render format : good

But we need to complete : format about store ?
can you give use please, thanks

cougar83
21 Oct 2010, 4:13 AM
This is the original code:


Ext.onReady(function(){
var store = new Ext.data.JsonStore({
proxy: new Ext.data.ScriptTagProxy({
url: 'http://localhost:8080/JsonWriter/getdata',
method: 'GET'
}),

//JsonReader configs
root: 'data',
fields: ['name', 'trainer', {
name: 'gruendung',
type: 'date',
dateFormat: 'd.m.Y'
}]
});

store.load();

var name_edit = new Ext.form.TextField();
var trainer_edit = new Ext.form.TextField();
var gruendung_edit = new Ext.form.DateField({
format: 'd.m.Y'
});

var mygrid = new Ext.grid.EditorGridPanel({
renderTo: 'mydiv',
frame: true,
title: 'Fußballvereine',
height: 200,
width: 400,
store: store,
clicksToEdit: 1,
columns: [{
header: "Name",
dataIndex: 'name',
editor: name_edit,
sortable: true
}, {
header: "Trainer",
dataIndex: 'trainer',
editor: trainer_edit
}, {
header: "Gründung",
dataIndex: 'gruendung',
editor: gruendung_edit,
renderer: Ext.util.Format.dateRenderer('d.m.Y')
}]
});
});

Foster
21 Oct 2010, 4:13 AM
Can you try removing the formatting on



var gruendung edit = ...


and just leaving in the code for the jsonStore? I think you dont need to define it on both, the jsonStore format should handle it

laurentParis
21 Oct 2010, 4:32 AM
shema must be :

STORE (convert json format => date format) => RENDERER (convert date format => format that you need to display)

so


{
name: 'gruendung',
type: 'date',
dateFormat: 'd.m.Y'
}
for me it's correct

can you check if into store of your grid, this data field are save into date format ?


}

cougar83
21 Oct 2010, 4:34 AM
SOLUTION!!!

I saved the number of the month February with one digit only ... when I save it like "02" everything is fine ... oh man, thank you for your support ...