From 1f73ae2323317d7edbfa93f7e3ac39be2579d5cf Mon Sep 17 00:00:00 2001 From: Gregoo Date: Fri, 31 Jan 2025 10:19:59 +0100 Subject: [PATCH] Configure the Grid search through data- attributes --- static/scripts/grid/filter.js | 35 +++++++++++++++++++++++++++++++---- templates/sets.html | 2 +- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/static/scripts/grid/filter.js b/static/scripts/grid/filter.js index 7406274..d0d8903 100644 --- a/static/scripts/grid/filter.js +++ b/static/scripts/grid/filter.js @@ -9,6 +9,19 @@ class BrickGridFilter { // Search setup if (this.html_search) { + // Exact attributes + if (this.html_search.dataset.searchExact) { + this.search_exact = new Set(this.html_search.dataset.searchExact.split(",").map(el => el.trim())); + } else { + this.search_exact = new Set(); + } + + // List attributes + this.search_list = []; + if (this.html_search.dataset.searchList) { + this.search_list = this.html_search.dataset.searchList.split(",").map(el => el.trim()); + } + this.html_search.addEventListener("keyup", ((gridfilter) => () => { gridfilter.filter(); })(this)); @@ -88,10 +101,24 @@ class BrickGridFilter { // Check all searchable fields for a match if (options.search) { - for (let attribute of ["data-name", "data-number", "data-parts", "data-theme", "data-year"]) { - if (current.getAttribute(attribute).includes(options.search)) { - current.parentElement.classList.remove("d-none"); - return; + // Browse the whole dataset + for (const set in current.dataset) { + // Exact attribute + if (this.search_exact.has(set)) { + if (current.dataset[set].includes(options.search)) { + current.parentElement.classList.remove("d-none"); + return; + } + } else { + // List search + for (const list of this.search_list) { + if (set.startsWith(this.search_list)) { + if (current.dataset[set].includes(options.search)) { + current.parentElement.classList.remove("d-none"); + return; + } + } + } } } diff --git a/templates/sets.html b/templates/sets.html index 6a07d73..e8d70a2 100644 --- a/templates/sets.html +++ b/templates/sets.html @@ -10,7 +10,7 @@
Search - +