为easyui datagrid 添加上下方向键移动

将以下脚本保存为 easyui-datagrid-moverow.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var DatagridMoveRow = (function($){
 
    function DatagridMoveRow(gridTarget){
        this.el = gridTarget;
        this.$el = $(this.el);
        this.rowIndex = -1;
        this.rowsCount = this.$el.datagrid(‘getData‘).rows.length;
        return this;
    }
 
    DatagridMoveRow.prototype = {
        getRowindex: function(){
            var selectRowIndex = this.$el.datagrid(‘getSelectedIndex‘);
            if(selectRowIndex == -1){
                this.rowIndex = 0 ;
            }else{
                this.rowIndex = selectRowIndex;
            }
        },
        moveUp: function(){
 
            this.getRowindex();
 
            if(this.rowIndex ==0){
                return false;
            }
 
            var i = --this.rowIndex;
            if(i>-1){
                this.$el.datagrid(‘selectRow‘,i);
            }else{
                this.rowIndex = 0;
            }
 
            return false;
        },
        moveDown: function (){
            this.getRowindex();
 
            if(this.rowIndex == this.rowsCount -1 ){
                return false;
            }
            var i = ++this.rowIndex;
            this.$el.datagrid(‘selectRow‘,i);
        }
    }
 
    return DatagridMoveRow;
 
})(jQuery);

定义调用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var moveRow = function(target){
        var options = $(target).datagrid(‘options‘);
 
        if(options.moveRow){
            var dmr = new DatagridMoveRow(target);
            $(document).on(‘keydown.datagridrow‘,function(e){
                if(e.keyCode == 38){ //up
                    dmr.moveUp();
                }else if(e.keyCode == 40) {// down
                    dmr.moveDown();
                }
            });
        }
    }

  

  

在初始化datagrid 的 onLoadSuccess 事件中

1
2
3
4
onLoadSuccess:function(){
   // 上下方向键移动
   moveRow(this);      
}

 

这样就OK啦!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。