ExtJs多级联动菜单的一种实现

1.ExtJs中的Combos控件,定义数据源store的时候可以采用下面的方式:
代码
var store = new Ext.data.SimpleStore({                            //定义组合框中显示的数据源
            //fields: ['Item', 'ItemValue'],
            fields: ['Item''ItemValue''ParentID''strValueID'],
            data: arrayItemStore
        });

fields可以设置多个字段,只要跟data属性的赋值一致就可以了。data的赋值需要是一个嵌套数组,其中数组的字元素就是fields定义的数组,如下所示:

data:
[

  [
'公司1','2458','0','48'],     //需要跟fields定义的数组维度一致。
  ['公司2','542','0','478'],
  [
'公司3','45','0','47']

]

2.加入公司和地区,根据parentID来关联,可以添加如下的代码:

代码
var co = Ext.getCmp("co");
var dq = Ext.getCmp("dq");
if (!Ext.isEmpty(co)) {
    co.on(
'beforeselect'function (box, record, index) {
    dq.store.filter(
'ParentID', record.get('strValueID'));
}

这里的record是Extjs传递过来的当前选中项的数据集,可以方便的取到我们在给Combos定义store时的数据项。
dq.store.filter方法用于筛选Combos地区的数据,这里有一点要注意,既然是筛选,地区Combos肯定得包含有所有的地区才可以。
如果需要Combos地区从数据库读取数据,可以采用下面的形式:

代码
//function that gets city store
function getCityStore()
{
    
var store = new Ext.data.JsonStore({
        url: 
'get-city-by-country-id',
        
//baseParams: countryId:countryIdVar,
        fields: [
            {name:
'cityId', type:'int'},
            
'cityName' 
        ]
    });
    
return store;
}

//than I have a countries combo
var countries = new Ext.form.ComboBox({    
    id:
'country',
    store: getCountryStore(),
    typeAhead: 
true,
    triggerAction: 
'all',
    emptyText: 
'...',
    listeners: {
        select: {
            fn:
function(combo, value) {
                
var modelDest = Ext.getCmp('city');
                
//set and disable cities
                modelDest.setDisabled(true);
                modelDest.setValue(
'');
                modelDest.store.removeAll();
                
//reload region store and enable region 
                modelDest.store.reload({
                    params: { countryId: combo.getValue() }
                });
                modelReg.setDisabled(
false);
        }
        }
    }        
})

 第三种方法:

var provinces = [[1,'北京'],[2,'上海']];   
  1. var cities = new Array();    
  2. cities[1] = [[11,'海淀'],[22,'东城']];    
  3. cities[2] = [[33,'黄埔'],[44,'浦东'],[55,'静安']];   
  4.   
  5. var comboProvinces = new Ext.form.ComboBox({   
  6.   
  7.            store: new Ext.data.SimpleStore(  {   
  8.                    fields: ["provinceId""provinceName"],   
  9.                    data: provinces   
  10.           }),   
  11.   
  12.            listeners:{   
  13.                   select:function(combo, record,index){   
  14.                          comboCities.clearValue();   
  15.                          comboCities.store.loadData(cities[record.data.provinceId]);   
  16.                  }   
  17.           },   
  18.   
  19.           valueField :"provinceId",   
  20.           displayField: "provinceName",   
  21.           mode: 'local',   
  22.           forceSelection: true,   
  23.           blankText:'请选择省份',   
  24.           emptyText:'请选择省份',   
  25.           hiddenName:'provinceId',   
  26.           editable: false,   
  27.           triggerAction: 'all',   
  28.           allowBlank:true,   
  29.           fieldLabel: '请选择省份',   
  30.           name: 'provinceId',   
  31.            80    
  32.   
  33. });   
  34.   
  35.  var comboCities = new Ext.form.ComboBox({   
  36.             store: new Ext.data.SimpleStore(  {   
  37.                         fields: ["cityId",'cityName'],   
  38.                        data:[]   
  39.             }),   
  40.   
  41.             valueField :"cityId",   
  42.             displayField: "cityName",   
  43.             mode: 'local',     
  44.             forceSelection: true,   
  45.             blankText:'选择地区',   
  46.             emptyText:'选择地区',   
  47.             hiddenName:'cityId',   
  48.             editable: false,   
  49.             triggerAction: 'all',   
  50.             allowBlank:true,   
  51.             fieldLabel: '选择地区',   
  52.             name: 'cityId',   
  53.              80   
  54. });   



ComboBox控件的id和hiddenName不要设置成一样,否则会选不中选项,不知道是不是ComboBox的一个bug。

原文地址:https://www.cnblogs.com/timy/p/1754203.html