JS Operation table sample (personal experience) _javascript tips

Source: Internet
Author: User
Remember the interview when you have encountered such a problem: there is a table, and then there are 4 input boxes, a merge button, the input box is this, from the first few lines to the first few lines, from the first column to the first few columns, and then click on the button to merge. At that time I came out from school, JS knowledge just know some, simply do not! Now think about it, in fact, this problem is still the basic Kung fu is solid! Everyone is interested in doing their own things to see if they can do it. Screenshot of topic:

Now it seems easy to do this, but I still spend a lot of time, maybe I'm thinking wrong? The main thing is to use JS to manipulate HTML, I now implemented the add row, delete rows, add columns, delete columns, but the merged cells can not be fully implemented, mainly the table will be out of chaos. Now put this issue out, interested colleagues can own in time to study under, see oneself can not make! The main problem is merging cells! You can also take a look at the problem with merging cells.

Some of the code I implemented myself:
The HTML section writes
Copy Code code as follows:

<body onload= "Init ();" >

<table id= "table" align= "Center" >
<tbody id= "Newbody" ></tbody>

</table>
<div>
<table width= "800px" border= "1px" align= "Center" >
&LT;TR&GT;&LT;TD align= "center" ><input type= "button" id= "AddRow" name= "AddRow" onclick= "AddRow ();" value= "Add Line" /></td>
&LT;TD align= "center" ><input type= "button" id= "Delrow" name= "Delrow" onclick= "Removerow ();" value= "Delete rows"/> </td>
</tr>
&LT;TR&GT;&LT;TD align= "Center" ><input type= button "id=" Delcell "name=" Delcell "onclick=" Removecell (); "Value = "Delete Column"/></td>
&LT;TD align= "center" ><input type= "button" id= "Addcell" name= "Addcell" onclick= "Addcell ();" Value= "Add column"/> </td>
</tr>
&LT;TR&GT;&LT;TD align= "center" colspan= "2" ><input type= "button" id= "Addrows" name= "Addrows" onclick= "AddRow_" Withinsert (); "value= Add Rows"/></td></tr>
</table>
</div>
<div>
<table width= "800px" border= "1px" align= "Center" >
<tr><td> from <input type= "text" id= "Beginrow" name= "Beginrow" value= ""/> Line to <input type= "text" name= "Endrow" id= "Endrow" value= "/> Row </td><td rowspan=" 2 "id=" test "><input type=" button "Name=" Hebing " Id= "hebing" value= "combined" onclick= "rebulid ();" /> </td></tr>
<tr><td> from <input type= "text" name= "Begincol" id= "Begincol" value= ""/> column to <input type= "text" name= "Endcol" id= "Endcol" value= "/> Column </td></tr>
</table>
</div>
</body>

Generate tables, using AppendChild wrote
Copy Code code as follows:

function init () {
_table=document.getelementbyid ("table");
_table.border= "1px";
_table.width= "800px";

for (Var i=1;i<10;i++) {
var row=document.createelement ("tr");
Row.id=i;
for (Var j=1;j<6;j++) {
var cell=document.createelement ("TD");
Cell.id =i+ "/" +J;
Cell.appendchild (document.createTextNode ("+cell.id+"));
Row.appendchild (cell);
}
document.getElementById ("Newbody"). appendchild (row);
}
}

Add rows, write using the AppendChild method
Copy Code code as follows:

function AddRow () {
var Length=document.getelementbyid ("table"). Rows.length;
/*document.getelementbyid ("Newbody"). InsertRow (length);
document.getElementById (length+1). setattribute ("id", length+2);
var tr=document.createelement ("tr");
tr.id=length+1;
var td=document.createelement ("TD");
for (i=1;i<4;i++) {
Td.id=tr.id+ "/" +i;
Td.appendchild (document.createTextNode ("+td.id+"));
Tr.appendchild (TD);

}
document.getElementById ("Newbody"). appendchild (TR);
}

Another way to add a row InsertRow wrote
Copy Code code as follows:

function Addrow_withinsert () {
Varrow=document.getelementbyid ("table"). InsertRow (document.getElementById ("table"). Rows.length);
VAR rowcount =document.getelementbyid ("table"). Rows.length;

var Countcell=document.getelementbyid ("table"). Rows.item (0). Cells.length;
for (Var i=0;i<countcell;i++) {
var Cell=row.insertcell (i);

Cell.innerhtml= "new" + (ROWCOUNT) + "/" + (i+1) + "column";
Cell.id= (ROWCOUNT) + "/" + (i+1);

}
}

Delete Row, written by deleterow (Row Index)
Copy Code code as follows:

/* Delete line, using DeleteRow (Row Index) * *
function Removerow () {
/* var Row=document.getelementbyid ("2");
var Index=row.rowindex;
alert (index);
document.getElementById ("Newbody"). DeleteRow (document.getElementById document.getElementById ("table"). rows.length). RowIndex);
}

Add columns, using the Insertcell (column position) method to write
Copy Code code as follows:

function Addcell () {
/*document.getelementbyid ("table"). Rows.item (0). cells.length
The number of columns used to get the table
*/
For (Var I=0;i<document.getelementbyid ("table"). rows.length;i++) {
var Cell=document.getelementbyid ("table"). Rows[i].insertcell (2);
Cell.innerhtml= "The first" + (i+1) + "/" +3+ "column";

}
}

Delete columns, using the Deletecell (column position) method to write
Copy Code code as follows:

/* Delete column, using Deletecell (column position) method * *
function Removecell () {
For (Var I=0;i<document.getelementbyid ("table"). rows.length;i++) {
document.getElementById ("table"). Rows[i].deletecell (0);
}
}

Merged cells (not implemented) wrote
My code is a problem, mainly the table will be messed up, has not been modified:
Copy Code code as follows:

function Rebulid () {
var Beginrow=document.getelementbyid ("Beginrow") . value;/* start line */
var Endrow=document.getelementbyid ("Endrow"). value;/* End Line/

var begincol= document.getElementById ("Begincol"). value;/* Start column */
var Endcol=document.getelementbyid ("Endcol"). value;/* End Column * *

var tempcol=beginrow+ "/" +begincol;/* Locate the column/
Alert (tempcol) for which you want to change the property;
var Td=document.getelementbyid (tempcol);    

/* Delete cells to be merged */
for (Var x=beginrow;x<=endrow;x++) {
for (Var i=begincol;i<=endcol;i++) {
if (x==beginrow) {

document.getElementById ("table"). Rows[x].deletecell (i+1);

}
else{

document.getElementById ("table"). Rows[x].deletecell (i);

}

}
}
td.rowspan= (Endrow-beginrow) +1;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.