PDA

View Full Version : [RESOLVED] JsonStore success/failure



senacle
5 Aug 2009, 4:36 AM
Hello,

I've a form with severals fields.
When i click on a button of this form, the values of the field are sent to php script.
The php script returns data in a json array.
I take these data and displays a listview.


The code when the button of the form is clicked on :


..........
donnee_stat.load({params: {application: Ext.getCmp('idjs_application').getValue(), formulaire: Ext.getCmp('idjs_formulaire').getValue(), service: Ext.getCmp('idjs_service').getValue(), date_debut: date_debut, date_fin: date_fin, periode: Ext.getCmp('idjs_periode').getValue(), type_requete: Ext.getCmp('idjs_type_requete').getValue(), nb_barre: Ext.getCmp('idjs_nb_barre').getValue()}});

layout.render('option_stat');
..........
donnee_stat is the jsonStore :



var donnee_stat = new Ext.data.JsonStore({
url: '../Php/BDD/Stat_Resultat.php',
fields:[
'abcisse',
'nouveau',
'encours',
'differe',
'resolu',
'clos',
'delai'
]
});
the layout has a listview wich takes the jsonstore as its store :



..............
var tableau=new Ext.ListView({
store: donnee_stat,
columns: [
{
header: ' ',
dataIndex: 'abcisse',
align: 'center',
},{
..............
No problem when data exists.

But when there's no data, i want to display a message.
I don't find how to do this.

Maybe i can use in the jsonstore



..........
failure: function(result, action) {
obj = Ext.decode(action.response.responseText);
var message = obj.errors.reason;
alert(message);
},
..........
Some idea ?

moegal
5 Aug 2009, 4:45 AM
what does your json look like if failure. success:false? Or just empty json?
Post the json if fail.

Marty

senacle
5 Aug 2009, 5:16 AM
json data for success



[{"abcisse":"01/2009","nouveau":33,"encours":0,"differe":0,"resolu":"0<br>(-)","clos":0},{"abcisse":"02
/2009","nouveau":79,"encours":4,"differe":0,"resolu":"2<br>(2 J 1 H 6 M 37 S )","clos":3},{"abcisse"
:"03/2009","nouveau":38,"encours":16,"differe":1,"resolu":"173<br>(159 J 1 H 14 M 59 S )","clos":11}
,{"abcisse":"05/2009","nouveau":2,"encours":0,"differe":0,"resolu":"0<br>(-)","clos":0},{"abcisse":"Cumul"
,"nouveau":152,"encours":20,"differe":1,"resolu":"175<br>(157 J 6 H 11 M 7 S )","clos":14}]

json data for failure



{success: false, errors: { reason: 'erreur' }}

moegal
5 Aug 2009, 7:37 AM
you may want to set up your json differently.

For an empty recordset I return the following:

( {"Records": [], "RecordCount":"0" } )

I would add :

root: 'Records'

or something like it to the store. I personally use a store with a json reader like:



var gridstore = new Ext.data.Store({
url: 'myfile.asp',
method: 'POST',
remoteSort: true,
reader: new Ext.data.JsonReader({
root: 'Records',
totalProperty: 'RecordCount',
id: 'idFieldName',
fields: [
'idFieldName',
'fieldOne',
'fieldTwo',
'fieldThree'
]
}),
listeners:{
load: function(store,records,options){
}
}
});


Finally my json would have the Records parent like:



( {"Records": [{"abcisse":"01/2009","nouveau":33,"encours":0,"differe":0,"resolu":"0<br>(-)","clos":0},{"abcisse":"02
/2009","nouveau":79,"encours":4,"differe":0,"resolu":"2<br>(2 J 1 H 6 M 37 S )","clos":3},{"abcisse"
:"03/2009","nouveau":38,"encours":16,"differe":1,"resolu":"173<br>(159 J 1 H 14 M 59 S )","clos":11}
,{"abcisse":"05/2009","nouveau":2,"encours":0,"differe":0,"resolu":"0<br>(-)","clos":0},{"abcisse":"Cumul"
,"nouveau":152,"encours":20,"differe":1,"resolu":"175<br>(157 J 6 H 11 M 7 S )","clos":14}], "RecordCount":"18" } )



Marty

senacle
5 Aug 2009, 10:38 PM
OK for the json format, but what are you doing when you receive empty data ?

Condor
6 Aug 2009, 12:14 AM
A store doesn't process the successProperty (only forms do), so even your failure response is processed as a successful response.

If you want a request to fail you should set a different HTTP status code in the server response (e.g. 500).

ps. The store load() method doesn't have a 'failure' parameter. You can use the 'callback' parameter or a 'exception' event handler.

senacle
6 Aug 2009, 1:06 AM
I've found how to do what i want.

I submit the form and i test if success or failure.
If failure ==> a message is displayed
If success ==> data are loaded in the store and the listview is displayed.

The JsonStore



var donnee_stat = new Ext.data.JsonStore({
fields:[
'abcisse',
'nouveau',
'encours',
'differe',
'resolu',
'clos',
'delai'
]
});
The code when the button of the form is clicked on :



buttons: [{
text: 'Calculer',
handler: function(){
var formulaire = Ext.getCmp('option_stat').form;
if(formulaire.isValid()){
date_debut = '';
if (Ext.getCmp('idjs_debut').getValue() != '') {
date_debut = Ext.getCmp('idjs_debut').getValue().format(Ext.getCmp('idjs_debut').format);
}
date_fin = '';
if (Ext.getCmp('idjs_fin').getValue() != '') {
date_fin = Ext.getCmp('idjs_fin').getValue().format(Ext.getCmp('idjs_fin').format);
}
Ext.getCmp('option_stat').getForm().submit({ //on soumet le formulaire
url: '../Php/BDD/Stat_Resultat.php', //URL de la page sur laquelle on effectue la méthode post
method: 'POST',
reset: false, //pour ne pas reseter le formulaire en cas d'échec
failure: function(result, action) { //Si la réponse est "success:false"
obj = Ext.decode(action.response.responseText); //L'objet JSON va lire le résultat du POST et le décoder
var message = obj.errors;
Ext.Msg.show({
title: 'Information',
msg: message,
closable: false,
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.INFO
});
},
success: function(result, action) {
obj = Ext.decode(action.response.responseText);
var donnee = obj.data;
donnee_stat.loadData(donnee);
// donnee_stat.load({params: {application: Ext.getCmp('idjs_application').getValue(), formulaire: Ext.getCmp('idjs_formulaire').getValue(), service: Ext.getCmp('idjs_service').getValue(), date_debut: date_debut, date_fin: date_fin, periode: Ext.getCmp('idjs_periode').getValue(), type_requete: Ext.getCmp('idjs_type_requete').getValue(), nb_barre: Ext.getCmp('idjs_nb_barre').getValue()}});
layout.title = 'coucou';
layout.render('option_stat');

}
}); //fin du submit
}
}
},{
text: 'Annuler',
handler: function(){
Ext.getCmp('option_stat').form.reset();
}
}]
The json data looks like :

failure :
{success: false, errors: 'Aucune demande enregistr&eacute;e avec les crit&egrave;res s&eacute;lectionn&eacute;s.'}
success :

{success: true, data: [{"abcisse":"01/2009","nouveau":33,"encours":0,"differe":0,"resolu":"0<br>(-)","clos":0},{"abcisse":"02/2009","nouveau":79,"encours":4,"differe":0,"resolu":"2<br>(2 J 1 H 6 M 37 S )","clos":3},{"abcisse":"03/2009","nouveau":38,"encours":16,"differe":1,"resolu":"173<br>(159 J 1 H 14 M 59 S )","clos":11},{"abcisse":"05/2009","nouveau":2,"encours":0,"differe":0,"resolu":"0<br>(-)","clos":0},{"abcisse":"Cumul","nouveau":152,"encours":20,"differe":1,"resolu":"175<br>(157 J 6 H 11 M 7 S )","clos":14}]}

senacle
6 Aug 2009, 1:34 AM
In the API Doc, i've found the emptyText (http://extjs.com/forum/../deploy/ext-3.0.0/docs/source/DataView.html#cfg-Ext.DataView-emptyText) ListView option.
It can be useful for my needs.
I will try it maybe later.