现在的位置: 首页 > 综合 > 正文

MultiComboBox

2013年09月08日 ⁄ 综合 ⁄ 共 5654字 ⁄ 字号 评论关闭

Ext2.0下拉多选菜单(MultiComboBox)

由于Ext2.0的ComboBox不支持菜单多选,本人花了点时间通过扩展ComboBox功能支持下拉多选功能的MultiComboBox。下面介绍MultiComboBox的使用方式,大家先看看下面的代码:

Java代码
  1. Ext
    .onReady(function(){  

  2.           var formPanel = new
     Ext
    .FormPanel({  

  3.             height : 100
    ,
    // 表单面板的高度
      
  4.             width : 400
    ,
    // 表单面板的宽度
      
  5.             labelWidth : 120
    ,
    // 字段标签宽度
      
  6.             labelAlign : "right"
    ,
    // 字段标签对齐方式
      
  7.             fileUpload: true
    ,
    //支持文件上传
      
  8.             defaults : {// 默认form元素类型为textfield
      
  9.                 xtype : "textfield"
    ,
    // 默认类型为textfield
      
  10.                 width : 150
     
    // 默认宽度
      
  11.             },  
  12.             items : [{  
  13.                xtype:'multicombo'
    ,  
  14.                width:250
    ,  
  15.                store: new
     Ext
    .data.SimpleStore({  

  16.                    fields: ["name"
    ,
    "value"
    ],  
  17.                    data:[['测试菜单1'
    ,
    1
    ],[
    '测试菜单2'
    ,
    2
    ],[
    '测试菜单3'
    ,
    3
    ],[
    '测试菜单4'
    ,
    4
    ]]}),  
  18.                valueField :"value"
    ,  
  19.                displayField: "name"
    ,  
  20.                labelSeparator:':'
    ,  
  21.                displaySeparator:';'
    ,  
  22.                valueSeparator:','
    ,  
  23.                mode: 'local'
    ,  
  24.                value:'1,2'
    ,  
  25.                forceSelection: true
    ,  
  26.                hiddenName:'test'
    ,  
  27.                editable: true
    ,  
  28.                triggerAction: 'all'
    ,  
  29.                allowBlank:false
    ,  
  30.                emptyText:'请选择'
    ,  
  31.                fieldLabel: '多选下拉ComBo'
      
  32.             }],       
  33.             buttons : [{  
  34.                 text : '提交'
    ,  
  35.                 type : 'submit'
    ,  
  36.                 handler : function() {  
  37.                       
  38.                 }  
  39.             }]  
  40.         });  
  41.         formPanel.render("multicombo-div"
    );  
  42. });  

由上面代码可以看到用法大致和ComboBox一样,不相同的地方是:

Java代码
  1. xtype:
    'multicombo'
    ,
    //MultiComboBox注册类型名称
      
  2. displaySeparator:';'
    ,
    //多选显示分隔字符
      
  3. valueSeparator:','
    ,
    //多选提交到后台的值分隔符
      
  4. value:'1,2'
    ,
    //  多值通过","分隔,与valueSeparator相对应,表示默认选择了"测试菜单1'"和"测试菜单2"
      

由于添加了多选CheckBox图标,所以需要在ext
-all.css文件最后添加两行支持样式:

Java代码
  1. .checked{background-image:url(../images/
    default
    /menu/checked.gif)}  
  2. .unchecked{background-image:url(../images/default
    /menu/unchecked.gif)}  

MultiComboBox的源代码:

Java代码
  1. Ext
    .form.MultiComboBox = Ext
    .extend(Ext
    .form.TriggerField, {  

  2.     defaultAutoCreate : {tag: "input"
    , type: 
    "text"
    , size: 
    "24"
    , autocomplete: 
    "off"
    },  
  3.     listClass: ''
    ,  
  4.     selectedClass: 'x-combo-selected'
    ,  
  5.     triggerClass : 'x-form-arrow-trigger'
    ,  
  6.     shadow:'sides'
    ,  
  7.     listAlign: 'tl-bl?'
    ,  
  8.     maxHeight: 300
    ,  
  9.     triggerAction: 'query'
    ,  
  10.     minChars : 4
    ,  
  11.     typeAhead: false
    ,  
  12.     queryDelay: 500
    ,  
  13.     pageSize: 0
    ,  
  14.     selectOnFocus:false
    ,  
  15.     queryParam: 'query'
    ,  
  16.     loadingText: 'Loading...'
    ,  
  17.     resizable: false
    ,  
  18.     handleHeight : 8
    ,  
  19.     editable: true
    ,  
  20.     allQuery: ''
    ,  
  21.     mode: 'remote'
    ,  
  22.     minListWidth : 70
    ,  
  23.     forceSelection:false
    ,  
  24.     typeAheadDelay : 250
    ,  
  25.     displaySeparator:';'
    ,  
  26.     valueSeparator:','
    ,  
  27.     lazyInit : true
    ,  
  28.   
  29.     initComponent : function(){  
  30.         Ext
    .form.ComboBox.superclass.initComponent.call(this
    );  
  31.         this
    .addEvents(  
  32.             'expand'
    ,  
  33.             'collapse'
    ,  
  34.             'beforeselect'
    ,  
  35.             'select'
    ,  
  36.             'beforequery'
      
  37.         );  
  38.         if
    (
    this
    .transform){  
  39.             this
    .allowDomMove = 
    false
    ;  
  40.             var s = Ext
    .getDom(this
    .transform);  
  41.             if
    (!
    this
    .hiddenName){  
  42.                 this
    .hiddenName = s.name;  
  43.             }  
  44.             if
    (!
    this
    .store){  
  45.                 this
    .mode = 
    'local'
    ;  
  46.                 var d = [], opts = s.options;  
  47.                 for
    (var i = 
    0
    , len = opts.length;i < len; i++){  
  48.                     var o = opts[i];  
  49.                     var value = (Ext
    .isIE ? o.getAttributeNode('value'
    ).specified : o.hasAttribute(
    'value'
    )) ? o.value : o.text;  
  50.                     if
    (o.selected) {  
  51.                         this
    .value = value;  
  52.                     }  
  53.                     d.push([value, o.text]);  
  54.                 }  
  55.                 this
    .store = 
    new
     Ext
    .data.SimpleStore({  

  56.                     'id'

    0
    ,  
  57.                     fields: ['value'

    'text'
    ],  
  58.                     data : d  
  59.                 });  
  60.                 this
    .valueField = 
    'value'
    ;  
  61.                 this
    .displayField = 
    'text'
    ;  
  62.             }  
  63.             s.name = Ext
    .id(); // wipe out the name in case somewhere else they have a reference
      
  64.             if
    (!
    this
    .lazyRender){  
  65.                 this
    .target = 
    true
    ;  
  66.                 this
    .el = Ext
    .DomHelper.insertBefore(s, 

    this
    .autoCreate || 
    this
    .defaultAutoCreate);  
  67.                 Ext
    .removeNode(s); // remove it
      
  68.                 this
    .render(
    this
    .el.parentNode);  
  69.             }else
    {  
  70.                 Ext
    .removeNode(s); // remove it
      
  71.             }  
  72.   
  73.         }  
  74.         this
    .selectedIndex = -
    1
    ;  
  75.         if<

抱歉!评论已关闭.