Create HTML table using Javascript

后端 未结 3 1072
陌清茗
陌清茗 2020-12-13 19:43

My question will ultimately be related to this site:

http://dbtest.net16.net/ethanol-01.html

EDIT: View unencrypted page source code here >>> http://dbtest.n

相关标签:
3条回答
  • 2020-12-13 20:02
    In the html file there are three input boxes with userid,username,department respectively.
    

    These inputboxes are used to get the input from the user.

    The user can add any number of inputs to the page.

    When clicking the button the script will enable the debugger mode.

    In javascript, to enable the debugger mode, we have to add the following tag in the javascript.

    /************************************************************************\

        Tools->Internet Options-->Advanced-->uncheck
        Disable script debugging(Internet Explorer)
        Disable script debugging(Other)
    
        <html xmlns="http://www.w3.org/1999/xhtml" >
    
        <head runat="server">
    
        <title>Dynamic Table</title>
    
        <script language="javascript" type="text/javascript">
    
        // <!CDATA[
    
        function CmdAdd_onclick() {
    
        var newTable,startTag,endTag;
    
    
    
        //Creating a new table
    
        startTag="<TABLE id='mainTable'><TBODY><TR><TD style=\"WIDTH: 120px\">User ID</TD>
        <TD style=\"WIDTH: 120px\">User Name</TD><TD style=\"WIDTH: 120px\">Department</TD></TR>"
    
        endTag="</TBODY></TABLE>"
    
        newTable=startTag;
    
        var trContents;
    
        //Get the row contents
    
        trContents=document.body.getElementsByTagName('TR');
    
        if(trContents.length>1)
    
        {
    
        for(i=1;i<trContents.length;i++)
    
        {
    
        if(trContents(i).innerHTML)
    
        {
    
        // Add previous rows
    
        newTable+="<TR>";
    
        newTable+=trContents(i).innerHTML;
    
        newTable+="</TR>";
    
        } 
    
        }
    
        }
    
        //Add the Latest row
    
        newTable+="<TR><TD style=\"WIDTH: 120px\" >" +
            document.getElementById('userid').value +"</TD>";
    
        newTable+="<TD style=\"WIDTH: 120px\" >" +
            document.getElementById('username').value +"</TD>";
    
        newTable+="<TD style=\"WIDTH: 120px\" >" +
            document.getElementById('department').value +"</TD><TR>";
    
        newTable+=endTag;
    
        //Update the Previous Table With New Table.
    
        document.getElementById('tableDiv').innerHTML=newTable;
    
        }
    
        // ]]>
    
        </script>
    
        </head>
    
        <body>
    
        <form id="form1" runat="server">
    
        <div>
    
        <br />
    
        <label>UserID</label> 
    
        <input id="userid" type="text" /><br />
    
        <label>UserName</label> 
    
        <input id="username" type="text" /><br />
    
        <label>Department</label> 
    
        <input id="department" type="text" />
    
        <center>
    
        <input id="CmdAdd" type="button" value="Add" onclick="return CmdAdd_onclick()" />
        </center>
    
    
    
        </div>
    
        <div id="tableDiv" style="text-align:center" >
    
        <table id="mainTable">
    
        <tr style="width:120px " >
    
        <td >User ID</td>
    
        <td>User Name</td>
    
        <td>Department</td>
    
        </tr>
    
        </table>
    
        </div>
    
        </form>
    
        </body>
    
        </html>
    
    0 讨论(0)
  • 2020-12-13 20:14

    The problem is that if you try to write a <table> or a <tr> or <td> tag using JS every time you insert a new tag the browser will try to close it as it will think that there is an error on the code.

    Instead of writing your table line by line, concatenate your table into a variable and insert it once created:

    <script language="javascript" type="text/javascript">
    <!--
    
    var myArray    = new Array();
        myArray[0] = 1;
        myArray[1] = 2.218;
        myArray[2] = 33;
        myArray[3] = 114.94;
        myArray[4] = 5;
        myArray[5] = 33;
        myArray[6] = 114.980;
        myArray[7] = 5;
    
        var myTable= "<table><tr><td style='width: 100px; color: red;'>Col Head 1</td>";
        myTable+= "<td style='width: 100px; color: red; text-align: right;'>Col Head 2</td>";
        myTable+="<td style='width: 100px; color: red; text-align: right;'>Col Head 3</td></tr>";
    
        myTable+="<tr><td style='width: 100px;                   '>---------------</td>";
        myTable+="<td     style='width: 100px; text-align: right;'>---------------</td>";
        myTable+="<td     style='width: 100px; text-align: right;'>---------------</td></tr>";
    
      for (var i=0; i<8; i++) {
        myTable+="<tr><td style='width: 100px;'>Number " + i + " is:</td>";
        myArray[i] = myArray[i].toFixed(3);
        myTable+="<td style='width: 100px; text-align: right;'>" + myArray[i] + "</td>";
        myTable+="<td style='width: 100px; text-align: right;'>" + myArray[i] + "</td></tr>";
      }  
       myTable+="</table>";
    
     document.write( myTable);
    
    //-->
    </script> 
    

    If your code is in an external JS file, in HTML create an element with an ID where you want your table to appear:

    <div id="tablePrint"> </div>
    

    And in JS instead of document.write(myTable) use the following code:

    document.getElementById('tablePrint').innerHTML = myTable;
    
    0 讨论(0)
  • 2020-12-13 20:21

    This beautiful code here creates a table with each td having array values. Not my code, but it helped me!

    var rows = 6, cols = 7;
    
    for(var i = 0; i < rows; i++) {
      $('table').append('<tr></tr>');
      for(var j = 0; j < cols; j++) {
        $('table').find('tr').eq(i).append('<td></td>');
        $('table').find('tr').eq(i).find('td').eq(j).attr('data-row', i).attr('data-col', j);
      }
    }
    
    0 讨论(0)
提交回复
热议问题