-
4 Feb 2013 7:04 AM #1
Answered: Search Form Restful submission
Answered: Search Form Restful submission
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:
Many Thanks in advanceCode: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); } }); } } }] });
Nick
-
Best Answer Posted by mitchellsimoens
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:
Code:var values = form.getValues(); store.proxy.extraParams = Ext.apply(store.proxy.extraParams || {}, { q : values.q, type : values.type }); store.load();
-
6 Feb 2013 3:00 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,582
- Vote Rating
- 433
- Answers
- 3102
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:
Code:var values = form.getValues(); store.proxy.extraParams = Ext.apply(store.proxy.extraParams || {}, { q : values.q, type : values.type }); store.load();Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.


Reply With Quote