Fixed csv upload

This commit is contained in:
Candifloss 2025-02-24 23:27:11 +05:30
parent a3fe6f922b
commit 14d26fbaeb
3 changed files with 39 additions and 10 deletions

View File

@ -1,4 +1,7 @@
function collectEditedData(event) { function collectEditedData(event) {
// Prevent default form submission
event.preventDefault();
// Extract headers (attribute names) from the table // Extract headers (attribute names) from the table
const headers = [...document.querySelectorAll('.table-new-assets thead th')].map(th => th.dataset.attrib); const headers = [...document.querySelectorAll('.table-new-assets thead th')].map(th => th.dataset.attrib);
const rows = document.querySelectorAll('.table-new-assets tbody tr'); const rows = document.querySelectorAll('.table-new-assets tbody tr');
@ -7,8 +10,20 @@ function collectEditedData(event) {
// Iterate through rows and collect data // Iterate through rows and collect data
rows.forEach(row => { rows.forEach(row => {
const cells = row.querySelectorAll('td'); 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 = {}; 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); assets.push(asset);
}); });
@ -17,9 +32,9 @@ function collectEditedData(event) {
input.type = 'hidden'; input.type = 'hidden';
input.name = 'assets'; input.name = 'assets';
input.value = JSON.stringify(assets); input.value = JSON.stringify(assets);
document.querySelector('form').appendChild(input); event.currentTarget.appendChild(input);
// Submit the form // Submit the form
event.target.submit(); event.currentTarget.submit();
return true; return true;
} }

View File

@ -9,14 +9,28 @@
<body> <body>
<h1>CSV Preview</h1> <h1>CSV Preview</h1>
<!-- Display error messages from confirm_save -->
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<details>
<summary>Errors found during submission (click to expand):</summary>
<ul>
{% for category, message in messages %}
<li><strong>{{ category }}:</strong> {{ message }}</li>
{% endfor %}
</ul>
</details>
{% endif %}
{% endwith %}
<!-- Render tables for new and existing assets --> <!-- Render tables for new and existing assets -->
{% for table_name, assets, editable in [ {% for table_name, assets, editable, table_class in [
('New Assets', new_assets, true), ('New Assets', new_assets, true, 'table-new-assets'),
('Existing Assets', existing, false) ('Existing Assets', existing_assets, false, 'table-existing-assets')
] %} ] %}
{% if assets %} {% if assets %}
<h2>{{ table_name }}</h2> <h2>{{ table_name }}</h2>
<table border="1" class="table-new-assets"> <table border="1" class="{{ table_class }}">
<thead> <thead>
<tr> <tr>
{% for attrib in item_attributes %} {% for attrib in item_attributes %}

View File

@ -12,15 +12,15 @@
{% with messages = get_flashed_messages(with_categories=true) %} {% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %} {% if messages %}
{% for category, message in messages %} {% for category, message in messages %}
<p style="color: red;">{{ message }}</p> <p><strong>{{ category }}:</strong> {{ message }}</p>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
{% endwith %} {% endwith %}
<!-- Display validation errors if any --> <!-- Display validation errors if any -->
{% if errors %} {% if errors %}
<details style="margin-bottom: 20px;"> <details>
<summary style="color: red; font-weight: bold;"> <summary>
Errors found in the CSV file (click to expand): Errors found in the CSV file (click to expand):
</summary> </summary>
<ul> <ul>