Recently write program, happen to have dynamic add delete line, the following is recorded
HTML is not written, there is nothing, directly on the core of the
New Line
function AddRow (obj) {
//Get a total number of rows in the table, convenient to append to the ordinal number assigned
var length = $ ("#grid tr"). Length;
To get the current number of lines, so that when you append, add under the line, I have more levels across, define var current = $ (obj) based on the actual level. Parent (). Parent ().
parent (). Prevall (). length; alert (current);
Here the main definition needs to append content, of course, starting from TR, very simple, everyone will
var addcontent = "";
The core method, Gird is the table id,current is inserted after the first few lines, Addcontent is to append the content of the
addtr (' grid ', current,addcontent);
Here's the reordering, reordering the numbers from 1 to N, and it happens that the first TR in my code doesn't show up, so it's not + 1
$ ("#grid tr"). each (function (index) {
if (index!= 0) {
$ (this). Children (' TD '). EQ (0). text (index);
}
);
OK, let's look at the next Addtr method ~ ~ This is found on the Internet, or good use of
/**
* Adds a row to the table specified row
*
tab ID
* Row number, such as:0-> first line 1-> second row -2-> penultimate line -1-> last line
* trhtml Add line HTML code
*/
function addtr (tab, Row, trhtml) {
//Get Table last line $ ("#tab tr:last")
//Get first row of table $ ("#tab tr"). EQ (0)
//Get the penultimate row of table $ ("#tab tr"). EQ ( -2)
var $tr =$ ("#" +tab+ "tr"). EQ (row);
if ($tr. Size () ==0) {
alert ("The specified table ID or number of rows does not exist!) ");
return;
}
$tr. After (trhtml);
}
Delete Row, this is easier
function DeleteRow (obj) {
//Remove $ (obj). Parent (). Parent ().
parent (). remove ();
Reorder, if the deletion is not the last line, do not reorder can not see the ~
$ ("#grid tr"). each (function (index) {
if (index!= 0) {
$ (this). Children (' TD '). EQ (0). text (index);
}
);
Dynamic add Delete Row, just a few lines of code, convenient for everyone to use
PS: $ (this). Prevall (). length + 1;//column
The above is the small series for you to bring the jquery dynamic add a simple instance of deleting rows (recommended) All content, I hope that we support the cloud-Habitat Community ~