简体   繁体   English

从JavaScript中的数组创建具有3列的表

[英]Creating Table with 3 column from an array in javascript

I am trying to generate 3 columns in each row of a table from an Array, I am getting the result but, I am not able get 3 columns each row. 我试图从一个数组的表的每一行中生成3列,我得到的结果,但我不能每行获得3列。

Could anyone help me spout the issue? 有人可以帮我喷出这个问题吗? please. 请。

var tTable = "<table border=\"0\">";

var newArray = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"];
var newTr = "<tr>";
for (var i = 0; i < newArray.length; i++) {

 if(i % 3 == 0 ) {
 newTr += "<td>" + newArray[i] + "</td></tr><tr>";
 } else{
 newTr += "<td>" + newArray[i] + "</td>";
 }

}
newTr+="</tr>";
tTable += newTr;
document.write(tTable);

Current Result is 当前结果是

2
3   4   5
6   7   8
9   10  11
12  13  14
15

but, the expected result is 但是,预期结果是

2   3   4
5   6   7
8   9   10
11  12  13
14  15

Any help on this how to solve this prob please? 请问对此有任何帮助如何解决这个问题?

Try something like this: 尝试这样的事情:

var tTable = "<table border=\"0\">";

var newArray = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"];
var newTr = "";
for (var i = 0; i < newArray.length; i++) {

  if(i % 3 == 0)
    newTr += (i > 0) ? "</tr><tr>" : "<tr>"


 newTr += "<td>" + newArray[i] + "</td>";

}
newTr+="</tr>";
tTable += newTr;
document.write(tTable);

As you start with element i = 0, you need to be careful because 0/3 = 0. In my example code, i check, like you, if we are at the the 3º element of the row, but i put a "special" condition when i = 0. So, in the first element we only create the row (), and for the next 3rd elements, we close the previous row and create a new one. 从元素i = 0开始,您需要小心,因为0/3 =0。在我的示例代码中,我像您一样检查是否在行的3º元素处,但是我将“特殊i = 0时的条件。因此,在第一个元素中,我们仅创建row(),而对于下一个第3个元素,我们关闭前一行并创建一个新行。

Your loop is evaulting to zero on that first run since your starting index is zero: 由于您的起始索引为零,因此您的循环在第一次运行时就评估为零:

if(i % 3 == 0) //where i = 0, you'll get 0

This ends your row. 这样就结束了您的行。 You should fix that. 你应该解决这个问题。

If you're going to "build" html in Javascript, you should really look into document.createElement. 如果要使用Javascript“构建” HTML,则应真正研究document.createElement。 Building HTML out of strings will likely be unpredictable. 用字符串构建HTML可能是不可预测的。

https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement https://developer.mozilla.org/zh-CN/docs/Web/API/Document/createElement

I won't explain everything else here, you can read the docs and figure it out -- this should solve any issues you might be having. 我不会在这里解释其他所有内容,您可以阅读文档并弄清楚-这应该可以解决您可能遇到的任何问题。

The error is with 错误是

if(i % 3 == 0 ) {
 newTr += "<td>" + newArray[i] + "</td></tr><tr>";
 }

Change the way you open and close the row. 更改打开和关闭行的方式。

 var tableBuilder = ''; var columns = 3; var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; for (var index = 0; index < data.length; index++) { if (index % columns == 0) { if (index > 0)tableBuilder += '</tr>'; tableBuilder += '<tr>' } tableBuilder += '<td>'; tableBuilder += index; tableBuilder += '</td>'; } tableBuilder += '</tr>' document.getElementById("display").innerHTML = tableBuilder; 
 <table id="display"> </table> 

The first time your loop runs, i = 0. so 0 % 3 = 0. which triggers the end of the row. 循环第一次运行时,i =0。因此0%3 =0。这将触发行的结尾。 Make your loop start at one instead by setting i = 1, not 0. 通过设置i = 1而不是0,使循环从1开始。

for (var i = 1; i < newArray.length; i++) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM