jQuery.Excel, 使用Ctrl+方向键/Home/End在input表格中移动
2009-02-23 18:17
465 查看
// Enable user to move focus in a grid of inputs with Ctrl + Up, Down, Left, Right, Home, End
// Could be called again after more rows are created.
// deerchao@gmail.com 2009-2-23
// usage:
//--------------Html----------------
// <tr class="dataRow">
// <td><input /></td>
// <td><input /></td>
// <td><input /></td>
// </tr>
// <tr class="dataRow">
// <td><input /></td>
// <td><input /></td>
// <td><input /></td>
// </tr>
//--------------Script-------------
// jQuery.excel('dataRow');
jQuery.extend({
excel: function(rowClass) {
var keys = { left: 37, up: 38, right: 39, down: 40, home: 36, end: 35 };
rowClass = rowClass ? rowClass : '.excel';
if (rowClass[0] != '.')
rowClass = '.' + rowClass;
$(rowClass).unbind('keyup',onkeyup).bind('keyup',onkeyup);
function onkeyup(evt) {
var ctrlOnly = evt.ctrlKey && !evt.altKey && !evt.shiftKey;
switch (evt.keyCode) {
case keys.down:
go("down");
break;
case keys.up:
go("up");
break;
case keys.left:
if (ctrlOnly)
go("left");
break;
case keys.right:
if (ctrlOnly)
go("right");
break;
case keys.home:
if (ctrlOnly)
go("home");
break;
case keys.end:
if (ctrlOnly)
go("end");
break;
}
function go(to) {
var td = $(evt.target).closest('td');
var tr = $(evt.currentTarget);
var toFocus = null;
switch (to) {
case 'home':
toFocus = lastInput(td.prevAll('td'));
break;
case 'end':
toFocus = lastInput(td.nextAll('td'));
break;
case 'left':
toFocus = firstInput(td.prevAll('td'));
if (!toFocus)
toFocus = lastInput(tr.prev('tr' + rowClass).children('td'));
break;
case 'right':
toFocus = firstInput(td.nextAll('td'));
if (!toFocus)
toFocus = firstInput(tr.next('tr' + rowClass).children('td'));
break;
case 'up':
toFocus = firstInput(tr.prev('tr' + rowClass).children('td'), td.prevAll('td').size());
break;
case 'down':
toFocus = firstInput(tr.next('tr' + rowClass).children('td'), td.prevAll('td').size());
break;
}
if (toFocus) {
toFocus.focus();
}
}
function firstInput(tds, start) {
if (!start)
start = 0;
for (var i = start; i < tds.size(); i++) {
var inputs = $(tds[i]).children('input, select, textarea');
if (inputs.size())
return inputs[0];
}
return null;
}
function lastInput(tds) {
for (var i = tds.size() - 1; i >= 0; i--) {
var inputs = $(tds[i]).children('input, select, textarea');
if (inputs.size())
return inputs[0];
}
return null;
}
}
}
});
// Could be called again after more rows are created.
// deerchao@gmail.com 2009-2-23
// usage:
//--------------Html----------------
// <tr class="dataRow">
// <td><input /></td>
// <td><input /></td>
// <td><input /></td>
// </tr>
// <tr class="dataRow">
// <td><input /></td>
// <td><input /></td>
// <td><input /></td>
// </tr>
//--------------Script-------------
// jQuery.excel('dataRow');
jQuery.extend({
excel: function(rowClass) {
var keys = { left: 37, up: 38, right: 39, down: 40, home: 36, end: 35 };
rowClass = rowClass ? rowClass : '.excel';
if (rowClass[0] != '.')
rowClass = '.' + rowClass;
$(rowClass).unbind('keyup',onkeyup).bind('keyup',onkeyup);
function onkeyup(evt) {
var ctrlOnly = evt.ctrlKey && !evt.altKey && !evt.shiftKey;
switch (evt.keyCode) {
case keys.down:
go("down");
break;
case keys.up:
go("up");
break;
case keys.left:
if (ctrlOnly)
go("left");
break;
case keys.right:
if (ctrlOnly)
go("right");
break;
case keys.home:
if (ctrlOnly)
go("home");
break;
case keys.end:
if (ctrlOnly)
go("end");
break;
}
function go(to) {
var td = $(evt.target).closest('td');
var tr = $(evt.currentTarget);
var toFocus = null;
switch (to) {
case 'home':
toFocus = lastInput(td.prevAll('td'));
break;
case 'end':
toFocus = lastInput(td.nextAll('td'));
break;
case 'left':
toFocus = firstInput(td.prevAll('td'));
if (!toFocus)
toFocus = lastInput(tr.prev('tr' + rowClass).children('td'));
break;
case 'right':
toFocus = firstInput(td.nextAll('td'));
if (!toFocus)
toFocus = firstInput(tr.next('tr' + rowClass).children('td'));
break;
case 'up':
toFocus = firstInput(tr.prev('tr' + rowClass).children('td'), td.prevAll('td').size());
break;
case 'down':
toFocus = firstInput(tr.next('tr' + rowClass).children('td'), td.prevAll('td').size());
break;
}
if (toFocus) {
toFocus.focus();
}
}
function firstInput(tds, start) {
if (!start)
start = 0;
for (var i = start; i < tds.size(); i++) {
var inputs = $(tds[i]).children('input, select, textarea');
if (inputs.size())
return inputs[0];
}
return null;
}
function lastInput(tds) {
for (var i = tds.size() - 1; i >= 0; i--) {
var inputs = $(tds[i]).children('input, select, textarea');
if (inputs.size())
return inputs[0];
}
return null;
}
}
}
});
相关文章推荐
- 使用JQuery 对表格数据上下移动
- 使用jquery插件 jquery.table2excel.js 将页面中的表格下载到本地(excel格式)
- jQuery操作表格背景色迭代和鼠标移动事件(CSS中使用expression)
- 在EasyJWeb中使用Java Excel API 处理电子表格
- poi读取Excel时日期为数字 的解决方法 转载 2017年12月14日 10:31:14 标签:poi /excel /java 19 在使用poi解析Excel文件时,会发现,表格中的日期解析出
- jQuery表格插件ParamQuery简单使用方法示例
- 使用jquery、ajax不刷新页面打印表格(返回数据类型text、json);
- 使用 jquery.inputlimiter 实现字数限制功能
- 使用JXL读取Excel表格,拷贝、更新Excel工作薄
- 使用jQuery操纵表格-增加、删除行和列
- jquery表格可编辑修改表格里面的值,点击td变input无刷新更新表格
- 使用jQuery快速解决input中placeholder值在ie中无法支持的问题
- 使用jquery和json动态添加表格
- 使用jQuery实现input数值增量和减量的方法
- secureCRT配置——针对Home、End无法使用
- jQuery表格排序组件-tablesorter使用示例
- Excel设置表格样式的显示效果使用excel如何改变表格样式
- jquery 实现表格行的上下移动和置顶
- 使用JQuery无刷新页面上传表单文件(Excel等)
- 在ASP.NET Web Forms中使用页面导出伪xls Excel表格