lists.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. $('#removeResultBtn').hide(); // 隱藏全部清除按鈕
  2. // 處理 radio 選取狀態
  3. $('.filter-list .form-check').click(function (e) {
  4. e.preventDefault();
  5. $(this).find('.form-check-input').prop('checked', true);
  6. // 取得 radio 按鈕的 label
  7. let radioLabel = $(this).find('.form-check-label').text().trim().replace(/\s+/g, ' '); // 移除換行空白
  8. // 取上一層 search-tab 按鈕文字
  9. let buttonValue = $(this).closest('.dropdown').find('.search-tab').text().trim();
  10. let buttonId = $(this).closest('.dropdown').find('.search-tab').attr('id');
  11. updateSelectedOptions(buttonId, buttonValue, radioLabel);
  12. });
  13. let filterList = []; // 篩選條件
  14. // 更新篩選狀態
  15. function updateSelectedOptions(id, button, radio) {
  16. page = 1;
  17. console.log('更新篩選狀態', button, radio);
  18. if (filterList.length) {
  19. let exists = false; // 判斷是否已存在
  20. for (let index = 0; index < filterList.length; index++) {
  21. const element = filterList[index];
  22. // 如有重複 button 只改 radio 狀態
  23. if (element.text === button) {
  24. element.value = radio;
  25. exists = true;
  26. // 更新篩選的值
  27. $(`.search-tab-result .budget:contains(${button})`).find('p').text(`${button}:${radio}`);
  28. }
  29. }
  30. // 如果 filterList 中不存在則新增
  31. if (!exists) {
  32. const newItem = {
  33. id: id,
  34. text: button,
  35. value: radio
  36. };
  37. filterList.push(newItem);
  38. createFilterHtml(newItem);
  39. }
  40. } else {
  41. // 儲存篩選條件
  42. filterList.push({
  43. id: id,
  44. text: button,
  45. value: radio
  46. })
  47. for (let index = 0; index < filterList.length; index++) {
  48. const element = filterList[index];
  49. createFilterHtml(element);
  50. }
  51. }
  52. dataSearch(); // 搜尋
  53. console.log('filterList', filterList);
  54. // 切換按鈕選取狀態
  55. $('.dropdown').each(function () {
  56. let hasCheckedRadio = $(this).find('input[type="radio"]:checked').length > 0;
  57. $(this).find('.search-tab').toggleClass('active', hasCheckedRadio);
  58. });
  59. }
  60. // 新增篩選條件 HTML
  61. function createFilterHtml(item) {
  62. let dom = `
  63. <span class="me-3">
  64. <span class="d-flex budget">
  65. <p class="me-1">${item.text}:${item.value}</p>
  66. <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">
  67. </span>
  68. </span>`;
  69. $('.search-tab-result').append(dom);
  70. $('#removeResultBtn').show();
  71. }
  72. // 清除單一篩選條件
  73. function removeBtn(element, id, val) {
  74. filterList = filterList.filter(item => item.text !== val);
  75. ; // 移除 div & active
  76. $(element).closest('.me-3').remove();
  77. $(`#${id}`).removeClass('active');
  78. // 移除同一層級 ul 內所有 radio 的選取狀態
  79. $(`#${id}`).siblings('.dropdown-menu').find('input[type="radio"]').prop('checked', false);
  80. if (filterList.length === 0) {
  81. $('#removeResultBtn').hide();
  82. }
  83. dataSearch();
  84. }
  85. // 鍵盤 Enter 輸入
  86. $('.keywords').on('keydown', function (event) {
  87. if (event.key === 'Enter') {
  88. dataSearch();
  89. }
  90. });
  91. let maxPagesMobile = 5; // 手機板最多顯示 5 頁
  92. let maxPagesDesktop = 10; // 電腦版最多顯示 10 頁
  93. // 分頁
  94. function setPagination(pages) {
  95. let screenWidth = $(window).width();
  96. let maxPages = screenWidth > 991 ? maxPagesDesktop : maxPagesMobile;
  97. function renderPagination(currentPage) {
  98. let dom = `
  99. <li class="page-item" onclick="handlePagination(this, 'previous')">
  100. <a class="page-link previous hidden" href="#" aria-label="Previous">
  101. <span aria-hidden="true"><</span>
  102. </a>
  103. </li>`;
  104. let startPage = Math.max(currentPage - Math.floor(maxPages / 2), 1);
  105. let endPage = Math.min(startPage + maxPages - 1, pages);
  106. if (endPage - startPage < maxPages) {
  107. startPage = Math.max(endPage - maxPages + 1, 1);
  108. }
  109. for (let index = startPage; index <= endPage; index++) {
  110. dom += `
  111. <li class="page-item ${index === currentPage ? 'active' : ''}" onclick="handlePagination(this)">
  112. <a class="page-link" href="#">${index}</a>
  113. </li>`;
  114. }
  115. dom += `
  116. <li class="page-item" onclick="handlePagination(this, 'next')">
  117. <a class="page-link next" href="#" aria-label="Next">
  118. <span aria-hidden="true">></span>
  119. </a>
  120. </li>`;
  121. $('.filter-list .pagination').html(dom);
  122. updateVisibility(currentPage, pages);
  123. }
  124. // 更新上頁 & 下頁的按鈕顯示狀態
  125. function updateVisibility(page, pages) {
  126. if (page === 1) {
  127. $('.page-link.previous').addClass('hidden');
  128. } else {
  129. $('.page-link.previous').removeClass('hidden');
  130. }
  131. if (page === pages) {
  132. $('.page-link.next').addClass('hidden');
  133. } else {
  134. $('.page-link.next').removeClass('hidden');
  135. }
  136. }
  137. window.handlePagination = function (item, type = "") {
  138. if (type === "previous" && page > 1) {
  139. page--;
  140. } else if (type === "next" && page < pages) {
  141. page++;
  142. } else if (!type) {
  143. page = parseInt($(item).find('.page-link').text());
  144. }
  145. renderPagination(page);
  146. dataSearch();
  147. }
  148. renderPagination(page);
  149. $(window).on('resize', function () {
  150. let newScreenWidth = $(window).width();
  151. if ((newScreenWidth > 991 && maxPages !== maxPagesDesktop) ||
  152. (newScreenWidth <= 991 && maxPages !== maxPagesMobile)) {
  153. maxPages = newScreenWidth > 991 ? maxPagesDesktop : maxPagesMobile;
  154. renderPagination(page);
  155. }
  156. });
  157. }
  158. // 頁碼處理
  159. function handlePagination(item, type = "") {
  160. if (type === "previous") {
  161. // 往前一頁
  162. if (page > 1) {
  163. page--;
  164. }
  165. console.log(page);
  166. } else if (type === "next") {
  167. // 往後一頁
  168. page++;
  169. console.log(page);
  170. } else {
  171. // 直接點擊頁碼
  172. page = parseInt($(item).find('.page-link')[0].innerText);
  173. }
  174. // 切換選取狀態
  175. $('.filter-list .page-item').removeClass('active');
  176. $('.filter-list .page-item').eq(page).addClass('active');
  177. // 設定上一頁按鈕的顯示狀態
  178. if (page === 1) {
  179. $('.page-link.previous').addClass('hidden');
  180. } else {
  181. $('.page-link.previous').removeClass('hidden');
  182. }
  183. dataSearch(); // 搜尋
  184. }
  185. // 熱搜關鍵字搜尋
  186. $(".search-bar-keyword a").click(function () {
  187. let keyword = $(this).text();
  188. $('.keywords').val(keyword);
  189. dataSearch();
  190. });
  191. // 全部清除
  192. $("#removeResultBtn").click(function () {
  193. console.log('全部清除');
  194. filterList.length = 0; // 清空篩選陣列
  195. $('.keywords').val(''); // 清空搜尋欄位
  196. $('#removeResultBtn').hide(); // 隱藏全部清除按鈕
  197. $('.search-tab-result').empty(); // 清空篩選條件 dom
  198. // 取消選取狀態
  199. $('.filter-list input[type="radio"]').prop('checked', false);
  200. $('.search-tab').removeClass('active');
  201. dataSearch();
  202. });
  203. // 輸入框自動補全
  204. let designerKeywordList = [];
  205. async function getKeyword() {
  206. console.log('getKeyword');
  207. let url = '../../json/designer_keyword.json';
  208. try {
  209. const response = await axios.get(url);
  210. designerKeywordList = response.data.designer_keyword_list;
  211. } catch (error) {
  212. console.log("error", error);
  213. }
  214. }
  215. getKeyword();
  216. $('#keywordInput').on('input', function () {
  217. let input = $(this).val();
  218. closeAllLists();
  219. if (!input) {
  220. return false;
  221. }
  222. let divList = $('#autocomplete-list');
  223. for (let i = 0; i < designerKeywordList.length; i++) {
  224. if (designerKeywordList[i].substr(0, input.length).toUpperCase() === input.toUpperCase()) {
  225. let item = $('<div></div>');
  226. item.html("<strong>" + designerKeywordList[i].substr(0, input.length) + "</strong>" + designerKeywordList[i].substr(input.length));
  227. item.on('click', function() {
  228. $('#keywordInput').val($(this).text());
  229. closeAllLists();
  230. });
  231. item.on('mouseenter', function() {
  232. $('#keywordInput').val($(this).text());
  233. });
  234. divList.append(item);
  235. }
  236. }
  237. });
  238. // 清空自動補全列表
  239. function closeAllLists() {
  240. $('.autocomplete-items').empty();
  241. }
  242. $(document).on('click', function (e) {
  243. closeAllLists();
  244. });