PDA

View Full Version : Submit form - post whole record.



maps_tecnologia
13 Jan 2012, 7:20 AM
I have a combobox and a datefield on my form. The combo box is backed up by a store and only show one field of it, like:
A record: {name: bla, id:7. class: ble}
Combobox shows: bla

I want to submit my form POSTing my fields as JSON data, but don't want to post like: {name:bla, date:01/01/1900}.
I want my combobox to submit the ENTIRE record, as such: {{name: bla, id:7. class: ble}date:01/01/1900}

I am using a hiddenField and manually setting him like: hiddenField.setValue(record.get('a')........);

Is there a better way to do this, maybe a "Extjs-way"?

mitchellsimoens
13 Jan 2012, 8:25 AM
You can pass in a params object in the submit method

maps_tecnologia
13 Jan 2012, 1:39 PM
But how can I do something like:

params: form.getField("combobox").GETMODELOBJECT ?

hendricd
13 Jan 2012, 2:53 PM
But how can I do something like:

params: form.getField("combobox").GETMODELOBJECT ?

Try:


Ext.pluck(form.getField("combobox").lastSelection || [], 'data')


This will return a raw Array of selected Record objects (remember Combo support multiSelect).

maps_tecnologia
16 Jan 2012, 6:01 AM
Ext.pluck(form.findField("combobox").lastSelection || [], 'data')It works =DJust so I can get this clearer, what does the
|| [], 'data') do?

mitchellsimoens
16 Jan 2012, 6:07 AM
It tries to get the lastSelection from the combobox but if that is falsey then it will use a blank array '[]'

maps_tecnologia
16 Jan 2012, 6:10 AM
Btw, I tried submiting my record but I got this:

request payload:
param_bla=%5Bobject%20Object%5D&param_date=19-01-2012


var record = Ext.pluck(form.findField("ComboBox").lastSelection || [], 'data');

Ext.Ajax.request({
url:'rest/a/getBla',
method: 'POST',
headers: {'Content-Type':'application/json; charset=utf-8'},
params: {
'param_bla': record,
'param_date': date
}
});

hendricd
16 Jan 2012, 6:38 AM
@maps_tecnologia--

The sample I provided returns an Array of selected records (as ComboBox may be configured to support multiple selections).

If you are only allowing a SINGLE selection, then just use the first record:



var record = Ext.pluck(form.findField("ComboBox").lastSelection || [], 'data')[0];
if(record) {
Ext.Ajax.request({
url :'rest/a/getBla',
method: 'POST',
jsonData: Ext.apply( record, {'param_date': date }) //mix in the date
});
}

maps_tecnologia
16 Jan 2012, 8:17 AM
Whoa, thats really nice, thanks!

Just one more thing (sorry for being so demanding), but my date must be somehow separated from the other data.

I am trying to send a Java object AND a Date field, so it should be like:
{{class:person, name:maps, age: 18}, date: 01-01-1990}

I am trying, but with no success, things like:

jsonData:
record,
date
....

hendricd
16 Jan 2012, 8:20 AM
So, that would be :

jsonData : { record : record, date : yourDate }

maps_tecnologia
16 Jan 2012, 8:28 AM
Thank you guys, you are all awesome. It's working now. My form can actually submit my whole record =D


{"record":{"class":"person","id":17,"nome":"José","idade":17},"date":"10-10-2008"}