diff --git a/static/edited_csv.js b/static/edited_csv.js index b1277a7..cf26c0a 100644 --- a/static/edited_csv.js +++ b/static/edited_csv.js @@ -1,4 +1,7 @@ function collectEditedData(event) { + // Prevent default form submission + event.preventDefault(); + // Extract headers (attribute names) from the table const headers = [...document.querySelectorAll('.table-new-assets thead th')].map(th => th.dataset.attrib); const rows = document.querySelectorAll('.table-new-assets tbody tr'); @@ -7,8 +10,20 @@ function collectEditedData(event) { // Iterate through rows and collect data rows.forEach(row => { const cells = row.querySelectorAll('td'); + if (cells.length !== headers.length) { + alert("Mismatch between the number of cells and headers. Please check the table."); + return false; + } + let asset = {}; - headers.forEach((attrib, i) => asset[attrib] = cells[i].innerText); + headers.forEach((attrib, i) => { + if (cells[i]) { + asset[attrib] = cells[i].innerText.trim(); + } else { + alert(`Missing data for attribute: ${attrib}`); + return false; + } + }); assets.push(asset); }); @@ -17,9 +32,9 @@ function collectEditedData(event) { input.type = 'hidden'; input.name = 'assets'; input.value = JSON.stringify(assets); - document.querySelector('form').appendChild(input); + event.currentTarget.appendChild(input); // Submit the form - event.target.submit(); + event.currentTarget.submit(); return true; } \ No newline at end of file diff --git a/templates/csv_preview.html b/templates/csv_preview.html index ee8159a..d373d40 100644 --- a/templates/csv_preview.html +++ b/templates/csv_preview.html @@ -9,14 +9,28 @@