PDA

View Full Version : select all elements of grid and send them in json string



vipuljaveri
22 Nov 2010, 11:51 PM
Hi can i select all the elements displayed in grid and store it in a json string which i want to send to server ...
my code is:

var grid_mapping = new Ext.grid.GridPanel({
id : 'grid_mapping',
store : store_UEST,
width : 450,
height : 700,
columns : [{
header : "User Elements",
width : 150,
sortable : true,
dataIndex : 'UserElements',
mode : 'local',
editor : {
xtype : 'textfield',
allowBlank : true
}
}, {
header : "Standard Taxonomy Elements",
width : 150,
mode : 'local',
sortable : true,
dataIndex : 'StdTaxElem',
editor : {
xtype : 'textfield',
allowBlank : false
}
}

],{)

thanks in advance

Condor
23 Nov 2010, 1:46 AM
1. Which one: All records, all selected record, all modified records?
2. How should the data in this 'JSON string' be formatted?

vipuljaveri
23 Nov 2010, 1:58 AM
i want to select all records...
for eg.
if the grid's structure is:
usrElem
x a
y b
z c

vipuljaveri
23 Nov 2010, 2:01 AM
i want to select all records...
for eg.
if the grid's structure is:
usrElem stdTaxElem
x a
y b
z c

then the json format i want to send on server ll be
{[usrElem:x,stdTaxElem:a.....and so on

Condor
23 Nov 2010, 2:18 AM
Something like:

var data = [];
grid.getStore().each(function(rec){
data.push(rec.data);
});
Ext.Ajax.request({
url: 'myurl',
jsonData: data,
success: function(response){
...
}
});

vipuljaveri
23 Nov 2010, 2:44 AM
hi condor thanks 4 ur reply

can you plz elaborate...
wht have you written inside success fn

Condor
23 Nov 2010, 2:54 AM
This sends the grid data in JSON format to the server.

The success handler is called with the response from your server (unless your server responds with an HTTP error status).

vipuljaveri
23 Nov 2010, 2:57 AM
but i am having 2 columns in my grid...will
data.push(rec.data); work on both the col values? and response b send in json format like {[usrElem:x,stdTaxElem:a.....and so on

Condor
23 Nov 2010, 3:50 AM
rec.data is an object containing all field values as an object keyed by field name.

vipuljaveri
23 Nov 2010, 4:28 AM
it worked...thanks a lot 4 ur posts
i jus luv dis site

vipuljaveri
23 Nov 2010, 11:10 PM
hey i used the same code n it worked bt now it is giving the same problem
var data = [];
grid_mapping.getStore().each(function(rec){
data.push(rec.data);
});
Ext.Ajax.request({
url: 'mappingScreen.action?act=testajax',
method : 'get',
jsonData: data,
success: function(response){
alert('success');
}
});
i cant see the response in firebug...

vipuljaveri
23 Nov 2010, 11:29 PM
hey now i am not getting response in firebug...what should i do

Condor
23 Nov 2010, 11:46 PM
So you do see the request being sent, but you don't see a response (the icon keeps spinning)?

In that case you have to check your server.

vipuljaveri
23 Nov 2010, 11:50 PM
i get success from server and i even can see the post tab in firebug having JSON and source as expected but cant see anything in the response tab in firebug...
the data i get in source is in the form
[{"id":3354,"UserElements":"a","StdTaxElem":"aa"},{"id":3353,"UserElements":"Travelling Expenses","StdTaxElem":...}.......and so on
but i cant see anything in response tab...
I want that response in my java class by request.getParameter("list")...
plz help

Condor
23 Nov 2010, 11:52 PM
This is application/json content and not x-form-encoded.

If you wanted that then you should use:

Ext.Ajax.request({
url: '...',
params: {
list: Ext.encode(data)
},
...
});

vipuljaveri
24 Nov 2010, 12:16 AM
hey actually i dont want the data to be in the url coz it is v.large...
I tried ur post n it worked bt nw m facing da same prob dat response wont come in the response tab...

Condor
24 Nov 2010, 12:23 AM
1. params are only added to the URL for method:'GET'. With the default method:'POST' they are in the postdata.

2. Debug your server. Is your sever actually sending a response?

vipuljaveri
24 Nov 2010, 1:13 AM
i am using the following code

handler : function() {
store_UEST.proxy.setApi('create','mappingScreen.action?act=saveMapping');
store_UEST.save();
var data = [];
grid_mapping.getStore().each(function(rec){
data.push(rec.data);
});
Ext.Ajax.request({
url: 'mappingScreen.action?act=testajax',
jsonData: data,
success: function(response){
alert('success from ajax');
}
});
}
and success from ajax alert msg is also been displayed...
but the response tab in firebug is showing nothing...

Condor
24 Nov 2010, 1:20 AM
So your server isn't returning anything besides HTTP status 200. If that's the way you programmed your server, then it's fine.

vipuljaveri
24 Nov 2010, 1:29 AM
This is my js code for grid panel...i want to take all the values of the grid and send them as a response to the server...
var grid_mapping = new Ext.grid.GridPanel({
id : 'grid_mapping',
store : store_UEST,
width : 450,
height : 700,
columns : [{
header : "User Elements",
width : 150,
sortable : true,
dataIndex : 'UserElements',
mode : 'local',
editor : {
xtype : 'textfield',
allowBlank : true
}
}, {
header : "Standard Taxonomy Elements",
width : 150,
mode : 'local',
sortable : true,
dataIndex : 'StdTaxElem',
editor : {
xtype : 'textfield',
allowBlank : false
}
}

],
viewConfig : {
forcefit : true
},
plugins : [editor],

height : 700,
width : 450,
frame : true,
tbar : [
{
// cls: 'x-btn-text-icon',
icon : 'images/save-icon.png',
text : 'Save All Modifications',
handler : function() {
var data = [];
grid_mapping.getStore().each(function(rec){
data.push(rec.data);
});
Ext.Ajax.request({
url: 'mappingScreen.action?act=testajax',
jsonData: data,
success: function(response){
alert('success from ajax');
}
});

}
}]
});

On my java class my code ll be of the form:
public void testajax(){
request = ServletActionContext.getRequest();
System.out.println("json from ajax list: "+request.getParameter("list"));
}

what should i do...plz help
i e1 got the expected o/p bt donno y m nt getting it nw...wher m i goin wrong?

vipuljaveri
24 Nov 2010, 1:43 AM
This is my js code.I want all the elements of the grid to be sent to my java class...
var grid_mapping = new Ext.grid.GridPanel({
id : 'grid_mapping',
store : store_UEST,
width : 450,
height : 700,
columns : [{
header : "User Elements",
width : 150,
sortable : true,
dataIndex : 'UserElements',
mode : 'local',
editor : {
xtype : 'textfield',
allowBlank : true
}
}, {
header : "Standard Taxonomy Elements",
width : 150,
mode : 'local',
sortable : true,
dataIndex : 'StdTaxElem',
editor : {
xtype : 'textfield',
allowBlank : false
}
}

],
viewConfig : {
forcefit : true
},
plugins : [editor],

height : 700,
width : 450,
frame : true,
tbar : [
var data = [];
grid_mapping.getStore().each(function(rec){
data.push(rec.data);
});
Ext.Ajax.request({
url: 'mappingScreen.action?act=testajax',
jsonData: data,
success: function(response){
alert('success from ajax');
}
});
}
}]
});

I do get the o/p as expected in the POST tab of firebug but dont get anything in the response tab...
How do i send all the elements to the server???
plz help

vipuljaveri
24 Nov 2010, 1:56 AM
This is my js code...I want to select all the elements of grid and send them to the java class...
var grid_mapping = new Ext.grid.GridPanel({
id : 'grid_mapping',
store : store_UEST,
width : 450,
height : 700,
columns : [{
header : "User Elements",
width : 150,
sortable : true,
dataIndex : 'UserElements',
mode : 'local',
editor : {
xtype : 'textfield',
allowBlank : true
}
}, {
header : "Standard Taxonomy Elements",
width : 150,
mode : 'local',
sortable : true,
dataIndex : 'StdTaxElem',
editor : {
xtype : 'textfield',
allowBlank : false
}
}

],
viewConfig : {
forcefit : true
},
plugins : [editor],

height : 700,
width : 450,
frame : true,
tbar : [ {
// cls: 'x-btn-text-icon',
icon : 'images/save-icon.png',
text : 'Save All Modifications',
handler : function() {
var data = [];
grid_mapping.getStore().each(function(rec){
data.push(rec.data);
});
Ext.Ajax.request({
url: 'mappingScreen.action?act=testajax',
method : 'get',
jsonData: data,
success: function(response){
alert('success from ajax');
}
});
}
}]
});
I e1 get success from ajax in the alert box...and the expected o/p in post tab of firebug...bt the response tab is showing nothing...have i done something wrong??

Condor
24 Nov 2010, 2:00 AM
As I said:

It all depends on what your server returns for the "mappingScreen.action?act=testajax" action.

vipuljaveri
24 Nov 2010, 2:05 AM
actually i want to send the data to mappingScreen.action?act=testajax action....
what should i do?

Condor
24 Nov 2010, 2:08 AM
You can't use method:'GET' in combination with jsonData.

What data format is the 'mappingScreen.action?act=testajax' expecting?

vipuljaveri
24 Nov 2010, 2:17 AM
{"list":[{"id":3354,"StdTaxElem":"a, Unsecured","UserElements":"aa"},{"id":3353,"StdTaxElem":"Foreign Currency Loans from Banks, Unsecured","UserElements":"Travelling Expenses"},{"id":3352,"StdTaxElem":"Foreign Currency Loans from Banks, Unsecured","UserElements":"Foreign Exchange Loss"},{"id":3351,"StdTaxElem":"Foreign Currency Loans from Banks, Unsecured","UserElements":"Preliminary Expenses Woff"},{"id":3350,"StdTaxElem":"Foreign Currency Loans from Banks, Unsecured","UserElements":"Printing & Stationery"},{"id":3349,"StdTaxElem":"Foreign Currency Loans from Banks, Unsecured","UserElements":"Registration Fees"},{"id":3347,"StdTaxElem":"Foreign Currency Loans from Banks, Unsecured","UserElements":"Underwritting Fees"},{"id":3346,"StdTaxElem":"Foreign Currency Loans from Banks, Unsecured","UserElements":"Profit & Loss Ac"}.......

this is the expected format for mappingScreen.action?act=testajax

Condor
24 Nov 2010, 2:27 AM
So it should be:

Ext.Ajax.request({
url: 'mappingScreen.action?act=testajax',
method: 'POST',
jsonData: {list: data},
success: function(response){
alert('success from ajax');
}
});
or is your server expecting:

Ext.Ajax.request({
url: 'mappingScreen.action?act=testajax',
method: 'POST', // or GET
params: {list: Ext.encode(data)},
success: function(response){
alert('success from ajax');
}
});

vipuljaveri
24 Nov 2010, 2:34 AM
Ext.Ajax.request({
url: 'mappingScreen.action?act=testajax',
method: 'POST',
jsonData: {list: data},
success: function(response){
alert('success from ajax');
}
});
works fine but i cant get the list on server...
how can i now get all the elements which u specified in the list ?
when i use request.getParameter("list") it shows null

Condor
24 Nov 2010, 2:37 AM
getParameter doesn't work with jsonData! Use my second example instead.

vipuljaveri
24 Nov 2010, 2:41 AM
actually my json string is very large that i cant use the get method...how can i get the parameter values of list then?
thanks 4 ur replies

Condor
24 Nov 2010, 2:45 AM
Again, use the second example with method:'POST' (which is the default anyway).

vipuljaveri
24 Nov 2010, 2:56 AM
hey man you r great...it worked...thanks a ton for your replies...