fixed results scrambling
This commit is contained in:
@@ -101,15 +101,23 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
if (data && data.items && data.items.length > 0) {
|
if (data && data.items && data.items.length > 0) {
|
||||||
resultsContainer.innerHTML = '';
|
resultsContainer.innerHTML = '';
|
||||||
|
|
||||||
data.items.forEach((item, index) => {
|
// Filter out null/undefined items first
|
||||||
if (!item) return; // Skip null/undefined items
|
const validItems = data.items.filter(item => item);
|
||||||
|
|
||||||
|
validItems.forEach((item, index) => {
|
||||||
const cardElement = createResultCard(item, searchType.value, index);
|
const cardElement = createResultCard(item, searchType.value, index);
|
||||||
|
|
||||||
|
// Store the item data directly on the button element
|
||||||
|
const downloadBtn = cardElement.querySelector('.download-btn');
|
||||||
|
if (downloadBtn) {
|
||||||
|
downloadBtn.dataset.itemIndex = index;
|
||||||
|
}
|
||||||
|
|
||||||
resultsContainer.appendChild(cardElement);
|
resultsContainer.appendChild(cardElement);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Attach download handlers to the newly created cards
|
// Attach download handlers to the newly created cards
|
||||||
attachDownloadListeners(data.items);
|
attachDownloadListeners(validItems);
|
||||||
} else {
|
} else {
|
||||||
// No results found
|
// No results found
|
||||||
resultsContainer.innerHTML = `
|
resultsContainer.innerHTML = `
|
||||||
@@ -133,12 +141,15 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
* Attaches download handlers to result cards
|
* Attaches download handlers to result cards
|
||||||
*/
|
*/
|
||||||
function attachDownloadListeners(items) {
|
function attachDownloadListeners(items) {
|
||||||
document.querySelectorAll('.download-btn').forEach((btn, index) => {
|
document.querySelectorAll('.download-btn').forEach((btn) => {
|
||||||
btn.addEventListener('click', (e) => {
|
btn.addEventListener('click', (e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
|
||||||
|
// Get the item index from the button's dataset
|
||||||
|
const itemIndex = parseInt(btn.dataset.itemIndex, 10);
|
||||||
|
|
||||||
// Get the corresponding item
|
// Get the corresponding item
|
||||||
const item = items[index];
|
const item = items[itemIndex];
|
||||||
if (!item) return;
|
if (!item) return;
|
||||||
|
|
||||||
const type = searchType.value;
|
const type = searchType.value;
|
||||||
@@ -323,7 +334,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
<div class="track-title">${item.name || 'Unknown'}</div>
|
<div class="track-title">${item.name || 'Unknown'}</div>
|
||||||
<div class="track-artist">${subtitle}</div>
|
<div class="track-artist">${subtitle}</div>
|
||||||
<div class="track-details">${details}</div>
|
<div class="track-details">${details}</div>
|
||||||
<button class="download-btn btn-primary">
|
<button class="download-btn btn-primary" data-item-index="${index}">
|
||||||
<img src="/static/images/download.svg" alt="Download" />
|
<img src="/static/images/download.svg" alt="Download" />
|
||||||
Download
|
Download
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
Reference in New Issue
Block a user