Fixed csv upload
This commit is contained in:
parent
a3fe6f922b
commit
14d26fbaeb
@ -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;
|
||||
}
|
@ -9,14 +9,28 @@
|
||||
<body>
|
||||
<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 -->
|
||||
{% 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 %}
|
||||
<h2>{{ table_name }}</h2>
|
||||
<table border="1" class="table-new-assets">
|
||||
<table border="1" class="{{ table_class }}">
|
||||
<thead>
|
||||
<tr>
|
||||
{% for attrib in item_attributes %}
|
||||
|
@ -12,15 +12,15 @@
|
||||
{% with messages = get_flashed_messages(with_categories=true) %}
|
||||
{% if messages %}
|
||||
{% for category, message in messages %}
|
||||
<p style="color: red;">{{ message }}</p>
|
||||
<p><strong>{{ category }}:</strong> {{ message }}</p>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
<!-- Display validation errors if any -->
|
||||
{% if errors %}
|
||||
<details style="margin-bottom: 20px;">
|
||||
<summary style="color: red; font-weight: bold;">
|
||||
<details>
|
||||
<summary>
|
||||
Errors found in the CSV file (click to expand):
|
||||
</summary>
|
||||
<ul>
|
||||
|
Loading…
Reference in New Issue
Block a user