Today, I encountered a problem: how to generate a table through Js.
The solution is as follows:
// Create a new table
Function createtable (){
VaR table = Document. getelementbyid ('newtb ');
VaR newrow = table. insertrow ();
Newrow. ID = createid (); // create ID
VaR taskid = table. Rows. Length-1;
VaR cell2 = newrow. insertcell ();
Cell2.innerhtml = '<input type = "hidden" id = "HDF + newrow. ID +'" name = "HDF" value = "'+ newrow. ID +'">'
If a line feed is required, the code is clearer. The following method is used: splice the image with +.
+ '<Div style = "border: 1px solid #1862a1; margin-top: 20px">'
In innerhtml, you need to use ''to enclose the HTML content .. Then, the ID and name of each control are different. Therefore, a new ID must be created for each control.
}
// Create a unique ID
Function createid (){
Return math. Round (math. Random () * 1000000 );
}
2: if we are not satisfied with the generated table, we can delete it through the hidden domain in the first row.
The Code is as follows:
Function del (ID ){
VaR table = Document. getelementbyid ("'newtb '");
VaR flag = 0;
For (I = 0; I <= table. Rows. length; I ++ ){
If (document. getelementsbyname ("HDF") [I]. value = ID ){
Document. getelementbyid ('<% = deltable. clientid %> '). value = document. getelementbyid ('<% = deltable. clientid %> '). value + ID + '\,';
Table. deleterow (I );
Flag = I;
Break;
}
}
}
Create a hidden domain of deltable.
And then save...
A new table is added cyclically in the background through literal.
Use stringbuilder to add data ..