PDA

View Full Version : Search Form Restful submission



Nick Rogers
4 Feb 2013, 7:04 AM
Hi,

I have created a simple search form which is making a request to a RESTFUL api.

I would like the RESTFUL request formatted from the fields in the form but do not know how to achieve this. I have two fields, q= the search term and type = the type of search I am creating. The result from the search will be in JSON format and get stored in a model/store.

I have put the URL creation dynamically in the handler and this appears to be working although it is still adding query parameters of the fields to the end of the URL. It feels like a pretty hacky approach though. Also, I have another form with a lot of fields where this approach won't look so clever!

Does anyone know a more 'elegant' EXTjs 4.1.3 solution for this?

My code looks like this:

var navSearchForm = Ext.create('Ext.form.Panel', {
xtype: 'form',
ui: 'panel-transparent',
url: '/UserSearch/',
actionMethods: {
read: 'GET'
},
appendId: false,
reader: {
type: 'json',
root: 'data',
totalProperty: 'total'
},
flex: 1,
layout: {
type: 'hbox',
align: 'middle',
pack: 'end'
},
items: [{
xtype: 'combo',
hideLabel: true,
displayField: 'name',
valueField: 'value',
typeAhead: true,
queryMode: 'local',
triggerAction: 'all',
name: 'type',
// value: 'phone',
emptyText: Cam.locale.header.nav.search.phoneNumber,
selectOnFocus: true,
width: 150,
store: Ext.create('Ext.data.Store', {
fields : ['name', 'value'],
data : [
{name : 'Phone Number', value: 'phone'},
{name : 'IMSI', value: 'IMSI'}
]
})
},{
xtype: 'textfield',
name: 'q',
padding: 15,
fieldLabel: '',
emptyText: Cam.locale.header.nav.search.searchTerm,
allowBlank: false // requires a non-empty value
}, {
xtype: 'button',
ui: 'btn-blue',
scale: '',
height: 30,
border: 0,
text: Cam.locale.header.nav.search.search,
handler: function() {
var form = this.up('form').getForm();
console.log(form);
existingURL = form.url;
if (form.isValid()) {
form.submit({
method: 'GET',
url: form.url + form._fields.items[0].value + '/' + form._fields.items[1].value,
success: function(form, action) {
Ext.Msg.alert('Success', action.result.msg);
},
failure: function(form, action) {
//Ext.Msg.alert('Failed', action.result.msg);
}
});
}
}
}]
});


Many Thanks in advance

Nick

mitchellsimoens
6 Feb 2013, 3:00 PM
You say you are going to store the search in a store so why not use a proxy to load the search in the store's proxy's extraParams. So your form would do something like:


var values = form.getValues();

store.proxy.extraParams = Ext.apply(store.proxy.extraParams || {}, {
q : values.q,
type : values.type
});

store.load();