I need to generate a two-headed table that draws data from three different arrays - riders, passes, and prices. Riders (column header) and Passes (row header) are both headers. Prices are where the Riders and Passes intersect.
So something like this: https://www.w3.org/WAI/tutorials/tables/two-headers/
Here's where I've been successful:
Here's where I'm struggling:
My questions: 1. How can I make sure the rider array populates only the first column?
How do I iterate over two of the loops so that I get more than just the first value? How do I make sure each cell has only one value?
<script>
function createTable(){
mytable = $('<table></table>').attr({ id:
"basicTable",class:"table table-hover"});
let pass =['one ride', 'one ride', 'two ride', 'two ride', 'week', 'week', 'month', 'month']
console.log(pass[0]);
let rider = [ 'regular', 'reduced']
console.log(rider[0]);
let price = ['1.00', '2.00', '3.00', '4.00', '5.00', '6.00', '7.00', '8.00']
];
console.log(price[0]);
let rows = price.length;
console.log(rows);
let cols = rider.length
console.log(cols);
var tr = [];
for (var i = 0; i <= rows; i++) {
var row = $('<tr></tr>').attr({ class: ["class1"].join(' ')
}).appendTo(mytable);
console.log(row)
if (i==0) {
for (var j = 0; j < cols; j++) {
$('<th scope="col"></th>').text(pass[i]).attr({class:
["info"]}).appendTo(row);
}
}else if (i==1) {
for (var j = 0; j < cols; j++) {
$('<td scope="col"></td>').text(rider[i]).appendTo(row);
}
} else{
for (var j = 0; j < cols; j++) {
$('<td scope="row"></td>').text(price[i]).appendTo(row);
}}}
mytable.appendTo("#box");
}
You seem to be missing the values for one of the columns. The following code is with the data you provided. If you can provide other data and if the requirement is different I can maybe improve it afterwards.
let mytable = $('<table></table>').attr({
id: "basicTable",
class: "table table-hover"
});
const columnHeaders = ['one ride', 'one ride', 'two ride', 'two ride', 'week', 'week', 'month', 'month']
const rowHeaders = ['regular', 'reduced']
const rowValues = [['1.00', '2.00', '3.00', '4.00', '5.00', '6.00', '7.00', '8.00']]
//The above two dimensional array should map to the length of rows and columns, then the following code will not fail.
mytable.append(getTabelHTML(columnHeaders,rowHeaders,rowValues));
mytable.appendTo("#box");
function getTabelHTML(columnHeaders,rowHeaders, rowValues) {
//create col header html
const colheaderValue = ['', ...columnHeaders] //put one empty value for first empty column
.map((header) => `<th scope="col">${header}</th>`)
.join(''); //join all the <th> strings
const colheaderRowHTML = `<tr>${colheaderValue}</tr>`; //wrap inside <TR>
//create other row HTML
const otherRowsHTML = rowHeaders.map((header, index) => {
let rowHeader = `<th scope="col">${header}</th>`
let rowValue = ''
if(rowValues[index]) {
rowValue = rowValues[index].map((rowcolValue)=>`<td scope="row">${rowcolValue}</td>`).join();
}
return `<tr>${rowHeader}${rowValue}</tr>`;
});
return colheaderRowHTML.concat(otherRowsHTML);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box"></div>
</div>