main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // $( document ).ready(function() {
  2. // });
  3. // 載入共用 template
  4. $('#navbar').load('../../template/navbar.html');
  5. $('#footer').load('../../template/footer.html');
  6. $('#btn-box').load('../../template/button.html');
  7. $('#topCarousel').load('../../template/top_carousel.html');
  8. $('#removeResultBtn').hide(); // 隱藏全部清除按鈕
  9. // 處理 radio 選取狀態
  10. $('.filter-list .form-check').click(function (e) {
  11. e.preventDefault();
  12. $(this).find('.form-check-input').prop('checked', true);
  13. // 取得 radio 按鈕的 label
  14. let radioLabel = $(this).find('.form-check-label').text().trim().replace(/\s+/g, ' '); // 移除換行空白
  15. // 取上一層 search-tab 按鈕文字
  16. let buttonValue = $(this).closest('.dropdown').find('.search-tab').text().trim();
  17. let buttonId = $(this).closest('.dropdown').find('.search-tab').attr('id');
  18. updateSelectedOptions(buttonId, buttonValue, radioLabel);
  19. });
  20. let filterList = []; // 篩選條件
  21. // 更新篩選狀態
  22. function updateSelectedOptions(id, button, radio) {
  23. page = 1;
  24. console.log('更新篩選狀態', button, radio);
  25. if (filterList.length) {
  26. let exists = false; // 判斷是否已存在
  27. for (let index = 0; index < filterList.length; index++) {
  28. const element = filterList[index];
  29. // 如有重複 button 只改 radio 狀態
  30. if (element.text === button) {
  31. element.value = radio;
  32. exists = true;
  33. // 更新篩選的值
  34. $(`.search-tab-result .budget:contains(${button})`).find('p').text(`${button}:${radio}`);
  35. }
  36. }
  37. // 如果 filterList 中不存在則新增
  38. if (!exists) {
  39. const newItem = {
  40. id: id,
  41. text: button,
  42. value: radio
  43. };
  44. filterList.push(newItem);
  45. createFilterHtml(newItem);
  46. }
  47. } else {
  48. // 儲存篩選條件
  49. filterList.push({
  50. id: id,
  51. text: button,
  52. value: radio
  53. })
  54. for (let index = 0; index < filterList.length; index++) {
  55. const element = filterList[index];
  56. createFilterHtml(element);
  57. }
  58. }
  59. designerSearch(); // 搜尋
  60. console.log('filterList', filterList);
  61. // 切換按鈕選取狀態
  62. $('.dropdown').each(function () {
  63. let hasCheckedRadio = $(this).find('input[type="radio"]:checked').length > 0;
  64. $(this).find('.search-tab').toggleClass('active', hasCheckedRadio);
  65. });
  66. }
  67. // 新增篩選條件 HTML
  68. function createFilterHtml(item) {
  69. let dom = `
  70. <div class="me-3">
  71. <span class="d-flex budget">
  72. <p class="me-1">${item.text}:${item.value}</p>
  73. <img onclick="removeBtn(this, '${item.id}', '${item.text}')" src="https://hhh.com.tw/assets/images/section/icon/close-btn-search.svg" alt="close-btn-search">
  74. </span>
  75. </div>`;
  76. $('.search-tab-result').append(dom);
  77. $('#removeResultBtn').show();
  78. }
  79. // 清除單一篩選條件
  80. function removeBtn(element, id, val) {
  81. console.log('id, val', id, val);
  82. filterList = filterList.filter(item => item.text !== val);
  83. ; // 移除 div & active
  84. $(element).closest('.me-3').remove();
  85. $(`#${id}`).removeClass('active');
  86. // 移除同一層級 ul 內所有 radio 的選取狀態
  87. $(`#${id}`).siblings('.dropdown-menu').find('input[type="radio"]').prop('checked', false);
  88. if (filterList.length === 0) {
  89. $('#removeResultBtn').hide();
  90. }
  91. designerSearch();
  92. }
  93. let assignOrder = ""; // 當前排序
  94. // new, hot, recommend 排序 (預設推薦)
  95. $(".search-btn-filter button").click(function () {
  96. // 切換選取狀態
  97. $('.search-btn-filter').find('.active').removeClass('active');
  98. $(this).addClass('active');
  99. assignOrder = $(this).attr('id');
  100. designerSearch("order_by");
  101. });
  102. let page = 1; // 當前頁數
  103. let pageSize = 18; // 每頁數量
  104. let isFirstLoad = true; // 初始載入
  105. // 列表篩選
  106. async function designerSearch(type = "") {
  107. let url;
  108. $('#designerSpinner').show();
  109. $('#designerList').hide();
  110. if (isFirstLoad) {
  111. // 第一次載入使用本地 JSON 文件
  112. url = './json/designers_lists_data.json';
  113. } else {
  114. // 後續使用 API (預設排序為 recommend)
  115. url = `https://m3.hhh.com.tw:18673/designer_search?page=${page}&page_size=${pageSize}`;
  116. if (type === "order_by") {
  117. url += `&${type}=${assignOrder}`;
  118. }
  119. if (assignOrder === "") {
  120. url += "&order_by=recommend";
  121. }
  122. let input = $(".keywords").val();
  123. if (input !== "") {
  124. url += `&keyword=${input}`;
  125. }
  126. if (filterList.length) {
  127. filterList.map(item => {
  128. if (item.id === 'budget') {
  129. switch (item.value) {
  130. case "100萬以下":
  131. url += `&${item.id}=1`;
  132. break;
  133. case "100~200萬":
  134. url += `&${item.id}=2`;
  135. break;
  136. case "200~300萬":
  137. url += `&${item.id}=3`;
  138. break;
  139. case "300~400萬":
  140. url += `&${item.id}=4`;
  141. break;
  142. case "400~500萬":
  143. url += `&${item.id}=5`;
  144. break;
  145. case "500萬以上":
  146. url += `&${item.id}=6`;
  147. break;
  148. default:
  149. break;
  150. }
  151. } else {
  152. url += `&${item.id}=${item.value}`;
  153. }
  154. });
  155. }
  156. }
  157. try {
  158. const response = await axios.get(url);
  159. console.log('response.data.designers', response.data.designers);
  160. let totalCount = response.data.total_count;
  161. let totalPages = Math.ceil(totalCount / pageSize);
  162. $("#totalCount").html(totalCount);
  163. if (totalPages) {
  164. $('.filter-list .pagination').show();
  165. setPagination(totalPages); // 分頁處理
  166. } else {
  167. $('.filter-list .pagination').hide();
  168. }
  169. let resultHtml = '';
  170. if (response.data.designers.length) {
  171. response.data.designers.forEach((item) => {
  172. resultHtml += `
  173. <div class="col-md-4 mb-4">
  174. <a href="${item.DesignerLink}">
  175. <div class="card lists-card">
  176. <img src="${item.CaseCoverImg}" class="cover-img" alt="${item.DesignerName} ${item.DesignerTitle}">
  177. <div class="card-body d-flex align-items-center">
  178. <div class="person-img me-3 me-md-2 me-lg-3" style="background-image: url('${item.DesignerCoverImg}');"></div>
  179. <section>
  180. <h5 class="text-muted mb-2">${item.DesignerTitle}</h5>
  181. <h6 class="mb-0 text-dark">
  182. <span class="font-weight-bold">
  183. ${item.DesignerName}
  184. </span>
  185. 設計師
  186. </h6>
  187. </section>
  188. </div>
  189. </div>
  190. </a>
  191. </div>`;
  192. });
  193. } else {
  194. resultHtml += "<p class='text-center mt-5'>找不到符合的資料,請重新搜尋。</p>"
  195. }
  196. $('#designerList').html(resultHtml);
  197. setTimeout(() => {
  198. $('#designerList').show();
  199. $('#designerSpinner').hide();
  200. }, 100)
  201. // 更新初始載入狀態
  202. if (isFirstLoad) {
  203. isFirstLoad = false;
  204. }
  205. } catch (error) {
  206. console.log("error", error);
  207. }
  208. }
  209. designerSearch();
  210. $('.keywords').on('keydown', function (event) {
  211. if (event.key === 'Enter') {
  212. designerSearch();
  213. }
  214. });
  215. let maxPagesMobile = 5; // 手機板最多顯示 5 頁
  216. let maxPagesDesktop = 10; // 電腦版最多顯示 10 頁
  217. // 分頁
  218. function setPagination(pages) {
  219. let screenWidth = $(window).width();
  220. let maxPages = screenWidth > 991 ? maxPagesDesktop : maxPagesMobile;
  221. function renderPagination(currentPage) {
  222. let dom = `
  223. <li class="page-item" onclick="handlePagination(this, 'previous')">
  224. <a class="page-link previous hidden" href="#" aria-label="Previous">
  225. <span aria-hidden="true"><</span>
  226. </a>
  227. </li>`;
  228. let startPage = Math.max(currentPage - Math.floor(maxPages / 2), 1);
  229. let endPage = Math.min(startPage + maxPages - 1, pages);
  230. if (endPage - startPage < maxPages) {
  231. startPage = Math.max(endPage - maxPages + 1, 1);
  232. }
  233. for (let index = startPage; index <= endPage; index++) {
  234. dom += `
  235. <li class="page-item ${index === currentPage ? 'active' : ''}" onclick="handlePagination(this)">
  236. <a class="page-link" href="#">${index}</a>
  237. </li>`;
  238. }
  239. dom += `
  240. <li class="page-item" onclick="handlePagination(this, 'next')">
  241. <a class="page-link next" href="#" aria-label="Next">
  242. <span aria-hidden="true">></span>
  243. </a>
  244. </li>`;
  245. $('.filter-list .pagination').html(dom);
  246. updateVisibility(currentPage, pages);
  247. }
  248. // 更新上頁 & 下頁的按鈕顯示狀態
  249. function updateVisibility(page, pages) {
  250. if (page === 1) {
  251. $('.page-link.previous').addClass('hidden');
  252. } else {
  253. $('.page-link.previous').removeClass('hidden');
  254. }
  255. if (page === pages) {
  256. $('.page-link.next').addClass('hidden');
  257. } else {
  258. $('.page-link.next').removeClass('hidden');
  259. }
  260. }
  261. window.handlePagination = function (item, type = "") {
  262. if (type === "previous" && page > 1) {
  263. page--;
  264. } else if (type === "next" && page < pages) {
  265. page++;
  266. } else if (!type) {
  267. page = parseInt($(item).find('.page-link').text());
  268. }
  269. renderPagination(page);
  270. designerSearch();
  271. }
  272. renderPagination(page);
  273. $(window).on('resize', function () {
  274. let newScreenWidth = $(window).width();
  275. if ((newScreenWidth > 991 && maxPages !== maxPagesDesktop) ||
  276. (newScreenWidth <= 991 && maxPages !== maxPagesMobile)) {
  277. maxPages = newScreenWidth > 991 ? maxPagesDesktop : maxPagesMobile;
  278. renderPagination(page);
  279. }
  280. });
  281. }
  282. // 頁碼處理
  283. function handlePagination(item, type = "") {
  284. if (type === "previous") {
  285. // 往前一頁
  286. if (page > 1) {
  287. page--;
  288. }
  289. console.log(page);
  290. } else if (type === "next") {
  291. // 往後一頁
  292. page++;
  293. console.log(page);
  294. } else {
  295. // 直接點擊頁碼
  296. page = parseInt($(item).find('.page-link')[0].innerText);
  297. }
  298. // 切換選取狀態
  299. $('.filter-list .page-item').removeClass('active');
  300. $('.filter-list .page-item').eq(page).addClass('active');
  301. // 設定上一頁按鈕的顯示狀態
  302. if (page === 1) {
  303. $('.page-link.previous').addClass('hidden');
  304. } else {
  305. $('.page-link.previous').removeClass('hidden');
  306. }
  307. designerSearch(); // 搜尋
  308. }
  309. // 熱搜關鍵字搜尋
  310. $(".search-bar-keyword a").click(function () {
  311. let keyword = $(this).text();
  312. $('.keywords').val(keyword);
  313. designerSearch();
  314. });
  315. // 全部清除
  316. $("#removeResultBtn").click(function () {
  317. console.log('全部清除');
  318. filterList.length = 0; // 清空篩選陣列
  319. $('.keywords').val(''); // 清空搜尋欄位
  320. $('#removeResultBtn').hide(); // 隱藏全部清除按鈕
  321. $('.search-tab-result').empty(); // 清空篩選條件 dom
  322. // 取消選取狀態
  323. $('.filter-list input[type="radio"]').prop('checked', false);
  324. $('.search-tab').removeClass('active');
  325. designerSearch();
  326. });