vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > Element UI 搜索关键字高亮

Element UI 表格中实现搜索关键字高亮显示功能

作者:刘 怼怼

在数据展示类应用中,搜索功能是提升用户体验的关键模块,而关键字高亮则能让搜索结果更加直观醒目,本文详解如何在ElementUI中实现表格搜索与关键字高亮,结合树形结构递归处理及CSS样式,提升数据可读性与用户体验,感兴趣的朋友跟随小编一起看看吧

在数据展示类应用中,搜索功能是提升用户体验的关键模块,而关键字高亮则能让搜索结果更加直观醒目。本文将详细介绍如何在 Element UI 框架中实现表格数据的搜索与关键字高亮功能,并结合树形表格的特性提供完整解决方案。

一、功能实现背景与核心需求

当表格中存在大量数据时,用户需要通过搜索快速定位目标内容。关键字高亮的核心价值在于:

结合用户提供的代码,我们需要实现以下核心功能:

  1. 搜索输入框的双向绑定与事件监听
  2. 表格数据中指定列的关键字匹配
  3. 匹配文本的样式高亮处理
  4. 树形表格结构下的递归高亮处理

二、核心代码实现与解析

1. 搜索组件与表格结构

首先看用户提供的基础代码结构,搜索输入框与表格的集成方式:

<el-form-item label="搜索内容:">
  <el-input v-model="searchForm.searchContent" placeholder="请输入搜索内容"></el-input>
</el-form-item>
<div class="table-container">
  <el-table :data="visibleTableData" border style="width: 99%;" max-height="650px"
    :tree-props="{ children: 'details' }" row-key="id" @selection-change="handleSelectionChange">
    <!-- 表格列定义... -->
    <el-table-column align="center" label="资料内容" prop="content">
      <template #default="scope">
        <span v-html="highlightText(scope.row.content, searchKeyword)"></span>
      </template>
    </el-table-column>
    <!-- 其他需要高亮的列... -->
  </el-table>
</div>

关键点解析

2. 关键字高亮核心方法

下面是 highlightText 方法的完整实现,建议添加到 Vue 实例的 methods 中:

methods: {
  // 关键字高亮处理方法
  highlightText(text, keyword) {
    // 处理空值或非字符串类型
    if (!text || typeof text !== 'string') return text;
    if (!keyword || typeof keyword !== 'string') return text;
    // 转义正则表达式特殊字符
    const escapedKeyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    // 创建不区分大小写的全局匹配正则
    const regex = new RegExp(escapedKeyword, 'gi');
    // 使用span包裹匹配内容并添加高亮样式
    return text.replace(regex, (match) => {
      return `<span class="highlight">${match}</span>`;
    });
  },
  // 搜索事件处理
  handleSearch() {
    // 获取搜索关键字
    this.searchKeyword = this.searchForm.searchContent.trim();
    // 执行搜索过滤(此处需根据实际数据结构实现)
    this.filterTableData();
  },
  // 表格数据过滤方法
  filterTableData() {
    if (!this.searchKeyword) {
      // 清空搜索时显示全部数据
      this.visibleTableData = [...this.originalTableData];
      return;
    }
    // 递归过滤树形表格数据(示例逻辑,需根据实际数据结构调整)
    this.visibleTableData = this.originalTableData.filter(item => {
      return this.checkRowMatch(item, this.searchKeyword);
    });
  },
  // 检查行数据是否匹配搜索条件
  checkRowMatch(row, keyword) {
    // 检查当前行需要高亮的字段
    const fields = ['content', 'projectName', 'form', 'receiverName'];
    for (const field of fields) {
      if (row[field] && row[field].toString().includes(keyword)) {
        return true;
      }
    }
    // 递归检查子节点
    if (row.details && row.details.length > 0) {
      for (const child of row.details) {
        if (this.checkRowMatch(child, keyword)) {
          return true;
        }
      }
    }
    return false;
  }
}

3. 高亮样式定制

为了让高亮效果更明显,需要在 CSS 中添加高亮样式:

.highlight {
  background-color: #ffeb3b; /* 黄色背景,可自定义 */
  color: #333;
  font-weight: 500;
  padding: 0 2px;
  border-radius: 2px;
}

三、总结与扩展方向

通过上述方法,我们实现了 Element UI 表格中的关键字高亮功能,核心在于利用正则表达式替换匹配文本,并结合树形结构的递归处理。

到此这篇关于Element UI 表格中实现搜索关键字高亮显示功能的文章就介绍到这了,更多相关Element UI 搜索关键字高亮内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文