I have an array and I want to create below HTML table from the array
I’ve tried writing the code bleow but seems like its appending rows in different places
Is there any other way to archive above table. im basically trying to create like Gannt chart kind of table
// Extract unique years and months
var years = [...new Set(dataArray.map(item => item.StartYear))];
var months = [...new Set(dataArray.map(item => item.StartMonth).concat(dataArray.map(item =>
item.EndMonth)))];
// Sort years and months
years.sort();
months.sort();
// Create the table dynamically
var tableHTML = '<table>';
tableHTML += '<tr><th>Year</th>';
years.forEach(year => tableHTML += `<th colspan="${months.length}">${year}</th>`);
tableHTML += '</tr>';
tableHTML += '<tr><td>Month</td>';
months.forEach(month => tableHTML += `<td>${month}</td>`);
tableHTML += '</tr>';
dataArray.forEach(item => {
tableHTML += `<tr><td>${item.Project}</td>`;
years.forEach(year => {
months.forEach(month => {
var isInProjectRange = (year === item.StartYear && month >= item.StartMonth && month <=
item.EndMonth) ||
(year === item.EndYear && month <= item.EndMonth);
tableHTML += `<td>${isInProjectRange ? 'YES' : ''}</td>`;
});
});
tableHTML += '</tr>';
});
tableHTML += '</table>';
// Append the table to the div with id 'table-container'
document.getElementById('table-container').innerHTML = tableHTML;
<div id="table-container"></div>
<script>
var dataArray = [
{'Project': 'Project 1', 'StartYear': '2023', 'EndYear': '2023', 'StartMonth': 'Nov', 'EndMonth': 'Dec', 'Owner':'Tom'},
{'Project': 'Project 1.1', 'StartYear': '2023', 'EndYear': '2023', 'StartMonth': 'Nov', 'EndMonth': 'Dec', 'Owner':'Tom'},
{'Project': 'Project 2', 'StartYear': '2024', 'EndYear': '2024', 'StartMonth': 'Jan', 'EndMonth': 'May', 'Owner':'Tom'},
{'Project': 'Project 2.1', 'StartYear': '2024', 'EndYear': '2024', 'StartMonth': 'Jan', 'EndMonth': 'Feb', 'Owner':'Tom'},
{'Project': 'Project 2.2', 'StartYear': '2024', 'EndYear': '2024', 'StartMonth': 'Feb', 'EndMonth': 'Mar', 'Owner':'Tom'},
{'Project': 'Project 2.3', 'StartYear': '2024', 'EndYear': '2024', 'StartMonth': 'Mar', 'EndMonth': 'May', 'Owner':'Tom'}
];
</script>
2
Answers
Here is a snippet that demonstrates one possible way of doing it:
In order to have an easier structure to scan through I converted your
dataArray
into an array of arrays:proj
. The subarrays ofproj
each contain three elements: the project name, a start date and an end date in numerical form (years*100 + month-index).In order to make the months sortable I also translated them back into numerical forms and I keep the array
months
andmidx
for lookup purposes in both directions.d0
andd1
are the start and the end of the project tables overview (moin and max dates to be displayed).In the main
for
loop I go fromd0
tod1
and calculate the current year and month for each of the numerical dates. Whenever a new year is started I need to recalculatem
,y
andd
(see afterif (m==12)
)The rest of the loop is now concerned with storing a
YES
or a blank into the appropriate field of the 2D arrayres
.Update
I added the
colspan
mechanism for the table header. This involves the use of aMap
object. Now the result should look exactly like the expected one.I am sure my answer can be simplified further. Go ahead and try it 🤷 …
Tried doing it my way and it’s a bit longer than the accepted answer but I think a nicer solution in my opinion.
Since your example table, never had a project that lasted more than one year or at least sometime in both years I’ve considered it might happen.
The first object of dataArray is changed so it lasts three years so you can see the effect if the project lasts longer.
Quick explanation:
Hope it helps 🙌🏼
Check the snippet: