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 @@

CSV Preview

+ + {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} +
+ Errors found during submission (click to expand): + +
+ {% endif %} + {% endwith %} + - {% for table_name, assets, editable in [ - ('New Assets', new_assets, true), - ('Existing Assets', existing, false) + {% for table_name, assets, editable, table_class in [ + ('New Assets', new_assets, true, 'table-new-assets'), + ('Existing Assets', existing_assets, false, 'table-existing-assets') ] %} {% if assets %}

{{ table_name }}

- +
{% for attrib in item_attributes %} diff --git a/templates/upload.html b/templates/upload.html index d128491..5d1914e 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -12,15 +12,15 @@ {% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} {% for category, message in messages %} -

{{ message }}

+

{{ category }}: {{ message }}

{% endfor %} {% endif %} {% endwith %} {% if errors %} -
- +
+ Errors found in the CSV file (click to expand):