开发者

Dynamically changing the DataStore of a ComboBox

I have a combo box开发者_运维知识库 which populates its values based on the selection of another combobox. I have seen examples where the params in the underlying store are changed based on the selection, but what I want to achieve is to change the store itself of the second combo based on the selection on the first combo. This is my code, but it doesn't work. Can someone please help?

{
                            xtype: 'combo',
                            id: 'leads_filter_by',
                            width: 100,
                            mode: 'local',
                            store: ['Status','Source'],
                            //typeAhead: true,
                            triggerAction: 'all',
                            selectOnFocus:true,
                            typeAhead: false,
                            editable: false,
                            value:'Status',
                            listeners:{
                                'select': function(combo,value,index){ 
                                    var filter_to_select = Ext.getCmp('cmbLeadsFilter');
                                    var container = filter_to_select.container;
                                    if (index == 0){
                                          filter_to_select.store=leadStatusStore;
                                        filter_to_select.displayField='leadStatusName';
                                        filter_to_select.valueField='leadStatusId';
                                      } else if(index==1) {
                                          filter_to_select.store=leadSourceStore;
                                        filter_to_select.displayField='leadSourceName';
                                        filter_to_select.valueField='leadSourceId';
                                      }  
                                    }
                               }
     },      
{
                            xtype: 'combo',
                            id: 'cmbLeadsFilter',
                            width:100,
                            store: leadStatusStore,
                            displayField: 'leadStatusName',
                            valueField: 'leadStatusId',
                            mode: 'local',
                            triggerAction: 'all',
                            selectOnFocus:true,
                            typeAhead: false,
                            editable: false
                        },     


That is not how its designed to work!! When you set a store in the config, you are binding a store to the combo. You don't change the store, instead you are supposed to change the data when required.

The right way of doing it would be to load the store with correct data from the server. To fetch data, you can pass params that will help the server side code get the set of options you need to load.


You will not want to change the store being used... Simply put, the store is bound to the control as it is instantiated. You can, however, change the URL, and params/baseParams used in any additional POST requests.

Using these params, you can code your service to return different sets of data in your combo box's store.


For the proposed problem you can try below solution :

Use below "listener" snippet for the first "leads_filter_by" combo. It will handle the dynamic store binding / changing for the second combobox.

listeners:{
           'select': function(combo,value,index){ 
                     var filter_to_select = Ext.getCmp('cmbLeadsFilter');
                     var container = filter_to_select.container;
                     if (index == 0){
                        //filter_to_select.store=leadStatusStore;
                        filter_to_select.bindStore(leadStatusStore);
                        filter_to_select.displayField='leadStatusName';
                        filter_to_select.valueField='leadStatusId';
                     } else if(index==1) {
                       //filter_to_select.store=leadSourceStore;
                        filter_to_select.bindStore(leadSourceStore);
                        filter_to_select.displayField='leadSourceName';
                        filter_to_select.valueField='leadSourceId';
                      }  
                 }
         }

Hope this solution will help you.

Thanks & Regards.


I had a similar problem. The second combobox would load the store and display the values, but when I would select a value, it would not actually select. I would click the list item and the combobox value would remain blank.

My research also suggested that it was not recommended to change the store and field mappings on a combobox after initialization so here was my solution:

  1. Create a container in the view that would hold the combobox to give me a reference point to add it back later
  2. Grab a copy of the initial config off of the combobox ( this lets me set my config declaritively in the view and not hard code it into my replace function ... in case I want to add other config properties later)
  3. Apply new store, valueField and displayField to that config
  4. Destroy old combobox
  5. Create new combobox with modified config
  6. Using my reference from step 1, add the new combobox

view:

 items: [{
    xtype: 'combobox',
    name: 'type',
    allowBlank: false,
    listeners: [{change: 'onTypeCombo'}],
    reference: 'typeCombo'
}, { // see controller onTypeCombo for reason this container is necessary.
    xtype: 'container',
    reference: 'valueComboContainer',
    items: [{
        xtype: 'combobox',
        name: 'value',
        allowBlank: false,
        forceSelection: true,
        reference: 'valueCombo'
    }]
}, {
    xtype: 'button',
    text: 'X',
    tooltip: 'Remove this filter',
    handler: 'onDeleteButton'
}]

controller:

replaceValueComboBox: function() {
    var me = this;

    var typeComboSelection = me.lookupReference('typeCombo').selection;
    var valueStore = Ext.getStore(typeComboSelection.get('valueStore'));
    var config = me.lookupReference('valueCombo').getInitialConfig();

    /* These are things that get added along the way that we may also want to purge, but no problems now:
    delete config.$initParent;
    delete config.childEls;
    delete config.publishes;
    delete config.triggers;
    delete config.twoWayBindable;
   */
    config.store = valueStore;
    config.valueField = typeComboSelection.get('valueField');
    config.displayField = typeComboSelection.get('displayField');
    me.lookupReference('valueCombo').destroy();
    var vc = Ext.create('Ext.form.field.ComboBox', config);

    me.lookupReference('valueComboContainer').add(vc);
},
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜