[FIX] website_sale_aplicoop: Complete infinite scroll and search filter integration

Major fixes:
- Fix JSON body parsing in load_products_ajax with type='http' route
  * Parse JSON from request.httprequest.get_data() instead of post params
  * Correctly read page, search, category from JSON request body

- Fix search and category filter combination
  * Use intersection (&) instead of replacement to preserve both filters
  * Now respects search AND category simultaneously

- Integrate realtime_search.js with infinite_scroll.js
  * Add resetWithFilters() method to reset scroll to page 1 with new filters
  * When search/category changes, reload products from server
  * Clear grid and load fresh results

- Fix pagination reset logic
  * Set currentPage = 0 in resetWithFilters() so loadNextPage() increments to 1
  * Prevents loading empty page 2 when resetting filters

Results:
 Infinite scroll loads all pages correctly (1, 2, 3...)
 Search filters work across all products (not just loaded)
 Category filters work correctly
 Search AND category filters work together
 Page resets to 1 when filters change
This commit is contained in:
snt 2026-02-17 01:26:20 +01:00
parent 5eb039ffe0
commit 40ce973bd6
4 changed files with 463 additions and 239 deletions

View file

@ -7,6 +7,26 @@
console.log("[INFINITE_SCROLL] Script loaded!");
// Visual indicator for debugging
if (typeof document !== "undefined") {
try {
var debugDiv = document.createElement("div");
debugDiv.innerHTML = "[INFINITE_SCROLL LOADED]";
debugDiv.style.position = "fixed";
debugDiv.style.top = "0";
debugDiv.style.right = "0";
debugDiv.style.backgroundColor = "#00ff00";
debugDiv.style.color = "#000";
debugDiv.style.padding = "5px 10px";
debugDiv.style.fontSize = "12px";
debugDiv.style.zIndex = "99999";
debugDiv.id = "infinite-scroll-debug";
document.body.appendChild(debugDiv);
} catch (e) {
console.error("[INFINITE_SCROLL] Error adding debug div:", e);
}
}
(function () {
"use strict";
@ -38,12 +58,14 @@ console.log("[INFINITE_SCROLL] Script loaded!");
this.perPage = parseInt(configEl.getAttribute("data-per-page")) || 20;
this.currentPage = parseInt(configEl.getAttribute("data-current-page")) || 1;
// Check if there are more products to load
var hasNextBtn = document.getElementById("load-more-btn");
this.hasMore = hasNextBtn && hasNextBtn.offsetParent !== null; // offsetParent === null means hidden
// Check if there are more products to load from data attribute
var hasNextAttr = configEl.getAttribute("data-has-next");
this.hasMore = hasNextAttr === "true" || hasNextAttr === "True";
if (!this.hasMore) {
console.log("[INFINITE_SCROLL] No more products to load");
console.log(
"[INFINITE_SCROLL] No more products to load (has_next=" + hasNextAttr + ")"
);
return;
}
@ -108,6 +130,44 @@ console.log("[INFINITE_SCROLL] Script loaded!");
console.log("[INFINITE_SCROLL] Fallback button listener attached");
},
resetWithFilters: function (searchQuery, categoryId) {
/**
* Reset infinite scroll to page 1 with new filters and reload products.
* Called by realtime_search when filters change.
*/
console.log(
"[INFINITE_SCROLL] Resetting with filters: search=" +
searchQuery +
" category=" +
categoryId
);
this.searchQuery = searchQuery || "";
this.category = categoryId || "0";
this.currentPage = 0; // Set to 0 so loadNextPage() increments to 1
this.isLoading = false;
this.hasMore = true;
// Update the config element data attributes for consistency
var configEl = document.getElementById("eskaera-config");
if (configEl) {
configEl.setAttribute("data-search", this.searchQuery);
configEl.setAttribute("data-category", this.category);
configEl.setAttribute("data-current-page", "1");
configEl.setAttribute("data-has-next", "true");
}
// Clear the grid and reload from page 1
var grid = document.getElementById("products-grid");
if (grid) {
grid.innerHTML = "";
console.log("[INFINITE_SCROLL] Grid cleared");
}
// Load first page with new filters
this.loadNextPage();
},
loadNextPage: function () {
var self = this;
this.isLoading = true;