element ui 树标签处理搜索过滤展示子节点

HTML

html 复制代码
    <div>
      <el-drawer
        size="50%"
        title="选择标签"
        :visible.sync="isShowDrawer"
        :before-close="handleCloseDrawer"
      >
        <div style="padding:15px">
          <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>

          <el-tree
            style="margin: 20px 0"
            highlight-current
            node-key="id"
            show-checkbox
            check-on-click-node
            class="filter-tree"
            :data="dataTag"
            :props="defaultProps"
            :filter-node-method="filterNode"
            :default-checked-keys="defaultCheck"
            ref="tree"
          ></el-tree>

          <div>
            <el-button size="small" type="primary" @click="onSave">保存</el-button>
            <el-button size="small" @click="handleCloseDrawer">取消</el-button>
          </div>
        </div>
      </el-drawer>
    </div>

JS

javascript 复制代码
 // 标签过滤方法
    filterNode(value, data, node) {
      if (!value) return true
      let one = data.label.indexOf(value) !== -1
      let two =
        node.parent &&
        node.parent.data &&
        node.parent.data.label &&
        node.parent.data.label.indexOf(value) !== -1
      let three =
        node.parent &&
        node.parent.parent &&
        node.parent.parent.data &&
        node.parent.parent.data.label &&
        node.parent.parent.data.label.indexOf(value) !== -1
      let four =
        node.parent &&
        node.parent.parent &&
        node.parent.parent.parent &&
        node.parent.parent.parent.data &&
        node.parent.parent.parent.data.label &&
        node.parent.parent.parent.data.label.indexOf(value) !== -1
      let result_one = false
      let result_two = false
      let result_three = false
      let result_four = false
      if (node.level === 1) {
        result_one = one
      } else if (node.level === 2) {
        result_two = one || two
      } else if (node.level === 3) {
        result_three = one || two || three
      } else if (node.level === 4) {
        result_three = one || two || three || four
      }
      return result_one || result_two || result_three || result_four
    },
    handleClose(index) {
      this.ruleFormData.tagList.splice(index, 1)
    },

    handleSelectTags() {
      this.defaultCheck = this.ruleFormData.tagList.map(i => i.id)
      this.isShowDrawer = true
    },
    onSave() {
      let tags = this.$refs.tree.getCheckedNodes()
      if (tags.length == 0) {
        return this.$message.error('请选择标签')
      }
      this.ruleFormData.tagList = tags
        .filter(item => !item.hasOwnProperty('children'))
        .map(item => {
          return {
            ...item,
            tagId: item.id,
            tagValue: item.label
          }
        })

      this.handleCloseDrawer()
    },
    handleCloseDrawer() {
      this.$refs.tree.setCheckedKeys([])
      this.filterText = ''
      this.defaultCheck = []
      this.isShowDrawer = false
    },

CSS

css 复制代码
/deep/ .el-tree-node.is-expanded > .el-tree-node__children {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

/deep/ .el-tree-node__label {
  font-size: 16px;
}

.image-tip {
  font-size: 12px;
  color: #606266;
}

/deep/ .el-tree-node {
  margin: 10px 0;
}

// /deep/.el-tree-node {
//   .is-leaf + el-checkbox .el-checkbox__inner {
//     display: inline-block;
//   }
// }
// .el-checkbox .el-checkbox__inner {
//   display: none;
// }
相关推荐
程序员爱技术1 分钟前
Vue 2 + JavaScript + vue-count-to 集成案例
前端·javascript·vue.js
悦涵仙子1 小时前
CSS中的变量应用——:root,Sass变量,JavaScript中使用Sass变量
javascript·css·sass
兔老大的胡萝卜1 小时前
ppk谈JavaScript,悟透JavaScript,精通CSS高级Web,JavaScript DOM编程艺术,高性能JavaScript pdf
前端·javascript
cs_dn_Jie5 小时前
钉钉 H5 微应用 手机端调试
前端·javascript·vue.js·vue·钉钉
开心工作室_kaic5 小时前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿5 小时前
webWorker基本用法
前端·javascript·vue.js
customer086 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
清灵xmf6 小时前
TypeScript 类型进阶指南
javascript·typescript·泛型·t·infer
小白学大数据6 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
qq_390161777 小时前
防抖函数--应用场景及示例
前端·javascript