JQuery implements the dynamic addition of rows to tables and the addition of events to new rows

  • 2020-03-30 03:37:39
  • OfStack

Implementation functions:

In general, when editing a table, the number of rows in the table is uncertain. If too many rows are added at one time, the page content will be too large and the response will be slow. Through this procedure to achieve the dynamic table to increase the row, always keep the bottom of a number of blank rows.

Effect:

One: the original page

Table 1 adds a new row and binds a timepicker

Three: table 2 automatically adds rows, new rows bind timepicker

HTML source code:


<!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> header 1</th> 
<th> header 1</th> 
<th> header 1</th> 
</tr> 
<tr> 
<th> header 2</th> 
<th> header 2</th> 
<th> header 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> footer 1</th> 
<th> footer 2</th> 
<th> footer 3</th> 
</tr> 
</tfoot> 
</table> 
<div style="height:20px;"></div> 
<table border="0" cellspacing="0" id="table2" class="autoRows"> 
<thead> 
<tr> 
<th> header 1</th> 
<th> header 1</th> 
<th> header 1</th> 
</tr> 
<tr> 
<th> header 2</th> 
<th> header 2</th> 
<th> header 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> footer 1</th> 
<th> footer 2</th> 
<th> footer 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 source code:


/// <reference path="jquery-1.7.2.min.js" /> 
//Add a click event to the body of the table, and the number of rows when clicked, leaving the table with n empty rows
(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]; 
} 
} 

//Add a hidden row followed by a new row to copy the row
var lastRow = tb.rows[tb.rows.length - 1]; 
var row = $(lastRow).clone(true, true); 
$(row).insertAfter($(tb).find("tr:last")).hide(); 

//To add an event to all rows, automatically add a new row when you get focus
for (var i = 0; i < tb.rows.length; i++) { 
AddAutoRowsEvent(tb.rows[i]); 
} 
}); 
} 
}); 
//Auto increment
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); 
//Adds a new line before the last line
row.insertBefore($(tb).find("tr:last")).show(); 
//Add an event for the newly added row
//AddAutoRowsEvent(tb.rows[tb.rows.length - 2]); 
//Execute if there is a callback function
if (typeof (rowfunction) == 'function') { 
rowfunction(row); 
} 
} 
} 
} 

//Adds an event to the specified row
function AddAutoRowsEvent(tr) { 
//If it's a JQuery object, it's an HTML object
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); 
} 
} 
} 
} 

//Inserts the specified number of rows at the specified location in the table. The contents of the newly inserted row are the last row in the body of the same table
//Obj: any object in a row
//N: the number of rows to increase
//BAutoRows: whether to add an auto-increment row property
$.fn.tableAutoRow.insertRow = function (obj, n, bAutoRows, isInsertAfter) { 
var loop = 0; //Add loop times to prevent dead loops
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); 
//Adds a new line before/after the current line
if (isInsertAfter) { 
row.insertAfter(obj).show(); 
} else { 
row.insertBefore(obj).show(); 
} 
if (bAutoRows) { 
AddAutoRowsEvent(row); 
} 
} 
} 
//Clears the specified row
//Obj is a node in a row or within a row
//StartColnum: start column
//EndColumn: end columns
//IsReset: whether to restore to the original value
$.fn.tableAutoRow.clearRowData = function (obj, startColnum, endColumn, isReset) { 
var loop = 0; //Add loop times to prevent dead loops
while (obj.tagName != "TR" && loop < 10) { 
obj = obj.parentNode; 
loop++; 
} 
if (obj.tagName != "TR") { 
return; 
} 
var cellsCount = obj.cells.length; //The total number of cells in this row
if (startColnum < 0 || !startColnum) { //Clear from the first column if the clear start column is not specified
startColnum = 0; 
} 
if (endColumn > cellsCount - 1 || !endColumn) { //If no clear stop column is specified, clear before the last column (usually the last column is used to place the clear button)
endColumn = cellsCount - 1; 
} 
if (isReset == undefined) { 
isReset = false; 
} 
for (var c = startColnum; c <= endColumn; c++) //Loop through each column, setting the value of the control in the cell
{ 
for (var j = 0; j < obj.cells[c].childNodes.length; j++) { //The loop processes the child nodes in the specified cell
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);

Related articles: