简体   繁体   English

来自多个JavaScript数组的HTML表

[英]HTML table from multiple JavaScript arrays

I am building a little interactive website for some people to use. 我正在建立一个供某些人使用的交互式小网站。 I have a lot of it built but am stuck on this part. 我有很多内置的,但都停留在这部分上。

There are 4 arrays that are based on a users selections, they start as empty arrays. 有4个基于用户选择的数组,它们以空数组开始。 As a user clicks on an item it's four components are added to each array respectively, they are all numbers. 当用户单击一个项目时,它的四个组成部分分别添加到每个数组中,它们都是数字。

Item 1: 3,5,7,9 项目1:3、5、7、9

Item 2: 2,4,6,8 项目2:2、4、6、8

    var bgArray = [3,2]; 
    var minority = [5,4];
    var poverty = [7,6];
    var lep = [9,8];

What I want to do is build an HTML table from the four arrays and have it look like the following. 我想要做的是从四个数组构建一个HTML表,并使它看起来像下面的样子。 Basically row 1 would be [0] for each array, row 2 would be [1] and so on. 基本上,每个数组的第1行为[0],第2行为[1],依此类推。 I cannot figure out a way to get them to line up. 我想不出一种办法让他们排队。 (run code snippet to see table) (运行代码段以查看表格)

 <table> <tr> <td>H1</td> <td>H2</td> <td>H3</td> <td>H4</td> </tr> <tr> <td>3</td> <td>5</td> <td>7</td> <td>9</td> </tr> <tr> <td>2</td> <td>4</td> <td>6</td> <td>8</td> </tr> </table> 

I plan to have users make their selections and then click a button to generate the table. 我计划让用户进行选择,然后单击一个按钮以生成表。 They can also deselect an item and regenerate the table. 他们还可以取消选择一项并重新生成表格。

Thanks in advance. 提前致谢。

You can iterate the arrays (considering that the length of the four matrixes is the same) and build the lines of table: 您可以迭代数组(考虑到四个矩阵的长度相同)并构建表行:

 var bgArray = [3,2] , minority = [5,4] , poverty = [7,6] , lep = [9,8]; var lines = ""; for (let i = 0 ; i < bgArray.length ; i++){ lines += "<tr>" lines += "<td>" + bgArray[i] + "</td>" lines += "<td>" + minority[i] + "</td>" lines += "<td>" + poverty[i] + "</td>" lines += "<td>" + lep[i] + "</td>" lines += "</tr>" } $("table").append(lines); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border="1"> <tr> <th>H1</th> <th>H2</th> <th>H3</th> <th>H4</th> </tr> </table> 

Eric, this code may be what you are requesting. 埃里克(Eric),此代码可能是您所要求的。 This will help you draw your table with the values of the arrays on a button click. 这将帮助您在单击按钮时使用表格的值绘制表格。

 var bgArray = [3,2,1]; var minority = [5,4,3]; var poverty = [7,6,5]; var lep = [9,8,7]; $("button").on("click", function(e){ e.preventDefault(); // Get the length of one array var numRows = bgArray.length; // Clear the table (except first row) $("table tr:not(:first-child)").remove(); // Add the rows for(var i = 0; i < numRows; i++){ var newRow = $("<tr></tr>"); newRow.append(createNewColData(bgArray[i])); newRow.append(createNewColData(minority[i])); newRow.append(createNewColData(poverty[i])); newRow.append(createNewColData(lep[i])); $("table").append(newRow); } }); // Auxiliar function to render a table data function createNewColData(data){ return "<td>" + data + "</td>"; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button>Add values</button> <br/> <br/> <table> <tr> <td>H1</td> <td>H2</td> <td>H3</td> <td>H4</td> </tr> </table> 

I'm not sure how your button display will work, but if you update your markup I could update my answer. 我不确定您的按钮显示如何工作,但是如果您更新标记,则可以更新我的答案。

Good luck and happy coding! 祝您好运,编码愉快!

You can achieve this without using any javascript library. 您无需使用任何javascript库即可实现此目的。

Vanilla Javascript will do 香草Javascript会做

HTML 的HTML

<table border="1">
  <tr>
    <th>H1</th>
    <th>H2</th>
    <th>H3</th>
    <th>H4</th>
  </tr>

</table>

JavaScript 的JavaScript

var bgArray = [3,2], minority = [5,4], poverty = [7,6], lep = [9,8];

var row = "";
for (var i = 0 ; i < bgArray.length ; i++){
      row += "<tr>" ; 
      row += "<td>" + bgArray[i] + "</td>"  ;    
      row += "<td>" + minority[i] + "</td>" ;
      row += "<td>" + poverty[i] + "</td>" ;
      row += "<td>" + lep[i] + "</td>" ;
      row += "</tr>";
}

var t = document.querySelector('table');

t.innerHTML = t.innerHTML + row;

The JSFiddle is here JSFiddle在这里

 var bgArray = [3,2], minority = [5,4], poverty = [7,6], lep = [9,8]; var row = ""; for (var i = 0 ; i < bgArray.length ; i++){ row += "<tr>" ; row += "<td>" + bgArray[i] + "</td>" ; row += "<td>" + minority[i] + "</td>" ; row += "<td>" + poverty[i] + "</td>" ; row += "<td>" + lep[i] + "</td>" ; row += "</tr>"; } var t = document.querySelector('table'); t.innerHTML = t.innerHTML + row; 
 <table border="1"> <tr> <th>H1</th> <th>H2</th> <th>H3</th> <th>H4</th> </tr> </table> 

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

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