vue 若依 element 搜索框搜索出来的关键字 高亮处理

最终效果:

方法一: 

在getList()查询事件中添加以下代码:

          // 获取搜索框和表格元素
          const searchInput = this.queryParams.name;
          const table = document.querySelector('#data-table'); 
          // 监听搜索框输入事件
          // searchInput.addEventListener('input', () => {
          // 获取搜索关键字
          const keyword = searchInput; 
          // 如果搜索关键字为空,移除所有高亮样式
          if (!keyword) {
            table.querySelectorAll('.highlight').forEach(el => {
              el.classList.remove('highlight');
            });
            return;
          } 
          // 遍历表格中的每个单元格
          table.querySelectorAll('td').forEach(td => {
            // 获取单元格文本内容
            const cellText = td.textContent.trim().toLowerCase();

            // 如果单元格内容包含搜索关键字,添加高亮样式
            if (cellText.includes(keyword)) {
              // 将匹配到的关键字替换为带有高亮样式的文本
              const highlightedText = cellText.replace(new RegExp(keyword, 'gi'), match => `<span class="highlight">${match}</span>`);
              td.innerHTML = highlightedText;
            } else {
              // 移除高亮样式
              td.innerHTML = cellText;
            }
          });
          // });

style:

/deep/ .el-table__row .highlight {
  background: yellow !important;
}

方法二:

// 搜索框
<div @keyup.enter="search">    
	<el-input
        :placeholder="请输入搜索内容"
        prefix-icon="el-icon-search"
        v-model.trim="searchData"
    >
    </el-input>
</div>
// 表格
<el-table :data="searchTableDate" height="auto" style="width: 99%; margin: auto">
    <el-table-column type="index" width="50"> </el-table-column>
    <el-table-column prop="name" :label="文件名">
        <template slot-scope="scope">
            // 核心
            <span  v-html="showData(scope.row.name, searchData)"></span>
        </template>
    </el-table-column>
</el-table>
// 替换关键字
 showData(val, searchData) {
    // 不区分大小写  const Reg = new RegExp(searchData, 'i');
    // 全局替换  const Reg = new RegExp(searchData, 'g');
    const Reg = new RegExp(searchData, 'ig');
    if (val) {
        // 注意 这里推荐使用正则占位符$& 不使用${searchData}  因为当这里使用正则表达式(i)不区分大小写时,如果你的文本是大写,搜索的关键字是小写,匹配结果会替换掉大写的文本
        // const res = val.replace(Reg, `<span style="background-color: yellow;">${searchData}</span>`);
        const res = val.replace(Reg, `<span style="background-color: yellow;">$&</span>`);
        return res;
    }
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要实现在 Vue 中的搜索框中输入多个关键字后,将匹配到的每个关键字高亮显示,可以遵循以下步骤: 1. 在 data 中定义一个变量来保存搜索关键字列表。 2. 在模板中使用 input 元素来渲染搜索框,并为其绑定一个 input 事件来监听输入内容变化。 3. 在模板中渲染需要搜索的内容,并使用 v-html 指令将内容渲染为 HTML。 4. 使用 computed 属性来实现关键字高亮的逻辑。在 computed 中定义一个方法,接收需要搜索的内容和搜索关键字列表,并使用正则表达式将匹配到的关键字用 <span> 标签包裹,从而达到高亮的效果。 以下是一个示例代码: ``` <template> <div> <input type="text" v-model="searchText" @input="highlightText" placeholder="请输入搜索关键字"> <div v-html="highlightedContent"></div> </div> </template> <script> export default { data() { return { searchText: '', keywords: ['Vue', '高亮'], content: '这是一段需要搜索的内容,其中包含关键字 Vue高亮。' } }, computed: { highlightedContent() { let result = this.content this.keywords.forEach(keyword => { const regex = new RegExp(keyword, 'gi') result = result.replace(regex, '<span class="highlight">$&</span>') }) return result } }, methods: { highlightText() { // 触发 computed 中的方法 } } } </script> <style> .highlight { color: red; font-weight: bold; } </style> ``` 在这个示例中,我们在 data 中定义了 searchText、keywords 和 content 三个变量,其中 keywords 是需要搜索关键字列表。在模板中使用 input 元素来渲染搜索框,并使用 v-model 将输入的内容绑定到 searchText 上,在 input 事件中触发 highlightText 方法。使用 v-html 指令将 computed 中返回的内容渲染为 HTML。在 computed 中定义了一个方法 highlightedContent,使用 forEach 循环遍历关键字列表,使用正则表达式将匹配到的关键字用 span 标签包裹。最后在样式中定义了高亮的样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半兽先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值