$('#removeResultBtn').hide(); // 隱藏全部清除按鈕 // 處理 radio 選取狀態 $('.filter-list .form-check').click(function (e) { e.preventDefault(); $(this).find('.form-check-input').prop('checked', true); // 取得 radio 按鈕的 label let radioLabel = $(this).find('.form-check-label').text().trim().replace(/\s+/g, ' '); // 移除換行空白 // 取上一層 search-tab 按鈕文字 let buttonValue = $(this).closest('.dropdown').find('.search-tab').text().trim(); let buttonId = $(this).closest('.dropdown').find('.search-tab').attr('id'); updateSelectedOptions(buttonId, buttonValue, radioLabel); }); let filterList = []; // 篩選條件 // 更新篩選狀態 function updateSelectedOptions(id, button, radio) { page = 1; console.log('更新篩選狀態', button, radio); if (filterList.length) { let exists = false; // 判斷是否已存在 for (let index = 0; index < filterList.length; index++) { const element = filterList[index]; // 如有重複 button 只改 radio 狀態 if (element.text === button) { element.value = radio; exists = true; // 更新篩選的值 $(`.search-tab-result .budget:contains(${button})`).find('p').text(`${button}:${radio}`); } } // 如果 filterList 中不存在則新增 if (!exists) { const newItem = { id: id, text: button, value: radio }; filterList.push(newItem); createFilterHtml(newItem); } } else { // 儲存篩選條件 filterList.push({ id: id, text: button, value: radio }) for (let index = 0; index < filterList.length; index++) { const element = filterList[index]; createFilterHtml(element); } } dataSearch(); // 搜尋 console.log('filterList', filterList); // 切換按鈕選取狀態 $('.dropdown').each(function () { let hasCheckedRadio = $(this).find('input[type="radio"]:checked').length > 0; $(this).find('.search-tab').toggleClass('active', hasCheckedRadio); }); } // 新增篩選條件 HTML function createFilterHtml(item) { let dom = `

${item.text}:${item.value}

close-btn-search
`; $('.search-tab-result').append(dom); $('#removeResultBtn').show(); } // 清除單一篩選條件 function removeBtn(element, id, val) { filterList = filterList.filter(item => item.text !== val); ; // 移除 div & active $(element).closest('.me-3').remove(); $(`#${id}`).removeClass('active'); // 移除同一層級 ul 內所有 radio 的選取狀態 $(`#${id}`).siblings('.dropdown-menu').find('input[type="radio"]').prop('checked', false); if (filterList.length === 0) { $('#removeResultBtn').hide(); } dataSearch(); } // 鍵盤 Enter 輸入 $('.keywords').on('keydown', function (event) { if (event.key === 'Enter') { dataSearch(); } }); let maxPagesMobile = 5; // 手機板最多顯示 5 頁 let maxPagesDesktop = 10; // 電腦版最多顯示 10 頁 // 分頁 function setPagination(pages) { let screenWidth = $(window).width(); let maxPages = screenWidth > 991 ? maxPagesDesktop : maxPagesMobile; function renderPagination(currentPage) { let dom = `
  • `; let startPage = Math.max(currentPage - Math.floor(maxPages / 2), 1); let endPage = Math.min(startPage + maxPages - 1, pages); if (endPage - startPage < maxPages) { startPage = Math.max(endPage - maxPages + 1, 1); } for (let index = startPage; index <= endPage; index++) { dom += `
  • ${index}
  • `; } dom += `
  • `; $('.filter-list .pagination').html(dom); updateVisibility(currentPage, pages); } // 更新上頁 & 下頁的按鈕顯示狀態 function updateVisibility(page, pages) { if (page === 1) { $('.page-link.previous').addClass('hidden'); } else { $('.page-link.previous').removeClass('hidden'); } if (page === pages) { $('.page-link.next').addClass('hidden'); } else { $('.page-link.next').removeClass('hidden'); } } window.handlePagination = function (item, type = "") { if (type === "previous" && page > 1) { page--; } else if (type === "next" && page < pages) { page++; } else if (!type) { page = parseInt($(item).find('.page-link').text()); } renderPagination(page); dataSearch(); } renderPagination(page); $(window).on('resize', function () { let newScreenWidth = $(window).width(); if ((newScreenWidth > 991 && maxPages !== maxPagesDesktop) || (newScreenWidth <= 991 && maxPages !== maxPagesMobile)) { maxPages = newScreenWidth > 991 ? maxPagesDesktop : maxPagesMobile; renderPagination(page); } }); } // 頁碼處理 function handlePagination(item, type = "") { if (type === "previous") { // 往前一頁 if (page > 1) { page--; } console.log(page); } else if (type === "next") { // 往後一頁 page++; console.log(page); } else { // 直接點擊頁碼 page = parseInt($(item).find('.page-link')[0].innerText); } // 切換選取狀態 $('.filter-list .page-item').removeClass('active'); $('.filter-list .page-item').eq(page).addClass('active'); // 設定上一頁按鈕的顯示狀態 if (page === 1) { $('.page-link.previous').addClass('hidden'); } else { $('.page-link.previous').removeClass('hidden'); } dataSearch(); // 搜尋 } // 熱搜關鍵字搜尋 $(".search-bar-keyword a").click(function () { let keyword = $(this).text(); $('.keywords').val(keyword); dataSearch(); }); // 全部清除 $("#removeResultBtn").click(function () { console.log('全部清除'); filterList.length = 0; // 清空篩選陣列 $('.keywords').val(''); // 清空搜尋欄位 $('#removeResultBtn').hide(); // 隱藏全部清除按鈕 $('.search-tab-result').empty(); // 清空篩選條件 dom // 取消選取狀態 $('.filter-list input[type="radio"]').prop('checked', false); $('.search-tab').removeClass('active'); dataSearch(); }); // 輸入框自動補全 let designerKeywordList = []; async function getKeyword() { console.log('getKeyword'); let url = '../../json/designer_keyword.json'; try { const response = await axios.get(url); designerKeywordList = response.data.designer_keyword_list; } catch (error) { console.log("error", error); } } getKeyword(); $('#keywordInput').on('input', function () { let input = $(this).val(); closeAllLists(); if (!input) { return false; } let divList = $('#autocomplete-list'); for (let i = 0; i < designerKeywordList.length; i++) { if (designerKeywordList[i].substr(0, input.length).toUpperCase() === input.toUpperCase()) { let item = $('
    '); item.html("" + designerKeywordList[i].substr(0, input.length) + "" + designerKeywordList[i].substr(input.length)); item.on('click', function() { $('#keywordInput').val($(this).text()); closeAllLists(); }); item.on('mouseenter', function() { $('#keywordInput').val($(this).text()); }); divList.append(item); } } }); // 清空自動補全列表 function closeAllLists() { $('.autocomplete-items').empty(); } $(document).on('click', function (e) { closeAllLists(); });