123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- $('#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 = `
- <span class="me-3">
- <span class="d-flex budget">
- <p class="me-1">${item.text}:${item.value}</p>
- <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">
- </span>
- </span>`;
- $('.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 = `
- <li class="page-item" onclick="handlePagination(this, 'previous')">
- <a class="page-link previous hidden" href="#" aria-label="Previous">
- <span aria-hidden="true"><</span>
- </a>
- </li>`;
- 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 += `
- <li class="page-item ${index === currentPage ? 'active' : ''}" onclick="handlePagination(this)">
- <a class="page-link" href="#">${index}</a>
- </li>`;
- }
- dom += `
- <li class="page-item" onclick="handlePagination(this, 'next')">
- <a class="page-link next" href="#" aria-label="Next">
- <span aria-hidden="true">></span>
- </a>
- </li>`;
- $('.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 = $('<div></div>');
- item.html("<strong>" + designerKeywordList[i].substr(0, input.length) + "</strong>" + 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();
- });
|