JQuery实现表格动态增加行并对新行添加事件


Posted in Javascript onJuly 30, 2014

实现功能:

通常在编辑表格时表格的行数是不确定的,如果一次增加太多行可能导致页面内容太多,反应变慢;通过此程序实现表格动态增加行,一直保持最下面有多个空白行。

效果:

一:原始页面

二:表1增加新行并绑定timepicker

三:表2自动增加行,新行绑定timepicker

HTML源码:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<title></title> 
<link href="../Script/jquery-easyui-1.3.2/themes/default/easyui.css" rel="external nofollow" rel="stylesheet" /> 
<style> 
.autoRows{ 
width: 350px; border:1px green solid; 
} 
.autoRows tbody tr td{ 
border-bottom:1px green solid; 
margin:0px; 
} 
.autoRows thead{ 
background-color:#8ec7d7; 
} 
.autoRows tfoot { 
background-color: #8ec7d7; 
} 
</style> 
</head> 
<body> 
<table border="0" cellspacing="0" id="table1" class="autoRows"> 
<thead> 
<tr> 
<th>表头1</th> 
<th>表头1</th> 
<th>表头1</th> 
</tr> 
<tr> 
<th>表头2</th> 
<th>表头2</th> 
<th>表头2</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
<td> 
<input id="Button1" type="button" value="insertAfter" onclick="addrow(this);" /></td> 
<td> 
<input id="Button3" type="button" value="Clear" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, false);" /></td> 
<td> 
<input id="Text2" type="text" value="aaaa" /></td> 
</tr> 
<tr> 
<td> 
<input id="Button2" type="button" value="insertBefore" onclick="$.fn.tableAutoRow.insertRow(this,1,true,false);" /></td> 
<td> 
<input id="Button4" type="button" value="Reset" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, true);" /></td> 
<td> 
<input id="Text1" name="ttt" type="text" value="asdfasfasfdsd" /></td> 
</tr> 
<tr> 
<td> 
<input id="Button5" type="button" value="insertBefore" onclick="$.fn.tableAutoRow.insertRow(this,1,true,false);" /></td> 
<td> 
<input id="Button6" type="button" value="Reset" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, true);" /></td> 
<td> 
<input id="Text3" type="text" name="Text3" /></td> 
</tr> 
</tbody> 
<tfoot> 
<tr> 
<th>表尾1</th> 
<th>表尾2</th> 
<th>表尾3</th> 
</tr> 
</tfoot> 
</table> 
<div style="height:20px;"></div> 
<table border="0" cellspacing="0" id="table2" class="autoRows"> 
<thead> 
<tr> 
<th>表头1</th> 
<th>表头1</th> 
<th>表头1</th> 
</tr> 
<tr> 
<th>表头2</th> 
<th>表头2</th> 
<th>表头2</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
<td> 
<input id="Button7" type="button" value="insertAfter" onclick="addrow(this);" /></td> 
<td> 
<input id="Button8" type="button" value="Clear" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, false);" /></td> 
<td> 
<input id="Text4" type="text" value="aaaa" /></td> 
</tr> 
<tr> 
<td> 
<input id="Button9" type="button" value="insertBefore" onclick="$.fn.tableAutoRow.insertRow(this, 1, true, false);" /></td> 
<td> 
<input id="Button10" type="button" value="Reset" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, true);" /></td> 
<td> 
<input id="Text5" name="ttt" type="text" value="asdfasfasfdsd" /></td> 
</tr> 
<tr> 
<td> 
<input id="Button11" type="button" value="insertBefore" onclick="$.fn.tableAutoRow.insertRow(this, 1, true, false);" /></td> 
<td> 
<input id="Button12" type="button" value="Reset" onclick="$.fn.tableAutoRow.clearRowData(this, 2, 2, true);" /></td> 
<td> 
<input id="Text6" type="text" name="Text3" /></td> 
</tr> 
</tbody> 
<tfoot> 
<tr> 
<th>表尾1</th> 
<th>表尾2</th> 
<th>表尾3</th> 
</tr> 
</tfoot> 
</table> 
</body> 
</html> 
<script src="../Script/jquery-1.7.2.min.js"></script> 
<script src="../Script/jquery.tableAutoRow.js"></script> 
<script src="../Script/jquery-easyui-1.3.2/jquery.easyui.min.js"></script> 
<script src="../Script/jquery.timepicker.js"></script> 
<script type="text/javascript"> 
$(function () { 
$(".autoRows").tableAutoRow(aaa); 
function aaa(row) { 
$(row).find(':text').timepicker(); 
} 
}); 
function addrow(obj) { 
$.fn.tableAutoRow.insertRow(obj); 
} 
</script>

JS源码:

/// <reference path="jquery-1.7.2.min.js" /> 
//为表格主体添加单击事件,当单击时添加行数,使表格保持有n个空行 
(function ($) { 
$.fn.extend({ 
rowfunction: null, 
tableAutoRow: function (newRowFunction) { 
rowfunction = newRowFunction; 
return $(this).each(function () { 
var tb = this; 
if (!(this.tagName.toUpperCase() == "TBODY")) { 
if (!this.tBodies[0]) { 
return; 
} else { 
tb = this.tBodies[0]; 
} 
} 

//添加一个隐藏行,后面新增行复制此行 
var lastRow = tb.rows[tb.rows.length - 1]; 
var row = $(lastRow).clone(true, true); 
$(row).insertAfter($(tb).find("tr:last")).hide(); 

//为除所有行添加事件,当获得焦点时自动增加新行 
for (var i = 0; i < tb.rows.length; i++) { 
AddAutoRowsEvent(tb.rows[i]); 
} 
}); 
} 
}); 
//自动增加行 
function autoRows(e) { 
var e = e || event; 
var obj = e.target || e.srcElement; 
while (obj.tagName != "TR") { 
obj = obj.parentNode; 
} 
var tb = obj.parentNode; 
var index = $(obj).index(); 
var n = 5 - (tb.rows.length - index); 
if (n > 0) { 
var lastRow = tb.rows[tb.rows.length - 1]; 
for (var j = 0; j < n; j++) { 
var row = $(lastRow).clone(true, true); 
//将新行添加到最后一行之前 
row.insertBefore($(tb).find("tr:last")).show(); 
//为新增加的行添加事件 
//AddAutoRowsEvent(tb.rows[tb.rows.length - 2]); 
//如果有回调函数则执行 
if (typeof (rowfunction) == 'function') { 
rowfunction(row); 
} 
} 
} 
} 

//为指定行增加事件 
function AddAutoRowsEvent(tr) { 
//如果是JQuery对象则转为HTML对象 
if (tr instanceof jQuery) { 
tr = tr[0]; 
} 

$(tr).bind('click', autoRows); 
var c = tr.cells.length; 
for (var j = 0; j < c; j++) { 
var childs = tr.cells[j].childNodes; 
for (var k = 0; k < childs.length; k++) { 
if (childs[k].type == "text" || childs[k].type == "textarea" || childs[k].type == "button") { 
$(childs[k]).bind('focus', autoRows); 
} 
} 
} 
} 

//在表格中指定位置插入指定行数,新插入的行内容为同一表格主体最后一行 
//obj:行内的任意对象 
//n:要增加的行数 
//bAutoRows:是否要添加自动增加行的属性 
$.fn.tableAutoRow.insertRow = function (obj, n, bAutoRows, isInsertAfter) { 
var loop = 0; //加入循环次数,防止死循环 
while (obj.tagName != "TR" && loop < 10) { 
obj = obj.parentNode; 
loop++; 
} 
if (obj.tagName != "TR") { 
return; 
} 
var tb = obj.parentNode; 
switch (arguments.length) { 
case 3: 
var isInsertAfter = true; 
case 2: 
var bAutoRows = true; 
var isInsertAfter = true; 
case 1: 
var bAutoRows = true; 
var isInsertAfter = true; 
var n = 1; 
} 
for (var i = 0; i < n; i++) { 
var lastRow = tb.rows[tb.rows.length - 1]; 

var row = $(lastRow).clone(true, true); 
//将新行添加到当前行之前/后 
if (isInsertAfter) { 
row.insertAfter(obj).show(); 
} else { 
row.insertBefore(obj).show(); 
} 
if (bAutoRows) { 
AddAutoRowsEvent(row); 
} 
} 
} 
//清除指定行数据 
//obj为行或者行内的节点 
//startColnum:起始列 
//endColumn:终止列 
//isReset:是否恢复到初始值 
$.fn.tableAutoRow.clearRowData = function (obj, startColnum, endColumn, isReset) { 
var loop = 0; //加入循环次数,防止死循环 
while (obj.tagName != "TR" && loop < 10) { 
obj = obj.parentNode; 
loop++; 
} 
if (obj.tagName != "TR") { 
return; 
} 
var cellsCount = obj.cells.length; //此行单元格总数 
if (startColnum < 0 || !startColnum) { //如果未指定清除起始列则从第一列清除 
startColnum = 0; 
} 
if (endColumn > cellsCount - 1 || !endColumn) { //如果未指定清除终止列则清除到最后一列前(通常最后一列用于放置清除按钮) 
endColumn = cellsCount - 1; 
} 
if (isReset == undefined) { 
isReset = false; 
} 
for (var c = startColnum; c <= endColumn; c++) //循环各列,设置单元格里的控件值 
{ 
for (var j = 0; j < obj.cells[c].childNodes.length; j++) { //循环处理指定单元格中的子节点 
var node = obj.cells[c].childNodes[j]; 
setObjData(node, isReset); 
} 
} 
}; 
function setObjData(node, isReset) { 
switch (node.type) { 
case "text": 
case "hidden": 
case "textarea": 
if (isReset) { 
node.value = node.defaultValue; 
} else { 
node.value = ""; 
} 
break; 

case "select-one": 
case "select-multiple": 
if (isReset) { 
for (var k = node.options.length - 1; k >= 0; k--) { 
node.options[k].selected = node.options[k].defaultSelected; 
} 
} else { 
for (var k = node.options.length - 1; k >= 0; k--) { 
//node.options.remove(k); 
node.options[k].selected = false; 
} 
} 
break; 
case "checkbox": 
case "radio": 
if (isReset) { 
node.checked = node.defaultChecked; 
} else { 
node.checked = false; 
} 
break; 
} 
if (node.childNodes && node.childNodes.length > 0) { 
var l = node.childNodes.length; 
for (var i = 0; i < l; i++) { 
setObjData(node.childNodes[i], isReset); 
} 
} 
} 
})(jQuery);
Javascript 相关文章推荐
JQUERY 对象与DOM对象之两者相互间的转换
Apr 27 Javascript
js 动态文字滚动的例子
Jan 17 Javascript
JavaScript对象之间的转换 jQuery对象和原声DOM
Mar 07 Javascript
jQuery中append()方法用法实例
Dec 25 Javascript
AngularJS表单编辑提交功能实例
Feb 13 Javascript
JavaScript职责链模式概述
Sep 17 Javascript
JavaScript使用正则表达式获取全部分组内容的方法示例
Jan 17 Javascript
jQuery插件HighCharts绘制简单2D柱状图效果示例【附demo源码】
Mar 21 jQuery
详谈jQuery中使用attr(), prop(), val()获取value的异同
Apr 25 jQuery
微信小程序上传图片实例
May 28 Javascript
jQuery的Ajax接收java返回数据方法
Aug 11 jQuery
vue 项目@change多个参数传值多个事件的操作
Jan 29 Vue.js
jQuery修改CSS伪元素属性的方法
Jul 30 #Javascript
教你在heroku云平台上部署Node.js应用
Jul 30 #Javascript
浅析Node在构建超媒体API中的作用
Jul 30 #Javascript
JS实现图片无间断滚动代码汇总
Jul 30 #Javascript
使用jquery.upload.js实现异步上传示例代码
Jul 29 #Javascript
js动态添加onclick事件可传参数与不传参数
Jul 29 #Javascript
使用focus方法让光标默认停留在INPUT框
Jul 29 #Javascript
You might like
php删除字符串末尾子字符,删除开始字符,删除两端字符(实现代码)
2013/06/27 PHP
thinkphp路由规则使用示例详解和伪静态功能实现(apache重写)
2014/02/24 PHP
通过dbi使用perl连接mysql数据库的方法
2014/04/16 PHP
PHP 面向对象程序设计(oop)学习笔记 (四) - 异常处理类Exception
2014/06/12 PHP
PHP获取数组长度或某个值出现次数的方法
2015/02/11 PHP
PHP数组的定义、初始化和数组元素的显示实现代码
2016/11/05 PHP
jquery实现类似淘宝星星评分功能有截图
2014/09/15 Javascript
Javascript this 关键字 详解
2014/10/22 Javascript
js查找节点的方法小结
2015/01/13 Javascript
详谈jQuery操纵DOM元素属性 attr()和removeAtrr()方法
2015/01/22 Javascript
Nodejs全局安装和本地安装的不同之处
2016/07/04 NodeJs
JQuery EasyUI学习教程之datagrid 添加、修改、删除操作
2016/07/09 Javascript
Vue 2.0中生命周期与钩子函数的一些理解
2017/05/09 Javascript
详解cordova打包成webapp的方法
2017/10/18 Javascript
JavaScript实现简单的文本逐字打印效果示例
2018/04/12 Javascript
加快Vue项目的开发速度的方法
2018/12/12 Javascript
vue路由教程之静态路由
2019/09/03 Javascript
jQuery 函数实例分析【函数声明、函数表达式、匿名函数等】
2020/05/19 jQuery
[02:02]DOTA2英雄基础教程 斯拉达
2013/12/11 DOTA
[40:10]2015国际邀请赛全明星表演赛
2015/08/07 DOTA
跟老齐学Python之正规地说一句话
2014/09/28 Python
python获取文件路径、文件名、后缀名的实例
2018/04/23 Python
django 自定义filter 判断if var in list的例子
2019/08/20 Python
django API 中接口的互相调用实例
2020/04/01 Python
python的dict判断key是否存在的方法
2020/12/09 Python
利用HTML5中Geolocation获取地理位置调用Google Map API在Google Map上定位
2013/01/23 HTML / CSS
荷兰皇家航空公司中国官网:KLM中国
2017/12/13 全球购物
英国索普公园票务和酒店套餐:Thorpe Breaks
2019/09/14 全球购物
学前教育求职自荐信范文
2013/12/25 职场文书
违反学校规定检讨书
2014/01/18 职场文书
模具毕业生推荐信
2014/02/15 职场文书
南京青奥会口号
2014/06/12 职场文书
机械制造专业大学生自我鉴定
2014/09/19 职场文书
格林童话读书笔记
2015/06/30 职场文书
python基础之函数的定义和调用
2021/10/24 Python
经典《舰娘》游改全新动画预告 预定11月开播
2022/04/01 日漫