SyuanYu преди 7 месеца
родител
ревизия
0ecd22e3a9
променени са 2 файла, в които са добавени 69 реда и са изтрити 21 реда
  1. 7 0
      js/lists.js
  2. 62 21
      search/lists/js/main.js

+ 7 - 0
js/lists.js

@@ -109,6 +109,13 @@ function removeBtn(element, id, val) {
   }
 
   dataSearch();
+
+  if (id === "keyword") {
+    // 判斷函數是否存在
+    if (typeof getAllSearchCount === 'function') {
+      getAllSearchCount();
+    }
+  }
 }
 
 // 鍵盤 Enter 輸入

+ 62 - 21
search/lists/js/main.js

@@ -169,9 +169,15 @@ async function dataSearch(type = "") {
   }
 
   try {
+    let isExist = filterList.some((item) => item.id === "keyword"); // 判斷是否已存在關鍵字
+
+    if (isExist) {
+      getAllSearchCount(); // 重新取得總筆數
+    }
+
     const response = await axios.get(url);
-    console.log('response.data >>', response.data);
 
+    let totalCount = response.data.total_count;
     let totalPages = Math.ceil(totalCount / pageSize);
 
     if (totalPages) {
@@ -227,7 +233,9 @@ async function dataSearch(type = "") {
         }
 
         let caseCount = response.data.total_count.toLocaleString();
-        $("#caseCount").html(`(${caseCount})`);
+        setTimeout(() => {
+          $("#caseCount").html(`(${caseCount})`);
+        }, 100)
 
         break;
 
@@ -260,7 +268,9 @@ async function dataSearch(type = "") {
         }
 
         let designerCount = response.data.total_count.toLocaleString();
-        $("#designerCount").html(`(${designerCount})`);
+        setTimeout(() => {
+          $("#designerCount").html(`(${designerCount})`);
+        }, 100)
 
         break;
 
@@ -297,7 +307,9 @@ async function dataSearch(type = "") {
         }
 
         let videoCount = response.data.total_count.toLocaleString();
-        $("#videoCount").html(`(${videoCount})`);
+        setTimeout(() => {
+          $("#videoCount").html(`(${videoCount})`);
+        }, 100)
 
         break;
 
@@ -347,7 +359,9 @@ async function dataSearch(type = "") {
         }
 
         let columnCount = response.data.total_count.toLocaleString();
-        $("#columnCount").html(`(${columnCount})`);
+        setTimeout(() => {
+          $("#columnCount").html(`(${columnCount})`);
+        }, 100)
 
         break;
 
@@ -380,7 +394,9 @@ async function dataSearch(type = "") {
         }
 
         let builderCount = response.data.total_count.toLocaleString();
-        $("#builderCount").html(`(${builderCount})`);
+        setTimeout(() => {
+          $("#builderCount").html(`(${builderCount})`);
+        }, 100)
 
         break;
 
@@ -438,9 +454,23 @@ dataSearch();
 async function getAllSearchCount() {
   let url = "https://m3.hhh.com.tw:18673/all_search_count";
 
+  if (filterList.length) {
+    let input;
+
+    // 取得關鍵字
+    filterList.find((item) => {
+      if (item.id === "keyword") {
+        input = item.value;
+
+        if (input !== "") {
+          url += `?keyword=${input}`
+        }
+      }
+    });
+  }
+
   try {
     const response = await axios.get(url);
-    console.log('response.data', response.data);
 
     $("#caseCount").text(`(${response.data.case_count.toLocaleString()})`)
     $("#designerCount").text(`(${response.data.designer_count.toLocaleString()})`)
@@ -472,16 +502,21 @@ function setTotalCount() {
   // 將各個數字相加
   let total = caseCount + designerCount + videoCount + builderCount + columnCount;
   $('#totalCount').text(total.toLocaleString());
-  console.log('Total:', total);
 }
 
 // 切換 Tab
 $('.search-tab-list .nav-item').click(function (e) {
-  getAllSearchCount(); // 重新取得總筆數
+  // getAllSearchCount(); // 重新取得總筆數
+
+  let isExist = filterList.some((item) => item.id === "keyword"); // 判斷是否已存在關鍵字
+
+  if (!isExist) {
+    getAllSearchCount(); // 重新取得總筆數
+  }
+
   // 使用 closest() 找到最近的 button 元素
   let button = $(e.target).closest('button');
   assignTab = button.attr('id');
-  console.log('assignTab', assignTab);
 
   if (assignTab === "designerTab") {
     $('.search-btn-filter').find('.active').removeClass('active');
@@ -493,11 +528,25 @@ $('.search-tab-list .nav-item').click(function (e) {
     $("#recommendItem").hide();
   }
 
+  // 需保留關鍵字,其餘條件全部清除
+  if (isExist) {
+    filterList = filterList.filter(item => item.text === "關鍵字");
+    // 刪除關鍵字以外的條件
+    $('.search-tab-result span.me-3').each(function () {
+      if (!$(this).find('p').text().includes('關鍵字')) {
+        $(this).remove();
+      }
+    });
+
+  } else {
+    filterList.length = 0; // 清空篩選陣列
+    $('#removeResultBtn').hide(); // 隱藏全部清除按鈕
+    $('.search-tab-result').empty(); // 清空篩選條件 dom
+  }
+
+
   // 全部清除
-  filterList.length = 0; // 清空篩選陣列
   $('.keywords').val(''); // 清空搜尋欄位
-  $('#removeResultBtn').hide(); // 隱藏全部清除按鈕
-  $('.search-tab-result').empty(); // 清空篩選條件 dom
   // 取消選取狀態
   $('.filter-list input[type="radio"]').prop('checked', false);
   $('.search-tab').removeClass('active');
@@ -535,9 +584,7 @@ let subCategory = [
 
 // 專欄文章分類
 $("#pills-column #mainCategory .form-check").click(function (e) {
-  console.log('e >>', e);
   let btnText = $(e.target).text().replace(/\s+/g, ''); // 取得主分類
-  console.log('btnText', btnText);
 
   // 檢查陣列中是否有次分類
   let hasSubCategory = filterList.find(item => item.text === "次分類");
@@ -573,8 +620,6 @@ $("#pills-column #mainCategory .form-check").click(function (e) {
 })
 
 function clickSubBtn(e, item) {
-  console.log('clickSubBtn', e, item);
-
   let currentElement = $(e.currentTarget);
 
   currentElement.find('.form-check-input').prop('checked', true);
@@ -586,9 +631,5 @@ function clickSubBtn(e, item) {
   let buttonValue = currentElement.closest('.dropdown').find('.search-tab').text().trim();
   let buttonId = currentElement.closest('.dropdown').find('.search-tab').attr('id');
 
-  console.log('buttonValue>>', buttonValue);
-  console.log('buttonId>>', buttonId);
-  console.log('radioLabel>>', radioLabel);
-
   updateSelectedOptions(buttonId, buttonValue, radioLabel);
 }