`
mutongwu
  • 浏览: 448446 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

MultiselectItemSelector 多选框

    博客分类:
  • Ext
阅读更多

改造自MultiselectItemSelector(http://extjs.com/learn/Extension:Multiselect)。
原有代码在关闭该控件以后,会遗留大量无效代码,因此,自己用一个 panel + 2个grid来实现,暂不支持拖拽。

Ext.namespace('Ext.ux')
Ext.ux.IconPanel = function(config){
    Ext.apply(this,config);
    Ext.ux.IconPanel.superclass.constructor.call(this,config);
}

Ext.extend(Ext.ux.IconPanel,Ext.Panel,{
    imagePath:'',
    drawUpIcon:true,
    drawDownIcon:true,
    drawLeftIcon:true,
    drawRightIcon:true,
    drawTopIcon:true,
    drawBotIcon:true,
    
    //style: 'padding-left:5px;padding-right:5px',
    
    initComponent: function(){
    
        Ext.ux.IconPanel.superclass.initComponent.call(this);
        this.addEvents(
            ''
        )
    },
    onRender: function(ct, position){
       Ext.ux.IconPanel.superclass.onRender.call(this,ct,position);
       
       if (this.imagePath!="" && this.imagePath.charAt(this.imagePath.length-1)!="/")
            this.imagePath+="/";
        this.iconUp = this.imagePath + (this.iconUp || 'up2.gif');
        this.iconDown = this.imagePath + (this.iconDown || 'down2.gif');
        this.iconLeft = this.imagePath + (this.iconLeft || 'left2.gif');
        this.iconRight = this.imagePath + (this.iconRight || 'right2.gif');
        this.iconTop = this.imagePath + (this.iconTop || 'top2.gif');
        this.iconBottom = this.imagePath + (this.iconBottom || 'bottom2.gif');
        var el= this.getEl();
        if (!this.toSortField) {
            this.toTopIcon = el.createChild({tag:'img', src:this.iconTop, style:{cursor:'pointer', margin:'2px'}});
            el.createChild({tag: 'br'});
            this.upIcon = el.createChild({tag:'img', src:this.iconUp, style:{cursor:'pointer', margin:'2px'}});
            el.createChild({tag: 'br'});
        }
        this.addIcon = el.createChild({tag:'img', src:this.switchToFrom?this.iconLeft:this.iconRight, style:{cursor:'pointer', margin:'2px'}});
        el.createChild({tag: 'br'});
        this.removeIcon = el.createChild({tag:'img', src:this.switchToFrom?this.iconRight:this.iconLeft, style:{cursor:'pointer', margin:'2px'}});
        el.createChild({tag: 'br'});
        if (!this.toSortField) {
            this.downIcon = el.createChild({tag:'img', src:this.iconDown, style:{cursor:'pointer', margin:'2px'}});
            el.createChild({tag: 'br'});
            this.toBottomIcon = el.createChild({tag:'img', src:this.iconBottom, style:{cursor:'pointer', margin:'2px'}});
        }
        if (!this.readOnly) {
            if (!this.toSortField) {
                this.toTopIcon.on('click', this.toTop, this);
                this.upIcon.on('click', this.up, this);
                this.downIcon.on('click', this.down, this);
                this.toBottomIcon.on('click', this.toBottom, this);
            }
            this.addIcon.on('click', this.fromTo, this);
            this.removeIcon.on('click', this.toFrom, this);
        }
        if (!this.drawUpIcon || this.hideNavIcons) { this.upIcon.dom.style.display='none'; }
        if (!this.drawDownIcon || this.hideNavIcons) { this.downIcon.dom.style.display='none'; }
        if (!this.drawLeftIcon || this.hideNavIcons) { this.addIcon.dom.style.display='none'; }
        if (!this.drawRightIcon || this.hideNavIcons) { this.removeIcon.dom.style.display='none'; }
        if (!this.drawTopIcon || this.hideNavIcons) { this.toTopIcon.dom.style.display='none'; }
        if (!this.drawBotIcon || this.hideNavIcons) { this.toBottomIcon.dom.style.display='none'; }

    },
    
    toTop:function(){
        this.gridsHolder.fireEvent('toTop');
    },
    toBottom: function(){
        this.gridsHolder.fireEvent('toBottom');
    },
    up: function(){
        this.gridsHolder.fireEvent('up');
    },
    down: function(){
        this.gridsHolder.fireEvent('down');
    },
    fromTo: function(){
        this.gridsHolder.fireEvent('fromTo');
    },
    toFrom: function(){
        this.gridsHolder.fireEvent('toFrom');
    },

    setGridsHoder: function(gridsHolder){
        this.gridsHolder = gridsHolder;
    }
});
Ext.ux.MultiSelectWin = function(config){
	Ext.apply(this,config);
	Ext.ux.MultiSelectWin.superclass.constructor.call(this, config);
	this.init();
};

Ext.extend(Ext.ux.MultiSelectWin, Ext.util.Observable,{
	
	title : '选择窗口',
	from_legend:'可选',
	to_legend: '已选',
	valueField: 'id',
	displayField: 'name',
    warningTitle: '提示',
    minLength:0,
    maxLength:Number.MAX_VALUE,
    minLengthText:'至少应选择{0}项',
    maxLengthText:'最多只能选择 {0}项',
    /**
     * 图标面板的config
     * @type {}
     */
    iconConfig:{},
	targetItem: null,
    localData:[],
	selectedData: [],
    saveFn: Ext.emptyFn,
	searchFn: Ext.emptyFn,
	baseParams: null,
	fieldName: null,

    toSortField:null,
    fromSortField:null,
    toSortDir:'ASC',
    fromSortDir:'ASC',
    allowDup: false,
    
	switchToFrom:false,
	openHeight:320,
	openWidth:460,
	ms_height:220,
	ms_width:200,


	init: function(){
		this.initDataStore();
		
		this.initComponent();
	},
	
	initDataStore: function(){
		var record = Ext.data.Record.create([
			{
				name: this.valueField,
				type: 'string'
			},{
				name: this.displayField,
				type: 'string'
		}]);
		if (this.localMode == true){
			this.fromStore  = new Ext.data.SimpleStore({
	            fields:[this.valueField, this.displayField]
	        });
		}
		else{
			var baseParams = {
					dir : 'asc',
					start : 0,
					limit : 0
				};

			if (this.baseParams)
				baseParams = this.baseParams;

			this.fromStore  = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(this.searchFn, true),
				reader: new Ext.data.ListRangeReader({
					totalProperty : 'total',
					id : 'id'
				},record),
				sortInfo:{field:this.displayField,direction:'ASC'},
				baseParams : baseParams
			});
		}
		
        this.selectedData = this.targetItem.getArrayValue();       
		this.toStore = new Ext.data.SimpleStore({
			fields:[this.valueField, this.displayField],
			data: this.selectedData
		});
		
        if(this.allowDup == false){
	        this.fromStore.on('load',function(){
	            this.fromStore.filterBy(function(record){
	                    for (var i=0;i < this.selectedData.length;i++){
	                        if(this.selectedData[i][0] == record.get(this.valueField))
	                            return false;
	                    }
	                    return true;
	                },this);    
	        },this);
        }
		
		if(this.localMode == true)
            this.fromStore.loadData(this.localData);
        else
            this.fromStore.load();
		
	},
	
	reComputeWH: function(){
		this.openHeight = this.openHeight;
		this.openWidth = this.openWidth;
		this.ms_height = this.openHeight - 100;
		this.ms_width = (this.openWidth-60)/2;
	},
    createIconsPanel: function(){
        this.iconsPanel = new Ext.ux.IconPanel(this.iconConfig);
        this.iconsPanel.setGridsHoder(this);        
    },
	initComponent: function(){
       
        if(this.openHeight || this.openWidth){
            this.reComputeWH();
        }
        this.addEvents(
                /**
                 * @event
                 */
                'destroy',

                /**
                 * @event
                 */
                'toTop',

                /**
                 * @event
                 */
                'toBottom',

                /**
                 * @event
                 */
                'up',

                /**
                 * @event
                 */
                'down',

                /**
                 * @event
                 */
                'fromTo',

                /**
                 * @event
                 */
                'toFrom');

        this.on('destroy', this.onDestroy, this);
        this.on('up', this.onUp, this);
        this.on('down', this.onDown, this);
        this.on('toTop', this.onToTop, this);
        this.on('toBottom', this.onToBottom, this);
        this.on('fromTo', this.onFromTo, this);
        this.on('toFrom', this.onToFrom, this);
        
        
        this.fromGrid = new Ext.grid.GridPanel({
            title : '可选',
            hideHeaders:true,
            width : this.ms_width,
            height: this.ms_height,
            autoExpandColumn:'name',
            layout : 'fit',
            sm : new Ext.grid.RowSelectionModel(),
            ds : this.fromStore,
            cm : new Ext.grid.ColumnModel([{
	            header : "header",
	            sortable : true,
	            dataIndex : this.displayField
	        }]),
            viewConfig:{forceFit:true}
        })

        this.fromGrid.on('rowdblclick', this.onFromGriddblclick, this);
        
        this.toGrid = new Ext.grid.GridPanel({
            title : '已选',
            hideHeaders:true,
            width : this.ms_width,
            height: this.ms_height,
            autoExpandColumn:'name',
            layout : 'fit',
            sm : new Ext.grid.RowSelectionModel(),
            ds : this.toStore,
            cm : new Ext.grid.ColumnModel([{
                header : "header",
                sortable : true,
                dataIndex : this.displayField
            }]),
            viewConfig:{forceFit:true}
        })
        
        this.toGrid.on('rowdblclick', this.onToGriddblclick, this);
        
        this.createIconsPanel();
        this.parentPanel = new Ext.Panel({
            layout:"table",
            layoutConfig:{columns:3},
            items: [this.fromGrid,this.iconsPanel,this.toGrid],
            frame: true
        });       

        this.selectorWin = new Ext.Window({
            frame: true,
            resizable:false,
            title : this.title,
            layout:'fit',
            modal: true,
            items : [this.parentPanel],
            width: this.openWidth,
            height: this.openHeight,
            buttonAlign:'center',
            buttons:[{
                text: '确定',
                handler: function(){
                  var records = this.toStore.getRange();
                  if(this.validateValue(records) == false)
                     return;
                  var values = [];
                  var selectedValue = '';
                  var selectedText = '';
                  
                  for (var i=0; i < records.length; i++){
                    var temp = [];
                    var v = records[i].get(this.valueField);
                    var t = records[i].get(this.displayField);
                    temp.push(v);
                    temp.push(t);
                    values.push(temp);
                    
                    if(selectedValue == ''){
                       selectedValue = v;
                       selectedText = t;
                    }
                    else{
                        selectedValue = selectedValue + ',' + v;
                        selectedText = selectedText + ',' + t;
                    }
                  }
                  this.targetItem.setValue(values);
                  var formValues = queryPanel.getFormValues();
                  Ext.apply(formValues,this.baseParams);
                  formValues.selectedValue = selectedValue;
                  formValues.selectedText = selectedText;
                  var fn = this.saveFn.createSequence(function(returnData){});
                  fn(formValues);
                  
                  this.selectorWin.close();
                },
                scope:this
            },{ 
                text:'取消',
                handler: function(){
                    this.selectorWin.close();
                },
                scope: this
            }]
        })
        
        this.selectorWin.show();
    },
    
    validateValue : function(value){
        if (value.length < this.minLength) {
            Ext.Msg.show({
               title: this.warningTitle,
               msg: String.format(this.minLengthText, this.minLength),
               buttons: Ext.Msg.OK,
               icon: Ext.MessageBox.INFO
            });
            return false;
        }
        if (value.length > this.maxLength) {
            Ext.Msg.show({
               title: this.warningTitle,
               msg: String.format(this.maxLengthText, this.maxLength),
               buttons: Ext.Msg.OK,
               icon: Ext.MessageBox.INFO
            });
            return false;
        }
        return true;
    },
    onFromGriddblclick: function(){
        this.fireEvent('fromTo');
    },
    onToGriddblclick: function(){
        this.fireEvent('toFrom');
    },
    onUp:function(){
        var store = this.toStore;
        var sm = this.toGrid.getSelectionModel();
        var data = sm.getSelections();
        
        var selectionsArray = [];
        for (var i=0; i < data.length; i++){
            selectionsArray.push(store.indexOf(data[i]));
        }
        var record = null;
        selectionsArray.sort();
        var newSelectionsArray = [];
        if (selectionsArray.length > 0) {
            for (var i=0; i<selectionsArray.length; i++) {
                record = store.getAt(selectionsArray[i]);
                if ((selectionsArray[i] - 1) >= 0) {
                    store.remove(record);
                    store.insert(selectionsArray[i] - 1, record);
                    newSelectionsArray.push(selectionsArray[i] - 1);
                }
            }
           this.toGrid.getView().refresh();
           sm.selectRows(newSelectionsArray);
        }    
    },
    onDown: function(){
        var store = this.toStore;
        var sm = this.toGrid.getSelectionModel();
        var data = sm.getSelections();
        
        var selectionsArray = [];
        for (var i=0; i < data.length; i++){
            selectionsArray.push(store.indexOf(data[i]));
        }
        var record = null;
        selectionsArray.sort();
        selectionsArray.reverse();
        var newSelectionsArray = [];
        if (selectionsArray.length > 0) {
            for (var i=0; i<selectionsArray.length; i++) {
                record = store.getAt(selectionsArray[i]);
                if ((selectionsArray[i] + 1) < store.getCount()) {
                    store.remove(record);
                    store.insert(selectionsArray[i] + 1, record);
                    newSelectionsArray.push(selectionsArray[i] + 1);
                }
            }
            this.toGrid.getView().refresh();
            sm.selectRows(newSelectionsArray);
        }
    },
    onToTop: function(){
        var store = this.toStore;
        var sm = this.toGrid.getSelectionModel();
        var data = sm.getSelections();
        
        var selectionsArray = [];
        for (var i=0; i < data.length; i++){
            selectionsArray.push(store.indexOf(data[i]));
        }
        var records = [];
        if (selectionsArray.length > 0) {
            selectionsArray.sort();
            for (var i=0; i<selectionsArray.length; i++) {
                record = store.getAt(selectionsArray[i]);
                records.push(record);
            }
            selectionsArray = [];
            for (var i=records.length-1; i>-1; i--) {
                record = records[i];
                store.remove(record);
                store.insert(0, record);
                selectionsArray.push(((records.length - 1) - i));
            }
        }
        this.toGrid.getView().refresh();
        sm.selectRows(selectionsArray);
     },
    onToBottom: function(){
        var store = this.toStore;
        var sm = this.toGrid.getSelectionModel();
        var data = sm.getSelections();
        
        var selectionsArray = [];
        for (var i=0; i < data.length; i++){
            selectionsArray.push(store.indexOf(data[i]));
        }
        var records = [];
        if (selectionsArray.length > 0) {
            selectionsArray.sort();
            for (var i=0; i<selectionsArray.length; i++) {
                record = store.getAt(selectionsArray[i]);
                records.push(record);
            }
            selectionsArray = [];
            for (var i=0; i<records.length; i++) {
                record = records[i];
                store.remove(record);
                store.add(record);
                selectionsArray.push((store.getCount()) - (records.length - i));
            }
        }
        this.toGrid.getView().refresh();
        sm.selectRows(selectionsArray);
    },
    onFromTo: function(){

        var records = this.fromGrid.getSelectionModel().getSelections();
 
        var selectionsArray = [];
        for (var i=0; i < records.length; i++){
            selectionsArray.push(this.fromStore.indexOf(records[i]));
        }
        if (selectionsArray.length > 0) {
            if(!this.allowDup)
                selectionsArray = [];
            for (var i=0; i<records.length; i++) {
                record = records[i];
                if(this.allowDup){
                    var x=new Ext.data.Record();
                    record.id=x.id;
                    delete x;   
                    this.toStore.add([record]);
                }else{
                    this.fromStore.remove(record);
                    this.toStore.add([record]);
                    selectionsArray.push((this.toStore.getCount() - 1));
                }
            }
        }
        this.toGrid.getView().refresh();
        this.fromGrid.getView().refresh();
        if(this.toSortField)
            this.toStore.sort(this.toSortField, this.toSortDir);
        if(this.allowDup)
            this.fromGrid.getSelectionModel().selectRows(selectionsArray);
        else 
            this.toGrid.getSelectionModel().selectRows(selectionsArray);    
    },
    onToFrom: function(){
        var records = this.toGrid.getSelectionModel().getSelections(); 
        var selectionsArray = [];
        if (records.length > 0) {
            
            for (var i=0; i<records.length; i++) {
                record = records[i];
                this.toStore.remove(record);
                if(!this.allowDup){
                    this.fromStore.add([record]);
                    selectionsArray.push((this.fromStore.getCount() - 1));
                }
            }
        }
        this.fromGrid.getView().refresh();
        this.toGrid.getView().refresh();
        if(this.fromSortField)
            this.fromStore.sort(this.fromSortField, this.fromSortDir);
        this.fromGrid.getSelectionModel().selectRows(selectionsArray);    
    },
    
	onDestroy:function(){

        this.toStore = null;
        this.toGrid = null;
        this.fromStore = null;
        this.fromGrid = null;
    }
});
Ext.reg('multiSelectWin',Ext.ux.MultiSelectWin);


截图


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

相关推荐

    Extjs MultiselectItemSelector

    首先,`MultiselectItemSelector` 提供了一个用户友好的界面,通常会显示一个可滚动的列表,用户可以通过勾选复选框来选择多个项目。这种组件的交互设计考虑了易用性和效率,使得用户可以快速地在大量数据中进行多选...

    基于Springboot的实验报告系统源码数据库文档.zip

    基于Springboot的实验报告系统源码数据库文档.zip

    ERA5_Climate_Single_Month.txt

    GEE训练教程——Landsat5、8和Sentinel-2、DEM和各2哦想指数下载

    基于springboot智能健康饮食系统源码数据库文档.zip

    基于springboot智能健康饮食系统源码数据库文档.zip

    基于SpringBoot的校园服务系统源码数据库文档.zip

    基于SpringBoot的校园服务系统源码数据库文档.zip

    史上最全IXIA测试仪配置使用指导手册(含IxNetwork,图文并茂超详细!).zip

    内容概要: IXIA测试仪的基本配置.doc ixia测试仪基础使用示例.doc IxNetwork如何进行抓包回放-V1.0.pdf IxNetwork如何自定义报文-V2.0.pdf ixia构造ip分片方法.txt IxNetwork使用简介.pdf 适用人群:网络协议造包、打流相关的测试工程技术人员,想要学习的同学可以下载哈 使用场景:构造pcap包,打流 Ixia简介 IXIA使用的是Server-client模式,Server端在测试仪表的主机上,在开机后会随着主机内的操作系统的启动而自动启动,一般情况下不需要人为的手工启动。因此在通常不需要为主机配置专用的显示器和键盘。 client端包括两个测试软件: Ixia Explorer和ScriptMate。这两个软件一般安装在测试用计算机上,在仪表自带的主机中也有这两个软件。根据测试项目的不同来选择使用不同的软件。Ixia Explorer主要提供数据流的测试,针对设备的功能进行测试; ScriptMate提供各种性能测试窗口,针对设备的性能进行测试。 Auto:自动分配;

    基于Python+Django花卉商城系统源码数据库文档.zip

    基于Python+Django花卉商城系统源码数据库文档.zip

    Umi-OCR-main.zip

    Umi-OCR-main.zip

    微信小程序源码-促销抽奖.zip

    基于微信小程序开发的促销抽奖小工具源码,适用于初学者了解小程序开发过程以及简单抽奖工具的实现。

    Sen2_median.txt

    GEE训练教程——Landsat5、8和Sentinel-2、DEM和各2哦想指数下载

    springboot的概要介绍与分析

    以下是一个关于Spring Boot设计的资源描述及项目源码的简要概述: Spring Boot设计资源描述 Spring Boot是一个为基于Spring的应用提供快速开发工具的框架,其设计旨在简化Spring应用的初始搭建和开发过程。以下是一些关键资源: Spring Boot官方文档:详细阐述了Spring Boot的核心特性、自动配置原理、起步依赖、内嵌式服务器等关键概念。这是学习和掌握Spring Boot设计的首选资源。 在线教程与视频:各大在线教育平台提供了丰富的Spring Boot教程和视频课程,从基础入门到高级应用,帮助开发者全面了解和掌握Spring Boot设计。 书籍与电子资料:许多技术书籍和在线电子资料深入讲解了Spring Boot的设计原理、最佳实践和项目案例,为开发者提供了宝贵的学习资源。 项目源码示例 以下是一个简单的Spring Boot项目源码示例,用于演示Spring Boot的基本结构和自动配置功能: java // 引入Spring Boot依赖 @SpringBootApplication public class MySpri

    基于springboot美妆领域管理系统源码数据库文档.zip

    基于springboot美妆领域管理系统源码数据库文档.zip

    tables-3.7.0+gpl-cp37-cp37m-win_amd64.whl

    tables-3.7.0+gpl-cp37-cp37m-win_amd64.whl

    算法实现的概要介绍与分析

    算法是计算机科学的核心,它们在解决各种问题时发挥着关键作用。一个好的算法不仅可以提高程序的效率,还可以简化复杂的问题。下面我将通过一个具体的例子——快速排序算法(Quick Sort)——来展示算法的实现过程,包括资源描述和项目源码。 ### 快速排序算法简介 快速排序是一种高效的排序算法,采用分治法的思想。其基本步骤如下: 1. 从数列中挑出一个元素,称为“基准”(pivot)。 2. 重新排序数列,所有比基准值小的元素放到基准前面,所有比基准值大的元素放到基准后面(相同的数可以到任一边)。在这个分割结束之后,该基准就处于数列的中间位置。这个称为分割(partition)操作。 3. 递归地(recursive)把小于基准值的子数列和大于基准值的子数列排序。 ### 资源描述 快速排序算法因其高效性和简洁性,在实际应用中非常受欢迎。它的时间复杂度平均为 O(n log n),最坏情况下为 O(n^2),但这种情况很少发生。快速排序的空间复杂度为 O(log n),因为它使用了递归来实现。 快速排序的一个典型应用场景是在数据库系统中对大量数据进行排序。由于它的高效性,快速排序

    基于springboot农场投入品运营线上管理系统源码数据库文档.zip

    基于springboot农场投入品运营线上管理系统源码数据库文档.zip

    基于springboot个性化影院推荐系统源码数据库文档.zip

    基于springboot个性化影院推荐系统源码数据库文档.zip

    linux基础进阶笔记

    linux基础进阶笔记,配套视频:https://www.bilibili.com/list/474327672?sid=4493093&spm_id_from=333.999.0.0&desc=1

    微信自动抢红包动态库.zip程序资源学习资料参考

    小程序 微信自动抢红包动态库.zip程序资源学习资料参考

    iOS版微信抢红包插件(支持后台抢红包).zip

    小程序 iOS版微信抢红包插件(支持后台抢红包).zip

    经典-FPGA时序约束教程

    经典-FPGA时序约束教程

Global site tag (gtag.js) - Google Analytics