PDA

View Full Version : How can I access a date typed field from an ExtJS JSonStore?



Jean-David Lanz
3 Dec 2012, 10:37 AM
Hello all,

I've been trying to retrieve a date value and an integer value from the database, using the following code:


var l_alsChampsMois, l_stoDonneesMois;

try {
l_alsChampsMois = [
{name: "date_mois", type: "date", dateFormat: "Y-m-d"},
{name: "indice", type: "integer"}
];

l_stoDonneesMois = new Ext.data.JsonStore({
fields: l_alsChampsMois,
autoLoad: false,
proxy: {
type: "ajax",
url: "/commun/req_sql/req_ind_per_mois.php",
reader: {
type: "json",
root: "rows"
},
// some configs to use jsFiddle echo service (you will remove them)
actionMethods: {
read: "POST"
},
extraParams: {
key:"test"
}
},
listeners: {
load: function(objStore, alsLignes, blnOk, objOptions) {
window.alert("Mois fin : " + objStore.getAt(0).get("date_mois"));
}
}
});

l_stoDonneesMois.load({params: {
p_idsoc: l_strIdSociete,
p_mois: l_datDebut.getMonth() + 1,
// getMonth renvoie 0 pour janvier, etc.
p_annee: l_datDebut.getFullYear(),
p_debut: 1,
p_etape: 1
}
});

with l_strIdSociete and l_datDebut being variables previously assigned and /commun/req_sql/req_ind_per_mois.php the PHP page that retrieves the data and converts it to JSON.

It seems to work fine (indeed, Firebug tells me the load does retrieve a data structure with "date_mois" and "indice" containing the values I expect them to), only the window.alert returns undefined. If I replace "date_mois" with "indice", it returns the expected value for "indice".

I've tried to use objStore.getAt(0).getData()["date_mois"], to no avail.

My only clue about this is that "date_mois" in the data structure shown by Firebug is an Object, but even so it shouldn't be undefined, now should it? I looked up http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Field-cfg-type that wasn't exactly forthcoming with straight answers.

So what did I do wrong there?

ettavolt
4 Dec 2012, 4:19 AM
If it is an Object, then toString representation would be [object Object], which can be parsed in correct date with any format. Server should convert dates to proper strings.

ascii
7 Dec 2012, 3:00 PM
Perhaps the JSON is not correctly formatted. You could post it here for us to see. Is there a "rows" object that contains the other fields? I ask because the other variable names are in French and "rows" is not - perhaps it was copied from an example.

123lal
7 Dec 2012, 11:42 PM
by default the mysql gives date format in format

after retrieving date from mysql you need to change the date format to

the corresponding format that you were used in the date field

if you are not customised the

xtype : 'datefield',
the

nb the format will be 12/04/2012 that is month/date/year


so i think you need to change the retrieved data from the database server to the above format in the json

then it will load to the xtype : 'datefield',


if you found problem again please post your json and xtype used for date field