Using the createelement function to dynamically create a table

Source: Internet
Author: User

When we use Ajax, the most important thing is the dynamic generation of pages in the success function. The current scenario is to use ajax to dynamically generate tables or other list formats for query output. I checked some information on the Internet. Most examples of dynamic table generation are implemented by spelling HTML text. This disadvantage is that JS performance is not high if the data volume is large. After referring to some materials, I decided to use dynamic HTML elements for display. The core function is document. createelement (string tagname) to generate elements such as table, thead, tbody, TR, th, and TD. The Code is as follows:

<HTML>
<Head>
<Title> Create Table </title>
</Head>
<Body>
<Div id = "Container"> </div>
<SCRIPT type = "text/JavaScript">
VaR con = Document. getelementbyid ("Container ");
VaR table = Document. createelement ("table ");
VaR thead = table. createthead ();
VaR tbody = table. createtbody ();
VaR TR = Document. createelement ("TR ");
For (VAR I = 0; I <5; I ++ ){
Th = Document. createelement ("th ");
Th. innerhtml = "title" + I;
Tr. appendchild (th );
}
Thead. appendchild (TR );
For (VAR I = 0; I <10; I ++ ){
VaR TR = Document. createelement ("TR ");
For (var j = 0; j <5; j ++ ){
TD = Document. createelement ("TD ");
TD. innerhtml = I + "," + J;
Tr. appendchild (TD );
}
Tbody. appendchild (TR );
}
Con. appendchild (table );
</SCRIPT>
</Body>
</Html>

If you want to improve the performance of your applications, you can also point it out or use more efficient methods.

Using the createelement function to dynamically create a table

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.