JS generate table

Source: Internet
Author: User

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 ..

 

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.