-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (64 loc) · 2.35 KB
/
Copy pathscript.js
File metadata and controls
76 lines (64 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// ✅ Global products array
let products = [];
let filteredProducts = []; // filtered list for search
let currentIndex = 0;
const pageSize = 15;
// 📌 Google Sheet CSV Link (Published as CSV)
const SHEET_URL =
"https://docs.google.com/spreadsheets/d/e/2PACX-1vTlrxWsu4bchDXwiDcitxq71ZJAVmpkEeAdBvRXox9OWiDS5O1qwBetSjcFjmuRVP7FeZIF609O2n1H/pub?output=csv";
// 🔹 Fetch data from Google Sheets on page load
fetch(SHEET_URL)
.then((res) => res.text())
.then((text) => {
let rows = text.split("\n").slice(1); // skip header
products = rows.map((row) => {
let [name, price, qty, image] = row.split(",");
return {
name: name || "Unknown",
price: price || 0,
qty: qty || 0,
image: image || "https://via.placeholder.com/80",
};
});
filteredProducts = products; // initially no filter
displayProducts(true); // display first 15
})
.catch((err) => console.error("Error fetching CSV:", err));
// 🔹 Display products with paging
function displayProducts(initial = false) {
const box = document.getElementById("products");
if (initial) box.innerHTML = ""; // clear on new search
const nextProducts = filteredProducts.slice(
currentIndex,
currentIndex + pageSize
);
nextProducts.forEach((p) => {
box.innerHTML += `
<div class="card">
<img src="${p.image}" onerror="this.src='https://via.placeholder.com/80'">
<div class="info">
<b class="product-name">${p.name}</b><br>
<span class="price">💲តម្លៃ ${p.price}$</span><br>
<span class="qty">☑️ ប្រភេទ: ${p.qty}</span>
</div>
</div>
`;
});
currentIndex += pageSize;
// hide See More button if no more products
document.getElementById("loadMoreBtn").style.display =
currentIndex >= filteredProducts.length ? "none" : "block";
}
// 🔹 Search products
function searchProduct() {
const keyword = document.getElementById("search").value.toLowerCase();
filteredProducts = products.filter((p) =>
p.name.toLowerCase().includes(keyword)
);
currentIndex = 0; // reset index
displayProducts(true); // show first page of search result
}
// 🔹 See More button
document.getElementById("loadMoreBtn").addEventListener("click", () => {
displayProducts();
});