I have a table. I want to add rowspan to the first td of each row. But when I do that, it is messing up the table and pushing my data in the table to right.
This is my code:
let data = [
{device: 'iphone', site: 'google', val1:10, val2:20, val3:30},
{device: 'iphone', site: 'bing', val1:23, val2:12, val3:14},
{device: 'iphone', site: 'jeeves', val1:67, val2:78, val3:12},
{device: 'ipad', site: 'google', val1:10, val2:20, val3:30},
{device: 'ipad', site: 'bing', val1:23, val2:12, val3:14},
{device: 'ipad', site: 'jeeves', val1:67, val2:78, val3:12},
{device: 'mac', site: 'google', val1:10, val2:20, val3:30},
{device: 'mac', site: 'bing', val1:23, val2:12, val3:14},
{device: 'mac', site: 'jeeves', val1:67, val2:78, val3:15}
]
let heads = Object.keys(data[0]);
heads.forEach(d => $(`#headers`).append(`<th>${d}</th>`));
data.forEach(td => {
$(`#body_deets`).append(`<tr></tr>`);
heads.forEach(th => {
$(`#body_deets > tr:last`).append(`<td>${td[th]}</td>`);
});
});
$(`#body_deets > tr > td:first-child`).each(function() {
$(this).attr('rowspan', '3');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class='table' border=1>
<thead><tr id='headers'></tr></thead>
<tbody id='body_deets'></tbody>
</table>
As you can see, this is adding the rowspan and pushing a lot of my data to the right.
How can I get my table to look like this:
Is there a way to do this using jQuery?
