首页 > 移动平台 > 详细

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

时间:2014-07-22 23:10:44      阅读:839      评论:0      收藏:0      [点我收藏+]

将以下脚本保存为 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啦!

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

原文:http://www.cnblogs.com/hxling/p/easyui_datagird_moverow.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!