Hi,
I have a problem with some HTML blocks and JS codes and need your help.
I am trying to show the quantity in different warehouses for each product in store.
I found a way to do that, with Chat GPT help but the problem is that the same HTML and JS combination works if I put HTML in the motivation block, but not if I put it in the HTML block for the product card. JS code gets all the needed data but looks like it canât send data back to that HTML block that is in the product tab. You can see on the screenshot, that in the motivation block, there is data like 100,200,300âŚ, and in the product tab, there is no data.
Maybe itâs not the best way and best-written codes, but that is what I can do with Chat GPT
HTML:
<div id="sw_elm_availability" class="pib-title cm-combination">
<i class="material-icons">store</i><em>RaspoloĹživost u radnjama</em>
</div>
<div id="elm_availability" class="pib-content cm-popup-box" style="display: block;">
<ul>
<li>Magacin 1: <span id="magacin1-stock">Nema podataka</span> komada</li>
<li>Magacin 2: <span id="magacin2-stock">Nema podataka</span> komada</li>
<li>Magacin 3: <span id="magacin3-stock">Nema podataka</span> komada</li>
</ul>
</div>
<script src="/js/zaliha.js"></script>
JS:
document.addEventListener('DOMContentLoaded', function() {
const productCode = document.querySelector('.ty-control-group__item').innerText;
// UÄitavanje svih CSV fajlova za magacine
Promise.all([
fetch('https://noks.co.rs/images/magacin1.csv').then(response => response.text()),
fetch('https://noks.co.rs/images/magacin2.csv').then(response => response.text()),
fetch('https://noks.co.rs/images/magacin3.csv').then(response => response.text())
])
.then(([magacin1Data, magacin2Data, magacin3Data]) => {
let magacin1Stock = findStockInCSV(magacin1Data, productCode);
let magacin2Stock = findStockInCSV(magacin2Data, productCode);
let magacin3Stock = findStockInCSV(magacin3Data, productCode);
// AĹžuriranje HTML-a sa podacima o zalihama
document.getElementById('magacin1-stock').textContent = magacin1Stock;
document.getElementById('magacin2-stock').textContent = magacin2Stock;
document.getElementById('magacin3-stock').textContent = magacin3Stock;
})
.catch(error => {
console.error('GreĹĄka prilikom uÄitavanja CSV fajlova:', error);
});
});
// Funkcija za pronalazak koliÄine u CSV fajlu
function findStockInCSV(csvData, productCode) {
let rows = csvData.split('\n');
for (let i = 0; i < rows.length; i++) {
let cols = rows[i].split(';');
if (cols[0] === productCode) {
return cols[1]; // VraÄamo koliÄinu
}
}
return 'Nema podataka'; // Ako product_code nije pronaÄen
}
I am open to suggestions if somebody has some different easier ideas for the same resultsâŚ
Thanks in advance!