How JS controls the dynamic generation of any row/column number table on a webpage
This article mainly introduces how javascript controls the dynamic generation of any row and column number tables on a webpage. The example shows how to control dom element addition by using javascript to operate table nodes, which has some reference value, for more information, see
This example describes how to dynamically generate a table of any number of rows and columns on a javascript control webpage. Share it with you for your reference. The specific analysis is as follows:
This is a very simple JS Code for generating tables online.
Using the JS function code, you can directly enter the number of rows and columns to automatically generate the table you need
Of course, you can also extend the JS Code to implement various forms of text generation.
The Code is as follows:
<Html>
<Head>
<Title> Js dynamic Table Generation </title>
<Style type = "text/css">
Table {font-size: 14px ;}
</Style>
</Head>
<Body>
<Script language = "javascript">
Function tableclick (name1, name2, name3 ){
Trow = name1.value;
Tcol = name2.value;
TV = name3.value;
If (Trow = "") | (Tcol = "") | (TV = "")){
Alert ("complete the conditions for table creation ");
}
Else {
R = parseInt (Trow );
C = parseInt (Tcol );
Table1 (r, c, TV );
}
}
Function tablevalue (a, ai, rows, col, str ){
Int1 = a. length;
For (I = 0; I <rows; ++ I ){
For (j = 0; j <col; ++ j ){
If (j = 0) & (ai> = int1) {break ;}
If (ai> = int1 ){
Str = str + "<td scope = 'col'> </td> ";
}
Else {
If (j = 0 ){
Str = str + "<tr> <th scope = 'col'>" + (a [ai ++]) + "</th> ";
}
Else {
If (j = col-1 ){
Str = str + "<td scope = 'col'>" + (a [ai ++]) + "</td> ";
}
Else {
Str = str + "<td scope = 'col'>" + (a [ai ++]) + "</td> ";
}
}
}
}
Str = str + "</tr> ";
}
Return str;
}
Function Table1 (row, col, Str1 ){
Var str = "";
A = new Array ();
S = new String (Str1 );
A = s. split ("#");
Int1 = a. length;
Ai = 0;
If (col <= int1 ){
Str = str + "<table width = '000000' border = '4'> ";
For (I = 0; I <col; ++ I ){
If (I = 0 ){
Str = str + "<tr> <th scope = 'col'>" + (a [ai ++]) + "</th> ";
}
Else {
If (I = (col-1 )){
Str = str + "<th scope = 'col'>" + (a [ai ++]) + "</th> </tr> ";
}
Else {
Str = str + "<th scope = 'col'>" + (a [ai ++]) + "</th> ";
}
}
}
If (int1> col ){
If (row> 1 ){
Str = tablevalue (a, ai, row-1, col, str );
}
}
Str = str + "</table> ";
Aa. innerHTML = str;
}
}
</Script>
<Form name = "form1" method = "post" action = "">
<P> <B> Number of rows: </B>
<Input name = "name1" type = "text" style = "width: 40px" value = "4">
<B> Number of columns: </B>
<Input name = "name2" type = "text" style = "width: 40px" value = "4">
<Input type = "button" name = "Submit3" value = "generate table" onClick = "tableclick (document. form1.name1, document. form1.name2, document. form1.name3) "> </p>
<P> <B align = "top"> Table value: </B> </p>
<P>
<Input name = "name3" wrap = "VIRTUAL" style = "width: 520px "value =" COL1 # COL2 # COL3 # COL4 # ROW1 # A1 # A2 # a3 # ROW2 # B1 # B2 # B3 # ROW3 # C1 # C2 # C3 ">
</P>
</Form>
<Div id = "aa"> </div>
</Body>
</Html>
I hope this article will help you design javascript programs.