From 32bce4e062681b353056f0618a9f34f40bf16159 Mon Sep 17 00:00:00 2001 From: Pengan Zhou Date: Sat, 3 Nov 2018 03:10:34 -0700 Subject: [PATCH] Add search support --- base.css | 7 ++++++- index.html | 8 +++++++- index.js | 40 +++++++++++++++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/base.css b/base.css index 7df658a..5174b68 100644 --- a/base.css +++ b/base.css @@ -59,6 +59,11 @@ a:active { text-decoration: none; } -figure a{ +figure a { + display: block; +} + +#loadmore { + text-align: center; display: block; } \ No newline at end of file diff --git a/index.html b/index.html index efff4b4..0f1f627 100644 --- a/index.html +++ b/index.html @@ -35,8 +35,11 @@ -
+
+
+ +
+
+ 加载更多 +
diff --git a/index.js b/index.js index aaa24d3..941ad60 100644 --- a/index.js +++ b/index.js @@ -13,16 +13,20 @@ function getUrlVars() { function processJson(data) { machineList = data; - machineList = splitArrayByTime(machineList); + reorderedList = splitArrayByTime(machineList); var i, j, temparray, chunk = 32; - for (i = 0, j = machineList.length; i < j; i += chunk) { - temparray = machineList.slice(i, i + chunk); + for (i = 0, j = reorderedList.length; i < j; i += chunk) { + temparray = reorderedList.slice(i, i + chunk); pages.push(temparray); } showMachines(pages[loadedPage]); } +function loadmore() { + showMachines(pages[loadedPage]); +} + function showMachines(machines) { var base = $("#base-cell"); var machineListContainer = $("#machine-list"); @@ -44,11 +48,37 @@ function showMachines(machines) { clone.show(); clone.attr("id", machine.id); clone.find("a").attr("href", playerlink); - clone.find("figcaption").text(title) - clone.find(".figure-img").attr("src", imageLink) + clone.find("figcaption").text(title); + clone.find(".figure-img").attr("src", imageLink); + clone.addClass("show-data"); machineListContainer.append(clone); } loadedPage++; + if (loadedPage >= pages.length) { + $("#loadmore").hide(); + } +} + +function search() { + + var keyword = $("#search-text").val(); + if (!keyword || keyword.length == 0) { + $(".show-data").remove(); + loadedPage = 0; + processJson(machineList); + return; + } + var searchResult = []; + for (i = 0; i < machineList.length; i++) { + var text = machineList[i].name + machineList[i].vendor; + if (text.includes(keyword)) { + searchResult.push(machineList[i]); + } + } + pages = []; + loadedPage = 0; + $(".show-data").remove(); + showMachines(searchResult); } function splitArrayByTime(someArray) { -- GitLab