使用 Javascript 动态创建 html 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14554376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:10:59  来源:igfitidea点击:

Dynamically creating an html table with Javascript

javascripthtml-table

提问by Nick

I am trying to create a table based on user input (actually two or three tables depending on the user input..) using Javascript, I am very much native to PHP and have already got this working in PHP, however i would like the user to be able to see the table before the query it. I found a script on here that partially did what I wanted and have attempted to edit it (I found it surprisingly similar to PHP) Basically it calculates the total amount of cells (ports) splits it by rows and columns, the "super" column is used if the user would like it to be split into multiple tables, which align next to each other, hence the div tag. Here's my JS:

我正在尝试使用 Javascript 根据用户输入创建一个表(实际上是两个或三个表,具体取决于用户输入..),我非常熟悉 PHP,并且已经在 PHP 中使用了它,但是我希望用户能够在查询之前看到表。我在这里找到了一个脚本,它部分完成了我想要的操作并试图对其进行编辑(我发现它与 PHP 惊人地相似)基本上它计算单元格(端口)的总量,将它按行和列拆分,即“超级”列如果用户希望将其拆分为多个表,这些表彼此相邻对齐,因此使用 div 标签。这是我的JS:

<html>
<head>
<script type="text/javascript">
function createTable()
{
var num_ports = document.getElementById('ports').value;
var num_super = document.getElementById('super').value;
var num_rows = document.getElementById('rows').value;
var num_cols = document.getElementById('cols').value;
var tbody = '';
var colStart = num_cols / num_super;
for( var i=0; i<num_super; i++){
    var theader = '<div><table border="1">\n';
        for(u=1; u<=num_row; u++){
          tbody += '<tr>';
            for( var j=0; j<colStart; j++)
            {
            tbody += '<td>';
            tbody += 'Cell ' + i + ',' + j;
            tbody += '</td>'
            }
    tbody += '</tr>\n';
}
var tfooter = '</table></div>';
document.getElementById('wrapper').innerHTML = theader + tbody + tfooter;
}
</script>
</head>

<body>
<form name="tablegen">
<label>Ports: <input type="text" name="ports" id="ports"/></label><br />
<label>Super Columns: <input type="text" name="super" id="super"/></label><br />
<label>Rows: <input type="text" name="rows" id="rows"/></label><br />
<label>Columns: <input type="text" name="cols" id="cols"/></label><br/>
<input name="generate" type="button" value="Create Table!" onclick='createTable();'/>
</form>

<div id="wrapper"></div>
</body>
</html>

Here is what the final output looks like after it has been processed by PHP (ports:24, col:6, rows:2, super:2):

下面是经过 PHP 处理后的最终输出(端口:24、列:6、行:2、超级:2):

table

桌子

Here is a js fiddle that I threw together: http://jsfiddle.net/9SnLB/

这是我拼凑的一个 js 小提琴:http: //jsfiddle.net/9SnLB/

Currently, when I click the button nothing happens, but, I suppose that is my first issue, but am I going about the setup correctly? Why wont the button run the function?

目前,当我单击按钮时什么也没有发生,但是,我想这是我的第一个问题,但是我的设置是否正确?为什么按钮不会运行该功能?

回答by Rick Calder

Two mistakes. One you didn't close the function bracket, ie a missing } at the end. The second is you used $row instead of the variable you created num_rows. For some reason it doesn't work in the fiddle, it does however work locally. The fiddle is saying the createTable function is undefined.

两个错误。一个你没有关闭函数括号,即最后缺少一个 } 。第二个是您使用 $row 而不是您创建的变量 num_rows。出于某种原因,它在小提琴中不起作用,但它确实在本地工作。小提琴说 createTable 函数未定义。

function createTable()
{
    var num_ports = document.getElementById('ports').value;
    var num_super = document.getElementById('super').value;
    var num_rows = document.getElementById('rows').value;
    var num_cols = document.getElementById('cols').value;
    var tbody = '';
    var colStart = num_cols / num_super;
    for( var i=0; i<num_super; i++){
        var theader = '<div><table border="1">\n';
            for($u=1; $u<=num_rows; $u++){
              tbody += '<tr>';
                for( var j=0; j<colStart; j++)
                {
                tbody += '<td>';
                tbody += 'Cell ' + i + ',' + j;
                tbody += '</td>'
                }
        tbody += '</tr>\n';
    }
    var tfooter = '</table></div>';
    document.getElementById('wrapper').innerHTML = theader + tbody + tfooter;
}
}

回答by Qvcool

var table = [["a1","a2","a3"]["b1","b2","b3"]["c1","c2","c3"]];
for(x = table.length;x > 0;x--) {
  document.write("<tr>");
  for(y = table[x].length;y > 0;y--) {
    document.write("<td>"+y+"</td>");
  }
document.write("</tr>");
}

Sorry if the syntax is wrong. You get the idea.

对不起,如果语法错误。你明白了。

回答by wolfhammer

You need to change your jsFiddle framework to "no wrap (head)" and correct errors in the javascript. "no wrap (head)" will allow access the function. The "for ($u=1" loop is missing the close brace and $row should be num_rows. The "for (j=0" loop is missing a semicolon at the last "tbody=".

您需要将 jsFiddle 框架更改为“no wrap (head)”并更正 javascript 中的错误。“no wrap (head)”将允许访问该功能。"for ($u=1" 循环缺少右大括号,$row 应该是 num_rows。"for (j=0" 循环在最后一个 "tbody=" 处缺少分号。

here's the corrected js.

这是更正后的 js。

function createTable() {
var num_ports = document.getElementById('ports').value;
var num_super = document.getElementById('super').value;
var num_rows = document.getElementById('rows').value;
var num_cols = document.getElementById('cols').value;
var tbody = '';
var colStart = num_cols / num_super;
for (var i = 0; i < num_super; i++) {
    var theader = '<div><table border="1">\n';
    for ($u = 1; $u <= num_rows; $u++) {
        tbody += '<tr>';
        for (var j = 0; j < colStart; j++) {
            tbody += '<td>';
            tbody += 'Cell ' + i + ',' + j;
            tbody += '</td>';
        }
    }
    tbody += '</tr>\n';
}
var tfooter = '</table></div>';
document.getElementById('wrapper').innerHTML = theader + tbody + tfooter;

}

}