You could iterate the array and build a new row if the remainder with 5 is zero.
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
tr;
array.forEach((v, i) => {
var td = document.createElement('td');
if (!(i % 5)) {
tr = document.createElement('tr');
document.getElementById('table0').appendChild(tr);
}
td.appendChild(document.createTextNode(v));
tr.appendChild(td);
});
<table id="table0"></table>
Answer from Nina Scholz on Stack OverflowYou could iterate the array and build a new row if the remainder with 5 is zero.
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
tr;
array.forEach((v, i) => {
var td = document.createElement('td');
if (!(i % 5)) {
tr = document.createElement('tr');
document.getElementById('table0').appendChild(tr);
}
td.appendChild(document.createTextNode(v));
tr.appendChild(td);
});
<table id="table0"></table>
var array = [1,2,3,4,5,6,7,8,9,10];
var result = "<table border=1>";
result += "<tr>";
for (var j = 0; j < array.length; j++) {
result += "<td>" + array[j] + "</td>";
if ((j + 1) % 5 == 0) {
result += "</tr><tr>";
}
}
result += "</tr>";
result += "</table>";
document.body.innerHTML = result;
Please give it a try if that works
Videos
var arr = ["1","2","-3","4","-5","-6","7"];
var table = document.createElement("table"), // create the table element
tbody = document.createElement("tbody"); // create the table body element
table.appendChild(tbody); // add the table body to table
// ### POSITIVE ROW ######################
var tr = document.createElement("tr"); // create the table row element
arr.forEach(function(e) { // for each element e in arr
var td = document.createElement("td"); // create a table cell element
if(e >= 0) { // if e is positive
td.textContent = e; // then change the text content of this cell to e
td.style.background = "#ccc"; // and change the background as well
}
tr.appendChild(td); // add the cell to the row (if the number is negative the cell will be empty and without background)
});
tbody.appendChild(tr); // add the row to table
//### NEGATIVE ROW ######################
tr = document.createElement("tr"); // same but for negative numbers ...
arr.forEach(function(e) {
var td = document.createElement("td");
if(e < 0) {
td.textContent = e;
td.style.background = "#ccc";
}
tr.appendChild(td);
});
tbody.appendChild(tr);
document.body.appendChild(table);
You can group some of the code in a function and reuse it:
// elt is shortcut for document.createElement
function elt(tag) {
return document.createElement(tag);
}
// take an array and a condition function conditionFN and return a row of all the element that passed the condition function with a background of #ccc
function makeRow(arr, conditionFN) {
var tr = elt("tr");
arr.forEach(function(e) {
var td = elt("td");
if (conditionFN(e)) {
td.textContent = e;
td.style.background = "#ccc";
}
tr.appendChild(td);
});
return tr;
}
// take an array and return the equivalent table element
function makeTable(arr) {
var table = elt("table"),
tbody = elt("tbody");
table.appendChild(tbody);
tbody.appendChild(makeRow(arr, function(e) { return e >= 0; })); // the condition function only allows positive numbers => positive row
tbody.appendChild(makeRow(arr, function(e) { return e < 0; })); // the condition function only allows negative numbers => negative row
return table;
}
document.body.appendChild(makeTable([1, 2, -3, 4, -5, -6, -7, 8, 9, 10]));
Using recursive function and table.inertRow() and row.insertCell()
var data = ["1", "2", "-3", "4", "-5", "-6", "7"];
var table = document.getElementById('myTable')
function plusMinusRows(arr, rowNum) {
var row = table.insertRow();
arr.map(Number).forEach(function(num) {
var value = !rowNum ? (num >= 0 ? num : '') : (num < 0 ? num : '');
var cell = row.insertCell();
cell.textContent = value;
cell.style.background = value !== '' ? '#cccccc' : '';
});
// call function again when rowNum not defined
!rowNum && plusMinusRows(arr, 2);
}
// initialize 2 rows
plusMinusRows(data);
<table id="myTable"></table>
Here's a function that will use the dom instead of string concatenation.
function createTable(tableData) {
var table = document.createElement('table');
var tableBody = document.createElement('tbody');
tableData.forEach(function(rowData) {
var row = document.createElement('tr');
rowData.forEach(function(cellData) {
var cell = document.createElement('td');
cell.appendChild(document.createTextNode(cellData));
row.appendChild(cell);
});
tableBody.appendChild(row);
});
table.appendChild(tableBody);
document.body.appendChild(table);
}
createTable([["row 1, cell 1", "row 1, cell 2"], ["row 2, cell 1", "row 2, cell 2"]]);
This is pretty easy to do with a double for loop.
function makeTableHTML(myArray) {
var result = "<table border=1>";
for(var i=0; i<myArray.length; i++) {
result += "<tr>";
for(var j=0; j<myArray[i].length; j++){
result += "<td>"+myArray[i][j]+"</td>";
}
result += "</tr>";
}
result += "</table>";
return result;
}
$(function () {
var $wrap = $('<div/>').attr('id', 'tableWrap');
var $tbl = $('<table/>').attr('id', 'basicTable');
for(i=0;i<myArray.length;i++){
// $(function () { is miss placed
$tbl.append($('<tr/>').append(
$('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>'),
$('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>'),
$('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>'),
$('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>'),
$('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>'),
$('<td/>').html('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]+'"></a>')));
}
$wrap.append($tbl);
$('#c').append($wrap);
});
PROBLEM
- Miss place of
$(function) {.. alt="'+myArray[i][1]'"should bealt="'+myArray[i][1]+'"
DEMO
First, you forgot to close table element after for statement
Second, why $(function() { after for ? Is wrong. If would use this, you have to close it with });
var $wrap = $('<div>').attr('id', 'tableWrap');
var $tbl = $('<table>').attr('id', 'basicTable');
for(i=0;i<myArray.length;i++){
$tbl.append($('<tr>').append(
$('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>'),
$('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>'),
$('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>'),
$('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>'),
$('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>'),
$('<td>').text('<a href="'+myArray[i][2]+'" target="_new"><img src="'+myArray[i][3]+'" alt="'+myArray[i][1]'"></a>')));
}
$tbl.append("</table">);
You could get first the headers of the table and while iterating the column names, you could collect the length of every data array and take the max length of it for later iterating the td elements.
var data = [{ Header: "Column1", Values: ["75", "79", "83"] }, { Header: "Column2", Values: ["77", "81", "86", "90"] }, { Header: "Column3", Values: ["98", "117"] }],
table = document.createElement('table'),
tr = document.createElement('tr'),
rows = [],
max = 0,
i;
table.appendChild(tr);
data.forEach(function (o) {
var th = document.createElement('th');
th.appendChild(document.createTextNode(o.Header));
tr.appendChild(th);
max = Math.max(max, o.Values.length);
});
for (i = 0; i < max; i++) {
tr = document.createElement('tr'),
table.appendChild(tr);
data.forEach(function (o) {
var td = document.createElement('td');
tr.appendChild(td);
td.appendChild(document.createTextNode(i in o.Values ? o.Values[i] : ''));
});
}
document.body.appendChild(table);
You'll have to use DOM methods and nested for loops to achieve this.
var tableJSON = [
{
"Header": "Column1",
"Values": [
"75",
"79",
"83"
]
},
{
"Header": "Column2",
"Values": [
"77",
"81",
"86",
"90"
]
},
{
"Header": "Column3",
"Values": [
"98",
"117"
]
}
];
var tableDiv = document.createElement("table");
var trElement = document.createElement("tr");
tableDiv.appendChild( trElement );
var prDiv = document.getElementById("pr");
tableJSON.forEach(function(a_column, index){
if(a_column.Header)
{
var tdElement = document.createElement("td");
tdElement.innerHTML = "<b>" + a_column.Header + "</b>";
trElement.appendChild( tdElement );
}
if(a_column.Values){
var allRows = tableDiv.childNodes;
for(var i=0 ;i< a_column.Values.length; i++)
{
var rowWanted = allRows[i+1];
if( !rowWanted )
{
rowWanted = document.createElement("tr");
tableDiv.appendChild( rowWanted );
}
if(rowWanted.childNodes.length==0)
for(var j=0; j< tableJSON.length; j++){
rowWanted.appendChild( document.createElement("td") );
}
rowWanted.childNodes[ index ].innerHTML = a_column.Values[i];
}
}
});
prDiv.appendChild(tableDiv);
<div id="pr">
</div>