Results 1 to 2 of 2

Thread: Search Form Restful submission

  1. #1
    Sencha User
    Join Date
    Nov 2012
    Posts
    1
    Vote Rating
    0
      0  

    Default 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:
    Code:
            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

  2. 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();

  3. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,553
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    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 @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •