Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically changing the DataStore of a ComboBox

Tags:

extjs

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
                        },     
like image 595
Joji Avatar asked Mar 22 '11 13:03

Joji


3 Answers

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.

like image 150
Abdel Raoof Olakara Avatar answered Nov 19 '22 23:11

Abdel Raoof Olakara


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.

like image 36
It Grunt Avatar answered Nov 20 '22 00:11

It Grunt


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.

like image 1
kiran Avatar answered Nov 20 '22 00:11

kiran