17370845950

如何实现卡片搜索无结果时准确显示“未找到卡片”提示

本文旨在解决动态卡片搜索中“未找到卡片”提示显示不准确的问题。通过优化javascript逻辑,我们展示了一种更健壮的方法:首先隐藏所有卡片,然后根据搜索条件过滤并仅显示匹配的卡片,最后根据匹配结果的数量精确控制“无内容”提示的可见性,确保用户体验的准确性和流畅性。

动态卡片搜索中“无结果”提示的实现与优化

在网页开发中,实现带有搜索功能的卡片布局是常见的需求。用户输入搜索关键词后,页面应动态显示匹配的卡片,并在没有匹配结果时提供友好的提示。然而,不恰当的逻辑可能导致“无结果”提示过早或错误地出现。本教程将详细探讨如何使用HTML、CSS和JavaScript构建一个健壮的卡片搜索功能,并特别关注“无结果”提示的正确显示。

初始问题分析

原始的JavaScript代码在处理搜索逻辑时存在一个常见误区。它在遍历卡片时,一旦遇到不匹配的卡片就立即将“无内容”提示设置为可见。这意味着,即使后续还有匹配的卡片,或者在搜索过程中仍有卡片可见,这个提示也可能被错误地显示出来。

function myFunction() {
  var input, filter, cards, cardContainer, noContent, title, i, cardExist;

  input = document.getElementById("myFilter");
  noContent = document.getElementById("no-content");

  filter = input.value.toUpperCase();
  cardContainer = document.getElementById("myItems");
  cards = cardContainer.getElementsByClassName("blog-card");

  noContent.style.display = "none"; // 每次搜索开始时隐藏提示

  for (i = 0; i < cards.length; i++) {
    title = cards[i].querySelector(".card-title");
    if (title.innerText.toUpperCase().indexOf(filter) > -1) {
      cards[i].style.display = "block";
    } else {
      cards[i].style.display = "none";
      // 错误:在这里直接显示 noContent 可能导致误判
      noContent.style.display = "flex"; 
    }
  }
}

上述代码的问题在于 noContent.style.display = "flex"; 语句被放置在 else 分支内。这意味着只要有一张卡片不匹配,"no-content" 元素就会被显示。如果搜索结果中包含多张卡片,但其中只有一张不匹配,"no-content" 元素也会被错误地显示出来,从而误导用户。

优化后的解决方案

为了确保“无结果”提示仅在确实没有任何卡片匹配搜索条件时才显示,我们需要调整逻辑,采用“先处理所有卡片,再统一判断结果”的策略。

1. HTML 结构

首先,确保你的HTML结构包含一个搜索输入框、一个卡片容器以及一个用于显示“无内容”提示的元素。

  



  
  
    
      

Auto Repair

Dentist

there are no more blog post with this term

注意: no-content 元素在初始状态下应设置为 display: none; 或使用 hidden 属性,以确保它在页面加载时不会显示。

2. CSS 样式

为了美观和功能性,需要为卡片、搜索框和“无内容”提示定义相应的CSS样式。no-content 类的样式确保它在显示时能够居中并具有良好的可读性。

@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@500&family=Open+Sans:wght@700&display=swap');

/* 搜索输入框样式 */
.input-container {
  display: flex;
  justify-content: center;
  width: 100%;
}

.input-container .search-imput {
  margin: 20px;
  width: 50%;
  height: 40px;
  border-radius: 30px;
  border: 2px solid #6F77E9;
}

::placeholder {
  color: #161663;
  font-weight: bold;
  text-align: center;
  font-size: 1em;
}

/* 卡片容器和卡片样式 */
.card-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  flex-direction: row;
  margin: 50px;
  /* width: 100%px;  此处应为 width: 100%; 或删除 px */
}

.blog-card {
  width: 490px;
  height: 470px;
  margin: 15px;
  border-radius: 20px;
  box-shadow: 0px 0px 4px 0px rgba(22, 22, 99, 0.30);
}

/* ... 其他卡片内部元素样式 ... */

/* 无内容提示样式 */
.no-content {
  width: 100vw;
  height: 50vh;
  display: flex; /* 默认设置为 flex,通过 JS 控制显示/隐藏 */
  justify-content: center;
  align-items: center;
}

.no-content h1 {
  font-size: 20px;
  padding: 0px 20px;
  font-family: 'Montserrat', sans-serif;
  color: #161663;
}

3. JavaScript 逻辑

核心优化在于JavaScript函数 myFunction()。我们将采用以下步骤:

  1. 获取所有卡片元素。
  2. 首先隐藏所有卡片,清除上一次搜索的状态。
  3. 根据搜索输入过滤出所有匹配的卡片。
  4. 只显示那些匹配的卡片。
  5. 根据匹配卡片的数量来决定是否显示“无内容”提示。
function myFunction() {
  const input = document.getElementById("myFilter");
  const noContent = document.getElementById("no-content");
  const filter = input.value.toUpperCase();
  const cardContainer = document.getElementById("myItems");
  // 将 HTMLCollection 转换为数组,以便使用 forEach 和 filter 方法
  const cards = Array.from(cardContainer.getElementsByClassName("blog-card"));

  // 步骤 1: 首先隐藏所有卡片,重置显示状态
  cards.forEach(card => card.style.display = "none");

  // 步骤 2: 过滤出所有匹配搜索条件的卡片
  const matchingCards = cards.filter(
    card => card.querySelector(".card-title").innerText.toUpperCase().includes(filter)
  );

  // 步骤 3: 显示所有匹配的卡片
  matchingCards.forEach(card => card.style.display = "block");

  // 步骤 4: 根据匹配卡片的数量,决定是否显示“无内容”提示
  noContent.style.display = (matchingCards.length === 0 ? "flex" : "none"); 
}

关键改进点与最佳实践

  • 分离关注点: 新的逻辑将“隐藏所有卡片”、“查找匹配卡片”、“显示匹配卡片”和“显示/隐藏无内容提示”这几个步骤清晰地分离开来。这使得代码更易于理解和维护。
  • 统一处理: 不再在循环内部逐个判断并显示“无内容”提示,而是先完成所有卡片的显示/隐藏操作,最后根据整体结果统一处理“无内容”提示的可见性。
  • 使用现代JavaScript: Array.from() 将 HTMLCollection 转换为真正的数组,从而可以使用 forEach 和 filter 等数组方法,使代码更简洁、更具表现力。
  • 初始状态: 确保 no-content 元素在页面加载时的初始状态是隐藏的(例如,通过 style="display: none;" 或 CSS 规则)。

总结

通过上述优化,我们解决了动态卡片搜索中“无结果”提示显示不准确的问题。这种“先隐藏全部,再显示匹配,最后判断总数”的策略是处理此类交互功能的标准做法,它不仅提升了用户体验的准确性,也使JavaScript代码更加清晰和易于维护。在构建类似的动态内容过滤功能时,请务必采纳这种分离逻辑和统一判断的思路。