博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

extjs的combobox的用法

Posted on 2013-08-05 21:55  奥客  阅读(8894)  评论(0编辑  收藏  举报

可以用javascript的数组作为数据源,也可以用json作为数据源:

1.用javascript数组

 var CountryCode = [
['93','Afghanistan(93)'],
['355','Albania (355)'],
['213','Algeria (213)'],
['684','American Samoa (684)'],
['376','Andorra (376)'],
['244','Angola (244)'],
.....
]

new Ext.form.ComboBox(...{
fieldLabel: 'Country Code',
name:'country_code',
forceSelection: true,
listWidth: 200,
store: new Ext.data.SimpleStore(...{
fields: ['value', 'text'],
data : CountryCode
}),
valueField:'value',
displayField:'text',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
selectOnFocus:true,//用户不能自己输入,只能选择列表中有的记录
allowBlank:false
})

2:用json作为数据源

var comboOptions = new Ext.data.JsonStore(...{
url:'myurl',
fields: ['id','name']});

comboOptions.load();

new Ext.form.ComboBox(...{
fieldLabel: 'Management Level',
name:'group_id',
forceSelection: true,
listWidth: 150,
store: comboOptions,
valueField:'id',
displayField:'name',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
selectOnFocus:true,
allowBlank:false
})

myurl输出的json数据格式如下:

[{"id":"1","name":"Super Admin"},{"id":"2","name":"Admin"}]

需要注意的是,如果返回的json数据有多列,需要在new JsonStore的时候,在fields一项中填写所有column的名字,否则不能填充combobox

 

 

 

 

  1. 第一种方式(这种方式,我在使用时,如果数据不是从后台传过来的就可以,如果是从后台传过来的就不行了,要使用第二种方式)、 
  2. var CountryCode = [ 
  3.     ['93','Afghanistan(93)'], 
  4.     ['355','Albania (355)'], 
  5.     ['213','Algeria (213)'], 
  6.     ['684','American Samoa (684)'], 
  7.     ['376','Andorra (376)'], 
  8.     ['244','Angola (244)'
  9. var checkupperson = new Ext.form.ComboBox({ 
  10.             fieldLabel: '审核人'
  11.             name:'checkupperson'
  12.             forceSelection: true
  13.             listWidth: 200, 
  14.             store: new Ext.data.SimpleStore({ 
  15.                 fields: ['value', 'text'], 
  16.                 data : CountryCode 
  17.                 }), 
  18.             valueField:'value'
  19.             displayField:'text'
  20.             typeAhead: true
  21.             mode: 'local'
  22.             triggerAction: 'all'
  23.             selectOnFocus:true,//用户不能自己输入,只能选择列表中有的记录  
  24.             allowBlank:false
  25.             listeners:{ 
  26.              select:function(){ 
  27.               alert(this.value); 
  28.              } 
  29.             } 
  30.         }); 
  31.   checkupperson.on('beforerender',function(){ 
  32.         this.value=376; 
  33.   }); 
  34. 第二种方式: 
  35.   var depCombo = new Ext.form.ComboBox({ 
  36.      fieldLabel:'部门'
  37.               allowBlank: false
  38.      allowNegative: false
  39.      triggerAction: 'all'
  40.      displayField :'depName'
  41.      valueField :'depId'
  42.      id:'test'
  43.              store: new Ext.data.JsonStore({ 
  44.       fields: ['depId','depName'], 
  45.       url: '../combobox.do?action=getDepartmentComboBox'
  46.       autoLoad : true
  47.       listeners :{ 
  48.         load:function(){ 
  49.         Ext.getCmp('test').setValue(17); 
  50.         } 
  51.        } //在此加一个这个玩意,就可以了,呵呵,是在jsonstore中加的,注意  
  52.       }), 
  53.        
  54.              editable :false 
  55.         }); 
第一种方式(这种方式,我在使用时,如果数据不是从后台传过来的就可以,如果是从后台传过来的就不行了,要使用第二种方式)、
var CountryCode = [
    ['93','Afghanistan(93)'],
    ['355','Albania (355)'],
    ['213','Algeria (213)'],
    ['684','American Samoa (684)'],
    ['376','Andorra (376)'],
    ['244','Angola (244)']
 ]
var checkupperson = new Ext.form.ComboBox({
            fieldLabel: '审核人',
            name:'checkupperson',
            forceSelection: true,
            listWidth: 200,
            store: new Ext.data.SimpleStore({
                fields: ['value', 'text'],
                data : CountryCode
                }),
            valueField:'value',
            displayField:'text',
            typeAhead: true,
            mode: 'local',
            triggerAction: 'all',
            selectOnFocus:true,//用户不能自己输入,只能选择列表中有的记录
            allowBlank:false,
            listeners:{
             select:function(){
              alert(this.value);
             }
            }
        });
  checkupperson.on('beforerender',function(){
        this.value=376;
  });
第二种方式:
  var depCombo = new Ext.form.ComboBox({
     fieldLabel:'部门',
              allowBlank: false,
     allowNegative: false,
     triggerAction: 'all',
     displayField :'depName',
     valueField :'depId',
     id:'test',
             store: new Ext.data.JsonStore({
      fields: ['depId','depName'],
      url: '../combobox.do?action=getDepartmentComboBox',
      autoLoad : true,
      listeners :{
        load:function(){
        Ext.getCmp('test').setValue(17);
        }
       } //在此加一个这个玩意,就可以了,呵呵,是在jsonstore中加的,注意
      }),
      
             editable :false
        });
  1.   
 
  1.   
 
  1. 第三种方式: 
第三种方式:
  1. bachLoad([xnStore,xqStore,jsStore], function(success){ 
  2.              if (!success) { 
  3.                  Ext.Msg.alert('提示', '数据加载失败,请联系系统管理员!'); 
  4.              }else
  5.               xn = '${DQXN}'
  6.               xq = '${DQXQ}'
  7.               Ext.getCmp('cmb_xq').setValue('${DQXQ}'); 
  8.               xqmc = Ext.getCmp('cmb_xq').getRawValue(); 
  9.              }