PDA

View Full Version : Cleanest way of sending all data in the grid at once to the server



hadi78
13 Oct 2009, 12:36 AM
hi

i have a grid inside a form and i want all the data of the grid to be submitted when
the submit button of the form is pressed. What is the cleanest way of achieving this in ext js 3? As a beginner of ext js i would appreciate code examples because i don't have a good overview over the ext js api yet.

thx in advance

SchattenMann
13 Oct 2009, 1:14 AM
{
text:'Confirmar'
,iconCls:'icon-plus'
,scope:this
,handler:function (){

var json = '';
var store = this.store;

var split = false;

store.each(function(store){

json += Ext.util.JSON.encode(store.data) + ',';
});


if (json != '' && doc != ''){

Ext.MessageBox.confirm('Confirme', 'Tem a certeza que deseja continuar?', function(btn){

if (btn == 'yes'){

Ext.Ajax.request({
url : 'php/contas_correntes.php',
method: 'POST',
params :{
cmd:'savePost',
doc:doc,
cliente:cliente,
post:json
},
success: function ( result, request ) {

store.load();

},
failure: function ( result, request ) {

}
});

}

});

}else if (json == '') {

Ext.Msg.alert('Atenção!', 'Tem de seleccionar pelo menos um documento.');

}else if (doc == '') {

Ext.Msg.alert('Atenção!', 'Tem de seleccionar o documento a gerar.');

}
}
}


simple example

hadi78
13 Oct 2009, 3:08 AM
thx for the example. i am not sure, but aren't you sending the data in a different post request than the form post request? i would like to send the store data of the grid within the form post request.

SchattenMann
13 Oct 2009, 3:16 AM
yes in my example i am...but the basic are there:

encode your grid in JSON to send it...in my example i send separate because that's what i needed...

but maybe you can do the following: before submit your form, encode the grid and set a hidden field with the result so this field is submitted with your grid values...

functional i think

;)

hadi78
13 Oct 2009, 3:54 AM
thx, i ll try my luck.

the intersting thing is that if i have a grid inside my form and sumbit the form, the first row of the grid store is send within the post request of the form automatically. Mabye there is a config option for the grid to send all the data and not only the first row?

SchattenMann
13 Oct 2009, 5:33 AM
that i don't know...

hadi78
13 Oct 2009, 5:53 AM
thx for your help and example code anyway. i finally did it your way and it seems to work.

mschwartz
13 Oct 2009, 5:59 AM
var encoded = Ext.encode(store.getRange(0, store.getCount));
// send to server, stick in hidden field value to use form submit
Ext.getCmp('myHiddenField').setValue(encoded);

Server:
$encoded = $_REQUEST['myHiddenField'];
$records = json_decode($encoded, true);
foreach $records as $record) {
// do something with $record['data']
}

hadi78
13 Oct 2009, 6:16 AM
var encoded = Ext.encode(store.getRange(0, store.getCount));
// send to server, stick in hidden field value to use form submit
Ext.getCmp('myHiddenField').setValue(encoded);

Server:
$encoded = $_REQUEST['myHiddenField'];
$records = json_decode($encoded, true);
foreach $records as $record) {
// do something with $record['data']
}



very nice

hadi78
13 Oct 2009, 7:49 AM
var encoded = Ext.encode(store.getRange(0, store.getCount));
// send to server, stick in hidden field value to use form submit
Ext.getCmp('myHiddenField').setValue(encoded);

Server:
$encoded = $_REQUEST['myHiddenField'];
$records = json_decode($encoded, true);
foreach $records as $record) {
// do something with $record['data']
}




Ext.encode always returns me an empty array :(( what could be the most logical cause for that without posting tons of code now? :D

hendricd
13 Oct 2009, 8:07 AM
Perhaps just a little tweak:


var recs = [];
Ext.each(store.getRange(), function(rec){
recs.push(rec.data);
});
// send to server, stick in hidden field value to use form submit
Ext.getCmp('myHiddenField').setValue(Ext.encode(recs));

Server:
$encoded = $_REQUEST['myHiddenField'];
$records = json_decode($encoded, true);
foreach $records as $data) {
// do something with $data['name']
}

mschwartz
13 Oct 2009, 8:07 AM
It would return an empty array if your store is empty.

You can recode the client side bit like this:


var count = store.getCount();
var records = store.getRange(0, count);
var encoded = Ext.encode(records);


Set a breakpoint in firebug a the var count= line, step over, and see what the count is.

Etc.

mschwartz
13 Oct 2009, 8:08 AM
Perhaps just a little tweak:


var recs = [];
Ext.each(store.getRange(), function(rec){
recs.push(rec.data);
});;
// send to server, stick in hidden field value to use form submit
Ext.getCmp('myHiddenField').setValue(Ext.encode(recs));

Server:
$encoded = $_REQUEST['myHiddenField'];
$records = json_decode($encoded, true);
foreach $records as $data) {
// do something with $data['name']
}



Sure.

It'd be nice if store would do the foreach part for you in some new yet undefined member function.

hendricd
13 Oct 2009, 8:12 AM
Sure.

It'd be nice if store would do the foreach part for you in some new yet undefined member function.

Actually it does:



var recs = [];
store.each(function(rec){
recs.push(rec.data);
});
// send to server, stick in hidden field value to use form submit
Ext.getCmp('myHiddenField').setValue(Ext.encode(recs));

Server:
$encoded = $_REQUEST['myHiddenField'];
$records = json_decode($encoded, true);
foreach $records as $data) {
// do something with $data['name']
}

mschwartz
13 Oct 2009, 8:20 AM
Actually it does:



var recs = [];
store.each(function(rec){
recs.push(rec.data);
});
// send to server, stick in hidden field value to use form submit
Ext.getCmp('myHiddenField').setValue(Ext.encode(recs));

Server:
$encoded = $_REQUEST['myHiddenField'];
$records = json_decode($encoded, true);
foreach $records as $data) {
// do something with $data['name']
}


I meant something like store.getRecordData(range)

hendricd
13 Oct 2009, 8:31 AM
I meant something like store.getRecordData(range)

Now you're just being greedy. ;)

mschwartz
13 Oct 2009, 10:48 AM
Now you're just being greedy. ;)

It's something you do want to do often enough...

And why do the loop/each() twice? (Once in store.getRange and once in your code)

hadi78
14 Oct 2009, 12:37 AM
great thx again ;)