当前位置: 编程技术>WEB前端
easyui datagrid 键盘上下控制选中行示例
来源: 互联网 发布时间:2014-08-25
本文导语: 扩展datagrid的一个方法keyCtr 代码如下: $.extend($.fn.datagrid.methods, { keyCtr : function (jq) { return jq.each(function () { var grid = $(this); grid.datagrid('getPanel').panel('panel').attr('tabindex', 1).bind('keydown', function (e) { switch (e.keyCode) { case 38: // up var select...
扩展datagrid的一个方法keyCtr
$.extend($.fn.datagrid.methods, {
keyCtr : function (jq) {
return jq.each(function () {
var grid = $(this);
grid.datagrid('getPanel').panel('panel').attr('tabindex', 1).bind('keydown', function (e) {
switch (e.keyCode) {
case 38: // up
var selected = grid.datagrid('getSelected');
if (selected) {
var index = grid.datagrid('getRowIndex', selected);
grid.datagrid('selectRow', index - 1);
} else {
var rows = grid.datagrid('getRows');
grid.datagrid('selectRow', rows.length - 1);
}
break;
case 40: // down
var selected = grid.datagrid('getSelected');
if (selected) {
var index = grid.datagrid('getRowIndex', selected);
grid.datagrid('selectRow', index + 1);
} else {
grid.datagrid('selectRow', 0);
}
break;
}
});
});
}
});
代码如下:
$.extend($.fn.datagrid.methods, {
keyCtr : function (jq) {
return jq.each(function () {
var grid = $(this);
grid.datagrid('getPanel').panel('panel').attr('tabindex', 1).bind('keydown', function (e) {
switch (e.keyCode) {
case 38: // up
var selected = grid.datagrid('getSelected');
if (selected) {
var index = grid.datagrid('getRowIndex', selected);
grid.datagrid('selectRow', index - 1);
} else {
var rows = grid.datagrid('getRows');
grid.datagrid('selectRow', rows.length - 1);
}
break;
case 40: // down
var selected = grid.datagrid('getSelected');
if (selected) {
var index = grid.datagrid('getRowIndex', selected);
grid.datagrid('selectRow', index + 1);
} else {
grid.datagrid('selectRow', 0);
}
break;
}
});
});
}
});