vue+vxe-table实现前端查询树结构查询的关键字变色

查询框输入要查询的关键字以后点击查找
展示查找的元素并且将关键字置红

在这里插入图片描述在这里插入图片描述

这边我使用的是vex-table的树形表格中=>实现树结构深层查找

<template>
  <a-card>
    <a-row :gutter="16">
      <a-col :span="5">
        <a-row>
          <a-col span="20">
            <a-input placeholder="名称" size="small" allowClear v-model="filterTitle">
              <a-icon slot="prefix" type="search" />
            </a-input>
          </a-col>
          <a-col span="4">
            <vxe-button size="mini" @click="setTreeExpand" style="height: 24px;">查找</vxe-button>
          </a-col>
        </a-row>
        <vxe-table ref="xTree" size="mini" max-height="600" highlight-hover-row :loading="loading" :data="tableData" :tree-config="{children: 'children',line: true, iconOpen: 'fa fa-caret-down', iconClose: 'fa fa-caret-right'}">
          <vxe-table-column field="name" title="名称" tree-node>
            <template v-slot="{ row }" style="height:22px">
              <span>
              //给元素加了图标
                <template v-if="row.children && row.children.length">
                  <i class="tree-node-icon fa" :class="$refs.xTree.isTreeExpandByRow(row) ? 'fa-folder-open-o' : 'fa-folder-o'"></i>
                </template>
                <template v-else>
                  <i class="tree-node-icon fa fa-file-o"></i>
                </template>
                <span style="margin-left: 4px;">
                  <a-button type="link" v-html="row.name"></a-button>
                </span>
              </span>
            </template>
          </vxe-table-column>
        </vxe-table>
      </a-col>
    </a-row>
  </a-card>
</template>
import XEUtils from 'xe-utils'
export default {
  data() {
    return {
      filterTitle: '',
      loading: false,
      originData: [],
      tableData: [],
    }
  },
  created() {
    this.loading = true
    setTimeout(() => {
      const list = [
        { id: 1, name: '从入门到放弃1' },
        {
          id: 2,
          name: '从入门到放弃2',
          children: [
            { id: 3, name: '从入门到放弃2-1' },
            { id: 4, name: '从入门到放弃2-2' },
            {
              id: 5,
              name: '从入门到放弃2-3',
              children: [
                {
                  id: 6,
                  name: '从入门到放弃2-3-1',

                  children: [{ id: 7, name: '从入门到放弃2-3-1-1' }],
                },
                { id: 8, name: '从入门到放弃2-3-2' },
                { id: 9, name: '从入门到放弃2-3-3' },
              ],
            },
          ],
        },
        { id: 11, name: '从入门到放弃3' },
        { id: 11, name: '从入门到放弃4' },
      ]
      this.loading = false
      this.originData = list
      this.tableData = this.originData
    }, 300)
  },
  methods: {
    setTreeExpand() {
      // 查找关键字展开与之相关的所有节点,其他无关系的节点关闭掉
      //组件本身的事件,使用了xe-utils中的searchTree方法,从树结构中根据回调查找数据 
      const filterName = XEUtils.toString(this.filterTitle).trim().toLowerCase()
      if (filterName) {
        const options = { children: 'children' }
        const searchProps = ['name']
        this.tableData = XEUtils.searchTree(
          this.originData,
          (item) => searchProps.some((key) => XEUtils.toString(item[key]).indexOf(filterName) > -1),
          options
        )
        //替换查找关键字的样式
        //这里我用了xe-utils中的eachTree方法,从树结构中遍历数据的键、值、路径,遍历数据中的name字段,将其关键字替换成有红色样式的
        this.$nextTick(() => {
          this.$refs.xTree.setAllTreeExpand(true)
        })
        let replaceReg = new RegExp(filterName, 'g')
        let replaceString = `<span class="search-text" >${filterName}</span>`
        XEUtils.eachTree(this.tableData, (item) => (item.name = item.name.replace(replaceReg, replaceString)))
      } else {
        this.tableData = this.originData
      }
    },
  },
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值