简体   繁体   English

用数组项填充表的第一列 - JavaScript

[英]Populate the first column of the table with array items - JavaScript

I am new to coding and trying to write a function that would generate a table with 3 columns and with the number of rows equal to the number of indexes in an array.我是编码新手并试图编写一个函数,该函数将生成一个包含 3 列且行数等于数组中索引数的表。 This is the array and the code I'm using to generate the table这是我用来生成表格的数组和代码

var msg1Arr = ['loginCommand', 'version', 'xID', 'passcode', 'machineID', 'equipSN', 'userSlot', 'clubID', 'loginType'];

function generateTable(){
    var body = document.getElementsByTagName("body")[0];
    var tbl = document.createElement("table");
    var tblBody = document.createElement("tbody");

    for(var i=0; i<realArray.length; i++){ //columns
        var row=document.createElement("tr");

        for (var j=0; j < 3; j++){  //rows
            var cell = document.createElement("td");
            var cellText = document.createTextNode("t");
            cell.appendChild(cellText);
            row.appendChild(cell);
        }
    tblBody.appendChild(row);
    }
    tbl.appendChild(tblBody);
    body.appendChild(tbl); 
    tbl.setAttribute("border", "2");
};

However, this code fills in all cells with "t" string.但是,此代码使用“t”字符串填充所有单元格。 I need the array to populate the first column, one index per row.我需要数组来填充第一列,每行一个索引。 Please help!请帮忙!

realArray[i]替换"t"

In order the first column to be populated with the arrays elements.为了用数组元素填充第一列。 Modify the code to be修改代码为

var msg1Arr = ['loginCommand', 'version', 'xID', 'passcode', 'machineID', 'equipSN', 'userSlot', 'clubID', 'loginType'];
    
    function generateTable(){
        var body = document.getElementsByTagName("body")[0];
        var tbl = document.createElement("table");
        var tblBody = document.createElement("tbody");
    
        for(var i=0; i<msg1Arr.length; i++){ //columns
            var row=document.createElement("tr");
    
            for (var j=0; j < 3; j++){  //rows
                var cell = document.createElement("td");
                var cellText = document.createTextNode("t");
                if(j == 0){
                   cellText = document.createTextNode(msg1Arr[i]);
                }
                cell.appendChild(cellText);
                row.appendChild(cell);
            }
        tblBody.appendChild(row);
        }
        tbl.appendChild(tblBody);
        body.appendChild(tbl); 
        tbl.setAttribute("border", "2");
     };

You could use a forEach loop on the array which will iterate with each value within the array.您可以在数组上使用 forEach 循环,它将迭代数组中的每个值。 Set your value and index in the forEach function v , i ... Insert the row with each iteration of the forEach loop, then set a for loop to create your table data columns that will reside within the rows.在 forEach 函数中设置您的值和索引v , i ... 在 forEach 循环的每次迭代中插入行,然后设置一个 for 循环来创建将驻留在行中的表数据列。 You said three, so the for loop can be hard coded to create a TD each time you loop through the for loop.您说的是三个,因此每次循环遍历 for 循环时,都可以对 for 循环进行硬编码以创建 TD。 Lastly to set the first column tot he value within the array, use a conditional that checks if the iterator is set to 1 , if it is then set the textContent of that TD to the value with in the array...最后,要将第一列设置为数组中的值,请使用条件检查迭代器是否设置为1 ,如果是,则将该 TD 的 textContent 设置为数组中的值...

See the comments in the code for further explanation.请参阅代码中的注释以获得进一步的解释。

 var msg1Arr = ['loginCommand', 'version', 'xID', 'passcode', 'machineID', 'equipSN', 'userSlot', 'clubID', 'loginType']; function generateTable() { // get the parent element here i used a div 'container' let cont = document.getElementById("container"); // create the table element let table = document.createElement("TABLE"); // use forEach on the array to loop through each iteration of values in the array msg1Arr.forEach(function(v, i) { // append the parent element (display) element with the table cont.append(table); // each iteration through the array, insert a table row into the table element // we use the index of the foreach loop to insert a table row let tr = table.insertRow(i); // iterate through the desired amount of columns using a for loop for (let j = 1; j <= 3; j++) { // each iteration of the for loop, create a table data let td = document.createElement('TD'); // append the row with the newly created TD tr.append(td); // conditional to see if the index of the td's is the first iteration of the TD's // if it is, then we insert the value within the array into that table data column if (j === 1) { td.textContent = v; } } // set your attributed for the border in the table table.setAttribute("border", "2"); // append the table row to the table table.append(tr); }) }; // run the function generateTable();
 <div id="container"> <!--write a function that would generate a table with 3 columns and with the number of rows equal to the number of indexes in an array.--> </div>

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

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