`

combox下拉列表多选(Ext.ux.form.LovCombo)

阅读更多

Ext.ux.form.LovCombo继承自Ext.form.ComboBox,实现了下拉列表的多选

Ext.ux.form.LovCombo的js源码为:

// add RegExp.escape if it has not been already added   
if('function' !== typeof RegExp.escape) {   
    RegExp.escape = function(s) {   
        if('string' !== typeof s) {   
            return s;   
        }   
        // Note: if pasting from forum, precede ]/\ with backslash manually   
        return s.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');   
    }; // eo function escape   
}   
  
// create namespace   
Ext.ns('Ext.ux.form');   
    
/**  
 *  
 * @class Ext.ux.form.LovCombo  
 * @extends Ext.form.ComboBox  
 */  
Ext.ux.form.LovCombo = Ext.extend(Ext.form.ComboBox, {   
  
    // {{{   
    // configuration options   
    /**  
     * @cfg {String} checkField name of field used to store checked state.  
     * It is automatically added to existing fields.  
     * Change it only if it collides with your normal field.  
     */  
     checkField:'checked',  
  
    /**  
     * @cfg {String} separator separator to use between values and texts  
     */  
    separator:',',  
  
    /**  
     * @cfg {String/Array} tpl Template for items.   
     * Change it only if you know what you are doing.  
     */  
    // }}}   
    // {{{   
    initComponent:function() {   
           
        // template with checkbox   
        if(!this.tpl) {   
            this.tpl =    
                 '<tpl for=".">'  
                +'<div class="x-combo-list-item">'  
                +'<img src="' + Ext.BLANK_IMAGE_URL + '" '  
                +'class="ux-lovcombo-icon ux-lovcombo-icon-'  
                +'{[values.' + this.checkField + '?"checked":"unchecked"' + ']}">'  
                +'<div class="ux-lovcombo-item-text">{' + (this.displayField || 'text' )+ '}</div>'  
                +'</div>'  
                +'</tpl>';   
        }   
    
        // call parent   
        Ext.ux.form.LovCombo.superclass.initComponent.apply(this, arguments);   
  
        // install internal event handlers   
        this.on({   
             scope:this  
            ,beforequery:this.onBeforeQuery   
            ,blur:this.onRealBlur   
        });   
  
        // remove selection from input field   
        this.onLoad = this.onLoad.createSequence(function() {   
            if(this.el) {   
                var v = this.el.dom.value;   
                this.el.dom.value = '';   
                this.el.dom.value = v;   
            }   
        });   
    
    }, // e/o function initComponent   
    // }}}   
    // {{{   
    /**  
     * Disables default tab key bahavior  
     * @private  
     */  
    initEvents:function() {   
        Ext.ux.form.LovCombo.superclass.initEvents.apply(this, arguments);   
  
        // disable default tab handling - does no good   
        this.keyNav.tab = false;   
  
    }, // eo function initEvents   
    // }}}   
    // {{{   
    /**  
     * clears value  
     */  
    clearValue:function() {   
        this.value = '';   
        this.setRawValue(this.value);   
        this.store.clearFilter();   
        this.store.each(function(r) {   
            r.set(this.checkField, false);   
        }, this);   
        if(this.hiddenField) {   
            this.hiddenField.value = '';   
        }   
        this.applyEmptyText();   
    }, // eo function clearValue   
    // }}}   
    // {{{   
    /**  
     * @return {String} separator (plus space) separated list of selected displayFields  
     * @private  
     */  
    getCheckedDisplay:function() {   
        var re = new RegExp(this.separator, "g");   
        return this.getCheckedValue(this.displayField).replace(re, this.separator + ' ');   
    }, // eo function getCheckedDisplay   
    // }}}   
    // {{{   
    /**  
     * @return {String} separator separated list of selected valueFields  
     * @private  
     */  
    getCheckedValue:function(field) {   
        field = field || this.valueField;   
        var c = [];   
  
        // store may be filtered so get all records   
        var snapshot = this.store.snapshot || this.store.data;   
  
        snapshot.each(function(r) {   
            if(r.get(this.checkField)) {   
                c.push(r.get(field));   
            }   
        }, this);   
  
        return c.join(this.separator);   
    }, // eo function getCheckedValue   
    // }}}   
    // {{{   
    /**  
     * beforequery event handler - handles multiple selections  
     * @param {Object} qe query event  
     * @private  
     */  
    onBeforeQuery:function(qe) {   
        qe.query = qe.query.replace(new RegExp(this.getCheckedDisplay() + '[ ' + this.separator + ']*'), '');   
    }, // eo function onBeforeQuery   
    // }}}   
    // {{{   
    /**  
     * blur event handler - runs only when real blur event is fired  
     */  
    onRealBlur:function() {   
        this.list.hide();   
        var rv = this.getRawValue();   
        var rva = rv.split(new RegExp(RegExp.escape(this.separator) + ' *'));   
        var va = [];   
        var snapshot = this.store.snapshot || this.store.data;   
  
        // iterate through raw values and records and check/uncheck items   
        Ext.each(rva, function(v) {   
            snapshot.each(function(r) {   
                if(v === r.get(this.displayField)) {   
                    va.push(r.get(this.valueField));   
                }   
            }, this);   
        }, this);   
        this.setValue(va.join(this.separator));   
        this.store.clearFilter();   
    }, // eo function onRealBlur   
    // }}}   
    // {{{   
    /**  
     * Combo's onSelect override  
     * @private  
     * @param {Ext.data.Record} record record that has been selected in the list  
     * @param {Number} index index of selected (clicked) record  
     */  
    onSelect:function(record, index) {   
        if(this.fireEvent('beforeselect', this, record, index) !== false){   
  
            // toggle checked field   
            record.set(this.checkField, !record.get(this.checkField));   
  			//record.set(this.displayField, !record.get(this.checkField));  
            // display full list   
            if(this.store.isFiltered()) {   
                this.doQuery(this.allQuery);   
            }   
            // set (update) value and fire event   
            this.setValue(this.getCheckedValue());   
            this.fireEvent('select', this, record, index);   
        }   
    }, // eo function onSelect   
    // }}}   
    // {{{   
    /**  
     * Sets the value of the LovCombo  
     * @param {Mixed} v value  
     */  
    setValue:function(v) {  
        if(v) {   
            v = '' + v;   
            if(this.valueField) {   
                this.store.clearFilter();
				//个人注释掉的,感觉没什么用
                /*this.store.each(function(r) {
					var re='(^|' + this.separator + ')' + RegExp.escape(r.get(this.valueField))+'(' + this.separator + '|$)';  
                    var checked = !(!v.match(re));   
  
                    r.set(this.checkField, checked);   
                }, this);*/   
                this.value = this.getCheckedValue();   
                this.setRawValue(this.getCheckedDisplay());   
                if(this.hiddenField) {   
                    this.hiddenField.value = this.value;   
                }   
            }   
            else {   
                this.value = v;   
                this.setRawValue(v);   
                if(this.hiddenField) {   
                    this.hiddenField.value = v;   
                }   
            }   
            if(this.el) {   
                this.el.removeClass(this.emptyClass);   
            }   
        }   
        else {   
            this.clearValue();   
        }   
    }, // eo function setValue   
    // }}}   
    // {{{   
    /**  
     * Selects all items  
     */  
    selectAll:function() {   
        this.store.each(function(record){   
            // toggle checked field   
            record.set(this.checkField, true);   
        }, this);   
  
        //display full list   
        this.doQuery(this.allQuery);   
        this.setValue(this.getCheckedValue());   
    }, // eo full selectAll   
    // }}}   
    // {{{   
    /**  
     * Deselects all items. Synonym for clearValue  
     */  
    deselectAll:function() {   
        this.clearValue();   
    } // eo full deselectAll    
    // }}}   
  
}); // eo extend   
    
// register xtype   
Ext.reg('lovcombo', Ext.ux.form.LovCombo);    
    
// eof  

 

 

CSS样式:

.ux-lovcombo-icon {   
		    width:16px;   
		    height:16px;   
		    float:left;   
		    background-position: -1px -1px ! important;   
		    background-repeat:no-repeat ! important;   
		}   
		.ux-lovcombo-icon-checked {   
		    background: transparent url(../../resources/images/default/menu/checked.gif);   
		}   
		.ux-lovcombo-icon-unchecked {   
		    background: transparent url(../../resources/images/default/menu/unchecked.gif);   
		}  

 

 示例:

Ext.onReady(function(){
 	var lc = new Ext.ux.form.LovCombo({   
        id:'lovcombo',
        renderTo:'lovcomboct',  
        width:300,  
        hideOnSelect:false,  
        maxHeight:200,  
        readOnly:true,  
        editable:false,  
      	store:new Ext.data.SimpleStore({   
          id:0,   
          fields:[{name:'id',type:'int'}, 'privGroup'],   
          data:[   
               [1, 'Personnel']   
              ,[11, 'Finance']   
              ,[2, 'Management']   
              ,[22, 'Production']   
              ,[3, 'Users']   
          ]   
      }),   
        triggerAction:'all',  
        valueField:'id',  
		//checkField:"id"
        displayField:'privGroup',   
        mode:'local'  
    });   
  
});

 效果:

 

  • 大小: 2.8 KB
分享到:
评论

相关推荐

    EXT控件lovcombo

    在EXT控件中,"lovcombo"(即Love Combo)是一种特殊的ComboBox,通常用于显示一个列表,其中每个项可以被选择,类似于下拉框。这个lovcombo控件的特色在于它支持多选,通过复选框(checkboxes)来实现这一功能,...

    C# COMBOX 树下拉,多选下拉等功能

    在C#编程中,COMBO BOX(组合框)控件是一种常见的用户界面元素,用于提供一个下拉列表供用户选择。然而,有时我们可能需要扩展其功能,使其具备树形结构或者支持多选。这样的需求在开发复杂的Windows应用程序时经常...

    CheckBoxList_WinForm多选下拉框_下拉多选_

    在这个类中,我们可以添加一个`ComboBox`和一个`ListBox`控件,然后通过代码逻辑将它们组合起来,以达到下拉多选的效果。 2. **界面设计**: 在`UserControl`上布局`ComboBox`和`ListBox`,并设置适当的属性,如`...

    combobox实现下拉框多选

    本方法和用checkbox, listbox等控件和事件拼凑出来的不同,本方法是一个集成的独立控件,基本实现了控件的顺滑度,下拉框可悬浮等效果,可以认为是comboBox的升级版,使用方便,仅需引用编译好的DLL,直接在toolBox...

    支持鼠标滚轮的ComBox下拉列表控件.rar

    当我们谈论“支持鼠标滚轮的ComBox下拉列表控件”时,这通常意味着开发者已经对标准的ComboBox控件进行了增强,使其能够通过鼠标的滚轮来滚动其下拉列表的部分。 ComboBox控件是Windows API、MFC、WinForms、WPF等...

    VB增强版ListView ComBox下拉列表控件.rar

    VB增强版ListView ComBox下拉列表控件,属性说明:  1.LVHead:列标题字符串,各列标题间用半角逗号“,”隔开  2.LVWidth:列宽度,也用“,”隔开,数目应与LVHead相同,值的大小取要显示的字符数  3.Style:下列...

    自定义Combox + CheckBox 的多选控件

    这个控件结合了ComboBox的下拉列表功能和CheckBox的选择特性,为开发者提供了一个方便的组件,以创建具有多选功能的下拉菜单。 首先,我们要理解ComboBox的基础知识。ComboBox在WPF中是一个可下拉的文本框,通常...

    Combobox下拉列表显示图片.zip

    这个"Combobox下拉列表显示图片.zip"文件可能包含了实现这一功能的代码示例或教程。 在Windows Forms应用程序中,Combobox默认只支持显示文本,但通过自定义绘制或者使用第三方控件,我们可以实现显示图片与文本...

    VBCombox实现下拉

    在VB(Visual Basic)编程中,ComboBox控件是一种常见的用户界面元素,用于提供一个下拉列表供用户选择。本文将详细讲解如何实现VB ComboBox的自动下拉功能,并结合描述中的"用了别人的代码修改了一下"这一线索,...

    COMBOX_add_pic.rar_combox.add

    它由两部分组成:一个文本输入框和一个下拉列表。当用户点击下拉按钮时,显示预定义的选项列表。常规的选项只包含纯文本,但通过编程技术,我们可以将每个选项与相应的图片关联起来,形成图文并茂的列表。 要实现这...

    Winform 下拉带Checkbox 功能

    本主题将深入探讨如何实现一个下拉列表框(ComboBox)控件,该控件带有复选框功能,即"Winform 下拉带Checkbox 功能"。这种控件通常用于让用户在多个选项中进行多选操作。 首先,我们需要理解控件的基本构造。在C#...

    C# 基于ComboBox 下拉多选 自定义控件 源代码

    3、在下拉列表中可显示自定义的多列数据。 4、可在下拉列表中通过输入关键字,自动搜索符合条件的数据行。 5、可指定不同的值列和显示列。 6、可设置是否显示行头和列头。 7、程序根据显示列的多少和宽度,自动调整...

    combox加Cekbox实现多选按钮

    combox加Cekbox实现多选按钮 combox实现多选功能并输出到textbox中

    VB 实现Combox下拉列表颜色选择控件 (1).zip

    标题“VB 实现Combox下拉列表颜色选择控件 (1).zip”所提及的,就是一种用VB实现的特殊ComboBox控件,它不仅具有标准的下拉列表功能,还能展示颜色选项,让用户能够方便地选取所需颜色。 ComboBox控件是VB中的基础...

    uniapp/h5多选模糊搜索下拉框(可以进行搜索匹配以及多选功能)

    uniapp/h5通用模糊下拉搜索下拉多选框; 使用步骤: 1、下载资源包,并进行解压; 2、将components文件夹中的文件拷贝到自己项目的components文件夹中; 3、在业务文件夹中引用组件multipleDataPickey,使用实例如...

    combox下拉树combox下拉树

    在IT行业中,`Combox`(组合框)是一种常见的用户界面元素,它结合了文本输入框和下拉列表的功能,通常用于提供用户选择一个或多个预定义的选项。当需要在下拉列表中呈现层级结构时,我们就会涉及到“Combox下拉树”...

Global site tag (gtag.js) - Google Analytics