简体   繁体   中英

ExtJS how to build linkage comboboxes in Grid panel

I'm new to ExtJS and I'm working on a address book in which admins can edit the users' address by pick the listed states and cities from two combobox.

I need to build some linkage comboboxes in grid panel so that after admin pick one state in the first drop-down, the relatives cities will be listed in the second drop-down automatically.

If it's only a simple panel I can update the cityStore after state been selected with following code:

        {                               
            xtype:"combo",                                                                                   
            name:'state',  
            id:'state',  
            displayField:'name',  
            valueField:'id',  
            store:storeState,  
            triggerAction:'all',  
            queryMode:'local',  
            selecOnFocus:true,  
            forceSelection:true,  
            allowBlank:false,  
            editable:true,
            //using select listener for updating city store  
            listeners:{  
                select:function(combo,record,index){  
                    try{  
                        var city = Ext.getCmp('city');  
                        city.clearValue();  
                        city.store.load(  
                             {  
                                 params:{  
                                     paramId:combo.getValue()  
                                 }  
                             }     
                        );  
                    }catch(ex){  
                        alert("Failed to load data");  
                    }  

                }  
                }  

        },

However in GridPanel if I update the cityStore with same way, the whole column will changed. Is there anyway to only address the column in the same row in Grid panel? Thanks!

You need to use validateedit & beforeedit events of grid to update the city store.

listeners: {
        validateedit: {  // to check if state value is changed & clearing city value
                fn: function(event,editor){ 
                    switch (editor.field) {
            case 'state':
                 if(editor.value!=editor.record.getData().state)
                     editor.record.set('city',null);
                break;
             }
        return true;
        }
        },
        beforeedit: { // to update the city store based the state value of corresponding row
                fn: function(event,editor){                   

        switch (editor.field) {
            case 'city':
                  var cityStore = Ext.data.StoreManager.lookup('cityStore');
                 cityStore.load({  
                                  params:{  
                                         paramId:editor.value 
                                         }  
                                   });  
                 break;
                }
         return true;
         }
        }
       }

Here is a working example where i am using two local stores state & city. Filtering the city store whenever it is edited with the value of state given in same row.There are three states A,B,C with 1-5,6-10 & 11-15 cities respectively.

You just need to reload Cities store with param from State every time user clicks on a City editor. Do it in beforequery event in combobox.

{
    //this is your Cities combo
    xtype: 'combo',
    valueField: 'foo',
    displayField: 'bar',
    queryMode: 'remote',
    queryCaching: false, //don't forget to disable query caching        
    bind: {
        store: '{cities}',   
    },
    listeners: {
        beforequery: function(queryPlan) {
            //get param from your States store
            queryPlan.combo.getStore();
            //then load this store
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM