吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3519|回复: 8
收起左侧

[会员申请] 申请会员ID:renwuqiangg【申请通过】

[复制链接]
吾爱游客  发表于 2017-2-3 13:58
1、申 I D renwuqiangg
2、个人邮箱:renwuqiangg@126.com

我自己写的mulselect-grid插件,是插件是基于extjs3的。可以实现的功能是对grid进行多选(可配合ctrl和shift),拖拽多选,点击标题行或第一列选择一列或一行,就像execl那样。并可以按ctrl+c复制到建贴板(仅支持ie)。

[JavaScript] 纯文本查看 复制代码
Ext.ns('Ext.rr.grid');
Ext.rr.grid.MultiCellSelectionModel = Ext.extend(Ext.grid.CellSelectionModel, {

    selections : [], // 选择区缓存

    constructor : function() {
        Ext.rr.grid.MultiCellSelectionModel.superclass.constructor.call(this);
    },
    initEvents : function() {
        Ext.rr.grid.MultiCellSelectionModel.superclass.initEvents.call(this);    
    },

    // 根据行列以及是否按住Ctrl键或者shift键,执行多选操作的逻辑
    handleMouseDown : function(grid, row, col, event) {
        if(event.button !== 0 || this.isLocked())
            return;
        
        if(event.shiftKey&&this.selection!=null){
                if(col == 0){
                        var r1 = Math.min(this.selection.cell[0],row);
                        var r2 = Math.max(this.selection.cell[0],row);
                        if(!event.ctrlKey)
                                        this.clearAllSelections();
                        for (var i = r1; i < r2+1; i++) {
                                this.selectRow(i,true);
                        }
                }else{
                        this.selectMatrix(row, col, event.ctrlKey);
                }
        }else{                
                if(this.selection!=null&&this.selections[this.selection.cell[0]][this.selection.cell[1]]){
                        var tmp = [this.selection.cell[0],this.selection.cell[1]];
                            this.select(row,col);
                            if(event.ctrlKey){
                                    //选了新的cell且按住ctrl的话,上一个cell虽然的选中状态,但是【选中】样式会被清除,这里把样式补回来
                                    this.grid.getView().onCellSelect(tmp[0], tmp[1]);
                            }
                }else
                        this.select(row,col);
                if(col == 0)
                        this.selectRow(row,event.ctrlKey);
                else
                        this.selectCell(row, col, event.ctrlKey);              
        } 
    },
    handleKeyDown : function(e){
        var key = e.getKey(),
        g = this.grid,
        v = g.getView(),
        selections = this.selections,
        sm = this;
        // Ctrl+c
        if(e.ctrlKey&&key==67){
                var row_flag = [];//未选的行index
                var col_flag = [];//未选的列index
                for (var i = 0; i < selections.length; i++) {
                        if(selections[i]==null){
                                row_flag[i] = 0;
                                continue;
                        }
                        var flag = false;
                        for (var j = 0; j < selections[i].length; j++) {
                                if(selections[i][j]==1){
                                        flag = true;
                                        break;
                                }
                        }
                        if(!flag)
                                row_flag[i] = 0;
                }
                for (var i = 0; i < g.getColumnModel().getColumnCount(); i++) {
                        var flag = false;
                        for (var r = 0; r < selections.length; r++) {
                                if(selections[r]==null){
                                        continue;
                                }else if(selections[r][i]==1){
                                        flag = true;
                                        break;
                                }
                        }
                        if(!flag)
                                col_flag[i] = 0;
                }
                var str = "";
                for ( var i = 0; i < selections.length; i++) {                       
                        if(selections[i]==null||row_flag[i]==0) continue;
                        for ( var j = 0; j < selections[i].length; j++) {
                                if(selections[i][j]==1){
                                        str+=v.getCell(i,j).firstChild.innerText;
                                }
                                if(j<selections[i].length-1&&col_flag[j]!=0){
                                        str+="\t";
                                }
                        }
                        str+="\r\n";
                        }
                var clipboard = window.clipboardData;
                var i=0;
                copyStr = function (){
                        clipboard.clearData();
                        clipboard.setData("Text",str);
                        
                        if(++i>10) return;
                        var text = clipboard.getData('Text');
                        if(text!=null) return;
                          setTimeout("copyStr()",20);
                }
                copyStr(str);
        }
    },
    // 清楚选择区内所以单元格被选中的样式
    clearAllSelections : function(){    
        for(var i = 0; i < this.selections.length; i++){
                if(this.selections[i]==null) continue;
                for ( var j = 0; j < this.selections[i].length; j++) {
                                if(this.selections[i][j]==1){
                                        this.grid.view.onCellDeselect(i, j); // GridView的内置方法,改变某单元格样式
                                }
                        }
        }
        this.selections = [];
    },
    // 选中某个单元格
    selectCell : function(rowIndex, colIndex, keepExisting){
        if(!keepExisting)
            this.clearAllSelections();
        if(this.selections[rowIndex]==null){
                this.selections[rowIndex] = [];
        }
        if(this.selections[rowIndex][colIndex] == 1){
                this.grid.view.onCellDeselect(rowIndex, colIndex);// 调用父类的方法,去掉【选中】样式
                this.selections[rowIndex][colIndex] = 0;
                
        }else{
                this.grid.getView().onCellSelect(rowIndex, colIndex);// 调用父类的方法,增加 【选中】样式
                this.selections[rowIndex][colIndex] = 1;
        }
    },
    // 选中某一行
    selectRow : function(rowIndex, keepExisting){
        var clen = this.grid.getColumnModel().getColumnCount();
        if(!keepExisting)// 是否清空所有已选择的单元格
            this.clearAllSelections();
        for(var c = 0; c < clen; c++){
            this.selectCell(rowIndex, c, true);
        }
    },
    selectCol: function(colIndex, event){
            if(!event.ctrlKey)
                    this.clearAllSelections();
                    
            if(event.shiftKey && this.selection!=null){
                    var c1 = Math.min(colIndex,this.selection.cell[1]);
                    var c2 = Math.max(colIndex,this.selection.cell[1]);
                    for ( var i = 0; i < this.grid.getStore().getCount(); i++) {
                                for ( var j = c1; j < c2+1; j++) {
                                        this.selectCell(i, j, true);
                                }
                        }
            }else{                   
                    if(this.selection!=null){
                        var tmp = [this.selection.cell[0],this.selection.cell[1]];
                            this.select(0,colIndex);
                            if(event.ctrlKey){
                                    //选了新的cell且按住ctrl的话,上一个cell虽然的选中状态,但是【选中】样式会被清除,这里把样式补回来
                                    this.grid.getView().onCellSelect(tmp[0], tmp[1]);
                            }
                }else{
                        this.select(0,colIndex); 
                }
                    for(var i = 0;i < this.grid.getStore().getCount(); i++){
                            this.selectCell(i, colIndex, true);
                    }
            }
    },
    // 选中矩形区域内所有的单元格,以上一次被选择的单元格或者cell为起点,
    selectMatrix : function(row, col, keepExisting,cell){
            cell=cell||this.selection.cell;
        if(!keepExisting)
            this.clearAllSelections();

            var r1 = Math.min(row,cell[0]);
            var r2 = Math.max(row,cell[0]);
            var c1 = Math.min(col,cell[1]);
            var c2 = Math.max(col,cell[1]);
            
            for ( var i = r1; i < r2+1; i++) {
                    for ( var j = c1; j < c2+1; j++) {                               
                        this.selectCell(i,j,true);
                        }
                }
    }

});
Ext.rr.grid.MulSelectCommonGrid = Ext.extend(Ext.rr.grid.DynamicGridPanel,{
        pageSize:20,
        storeCfg:{},
        dataUrl:null,        
        columns : [],
        forceFitColumn : true,
        initComponent:function(){
                Ext.applyIf(this.storeCfg,{
                        xtype:'groupingstore',
                        url:this.dataUrl || this.storeUrl||this.url,
                        autoDestroy: true,
                        groupDir : this.groupDir ? this.groupDir : "ASC",
                        reader : new Ext.data.JsonReaderEx(),
                        baseParams : this.baseParams ? this.baseParams : {}
                });
                var store = this.store || Ext.create(this.storeCfg);
                delete this.storeCfg;
                this.plugins.push(this.summaryPlugin||new Ext.ux.grid.GroupSummary());
                Ext.apply(this,{
                        store:store,
                        // sm: this.checkboxSelModel || new
                        // Ext.grid.RowSelectionModel({singleSelect:true}),
                        sm:new Ext.rr.grid.MultiCellSelectionModel(),
                        pageSize:this.pageSize || this.limit,
                        bbar:new Ext.PagingToolbar({
                        pageSize: this.pageSize || this.limit,
                        store: store,
                        displayInfo: true,
                        displayMsg : "第{0}条到{1}条,共{2}条",
                        emptyMsg:"没有记录",
                        plugins : new Ext.ux.PagingToolbar.PageCount(),
                        items:['-',{
                                text : '过滤配置',
                                menu : {
                                        items:[{
                                                text:'自动过滤:打开',
                                                enableToggle: true,
                                                handler:this.setAutoFilter,
                                                        scope:this
                                        },'-',{
                                                        text:'本地过滤:关闭' ,
                                                        enableToggle: true,
                                                        handler: this.setLocalFilter,
                                                        scope : this
                                                },'-',{
                                            text: '查看过滤条件',
                                            tooltip: '查看所有的过滤条件',
                                            handler: this.filterCondition,
                                            scope : this
                                        },'-',{
                                            text: '清除过滤条件',
                                            tooltip: '清除所有的过滤条件',
                                            handler: this.clearfilter,
                                            scope : this
                                        },'-',{
                                                text:'进行过滤',
                                                handler:this._doFilter,
                                                scope:this
                                        }]                                
                                }
                        },'-',{
                                text : '分组配置',
                                menu : {
                                        items:[{
                                                        text:'远程分组:关闭',
                                                        enableToggle: true,
                                                        handler:this.setRemoteGroup,
                                                        scope:this
                                                },'-',{
                                                        text:'分组方向:升序',
                                                        handler:this.setGroupDir,
                                                        scope:this
                                                },'-',{
                                                        text:'清除分组',
                                                        handler:this.clearGroup,
                                                        scope:this
                                                }]
                                }
                        },'-',{
                                        text:'远程排序:关闭',
                                        enableToggle: true,
                                        handler:this.setRemoteSort,
                                        scope:this
                                }]
                    }),
                        view: this.view || new Ext.grid.GroupingView({
                                hideGroupedColumn : true,
                    forceFit: this.forceFitColumn,
                    groupByText:"按这个字段分组",
                    showGroupsText:"通过分组显示"
                }),
                loadMask : {msg:"数据加载中..."}
                });
                Ext.rr.grid.MulSelectCommonGrid.superclass.initComponent.apply(this, arguments);
                this.on("headerclick", function(grid,columnIndex, e ) {
            grid.getSelectionModel().selectCol(columnIndex,e);               
            return false;
        });
                var isDown = false;
                var lastXY = [];
                var nearestCell = null;//最近的
                this.on("mousedown", function(e ) {
                        isDown = true;
                        lastXY = e.getXY();
        });
                this.on("mouseup", function(e ) {
                        isDown = false;
                        nearestCell = null;
                });
                this.on("mouseover", function(e ) {
                        if(isDown){
                                var col = 0;
                                var row = 0;
                                var view = this.getView();
                                
                                for (; col < this.getColumnModel().config.length; col++) {
                                        var div = view.getCell(0,col).firstChild;
                                        var x = div.getBoundingClientRect().right;
                                        if(x>e.getXY()[0]){
                                                break;
                                        }
                                }
                                for (; row < this.getStore().getCount(); row++) {
                                        var div = view.getCell(row,0).firstChild;
                                        var y = div.getBoundingClientRect().bottom;
                                        if(y>e.getXY()[1]){
                                                break;
                                        }
                                }
                                if(nearestCell==null){
                                        nearestCell = [this.getSelectionModel().selection.cell[0],this.getSelectionModel().selection.cell[1]];
                                }
                                if(nearestCell[0]!=row||nearestCell[1]!=col){
                                        if(e.ctrlKey){
                                                this.getSelectionModel().selectMatrix(nearestCell[0],nearestCell[1],true);
                                                this.getSelectionModel().selectMatrix(row,col,true);
                                        }else{
                                                this.getSelectionModel().selectMatrix(nearestCell[0],nearestCell[1],false);
                                                this.getSelectionModel().selectMatrix(row,col,false);
                                        }
                                        nearestCell = [row,col];
                                }
                                
                        }
                });
        },
        setRemoteGroup:function(button,state){
                var store = this.store;
                var sortLocal = store.remoteGroup = !store.remoteGroup;
                var text = '远程分组:' + (sortLocal ? '打开' : '关闭');
                button.setText(text);
                store.lastOptions.params["start"] = 0;
                store.applyGroupField();
                if(!sortLocal){
                        if(store.baseParams){
                delete store.baseParams.groupBy;
                delete store.baseParams.groupDir;
            }
            var lo = store.lastOptions;
            if(lo && lo.params){
                delete lo.params.groupBy;
                delete lo.params.groupDir;
            }
                }
                store.reload();
        },
        setGroupDir:function(button,state){
                var store = this.store;
                store.groupDir = store.groupDir == "ASC" ? "DESC" : "ASC";
                var text = '分组方向:' + (store.groupDir == "ASC" ? '升序' : '降序');
                button.setText(text);
                if(store.remoteGroup){
                        store.lastOptions.params.groupDir = store.groupDir;
                        store.reload();
                        store.sort();
                }else{
                        store.sort();
                        store.fireEvent('datachanged', store);
                }
        },
        clearGroup:function(){
                 this.store.clearGrouping();
        },
        setRemoteSort:function(button, state){
                var store = this.store;
                var sortLocal = store.remoteSort = !store.remoteSort;
                var text = '远程排序:' + (sortLocal ? '打开' : '关闭');
                button.setText(text);
                store.hasMultiSort = true;
                store.lastOptions.params["start"] = 0;
                store.reload();
        },
        setAutoFilter:function(button, state){
                var filterPlugin = this.filters;
                var autoFilter = filterPlugin.autoReload = !filterPlugin.autoReload;
                var text = '自动过滤:' + (autoFilter ? '打开' : '关闭');
                button.setText(text);
        },
        setLocalFilter:function(button,state){
                var filterPlugin = this.filters;
                var local = filterPlugin.local = !filterPlugin.local;
        var text = '本地过滤:' + (local ? '打开' : '关闭');
        filterPlugin.bindStore(this.getStore());
        button.setText(text);
        this.getStore().reload();
        },
        filterCondition:function(){
            var _filterData = this.filters.getFilterData();
            var comparisons = {'gt':'大于','lt':'小于','eq':'等于'};
            var cm = this.getColumnModel();
            var _s = "";
            var v ;
            Ext.each(_filterData,function(obj){
                    var cson = obj.data.comparison ? comparisons[obj.data.comparison] : '';
                    if(obj.data.type=='list'){
                            v = cson+obj.data.text;
                    }else{
                            v = cson+obj.data.value;
                    }
                    _s += cm.getColumnHeader(cm.findColumnIndex(obj.field))+":"+v+"<br/>"
            });
        Ext.Msg.alert('过滤条件',_s);
        },
        clearfilter:function(){
            var filterPlugin = this.filters;
        filterPlugin.clearFilters();
        if(!filterPlugin.autoReload)
                filterPlugin.deferredUpdate.delay(filterPlugin.updateBuffer);
        },
        _doFilter:function(){
                   var filterPlugin = this.filters;
                filterPlugin.deferredUpdate.delay(filterPlugin.updateBuffer);
        }
});

Ext.reg('mulselectcommongrid',Ext.rr.grid.MulSelectCommonGrid);

使用方法:将你项目中的xtype:grid改为xtype:mulselectcommongrid即可

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Hmily 发表于 2017-2-6 15:35
这个我看不懂,解说一下?
LzSkyline 发表于 2017-2-7 10:58 来自手机
楼主写的是基于extjs这个前端库开发的一款 支持多选行的前端表格组件,自写了一个控件模块

点评

质量如何?  详情 回复 发表于 2017-2-7 11:44
Hmily 发表于 2017-2-7 11:44
LzSkyline 发表于 2017-2-7 10:58
楼主写的是基于extjs这个前端库开发的一款 支持多选行的前端表格组件,自写了一个控件模块

质量如何?
LzSkyline 发表于 2017-2-7 16:04

我没有用过extjs, 不知道代码中那些是官网或社区的示例, 如果这些全是LZ写的话还是比较有水平的

点评

那就通过吧。  发表于 2017-2-7 16:12
Hmily 发表于 2017-2-7 16:14
ID:renwuqiangg
邮箱:renwuqiangg@126.com

申请通过,欢迎光临吾爱破解论坛,期待吾爱破解有你更加精彩,ID和密码自己通过邮件密码找回功能修改,请即时登陆并修改密码!
登陆后请在一周内在此帖报道,否则将删除ID信息。

ps:登录后请把文章整理发布到编程区吧。
renwuqiangg 发表于 2017-2-9 13:29
Hmily 发表于 2017-2-7 16:14
ID:renwuqiangg
邮箱:

报道!
这些代码的确是我自己写的,当时公司有这个需求,我找了好多地方都没有现成的,就自己写了个插件。
水平并不高,但我会努力的~
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-15 17:52

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表