vue el-select的下拉树形结构 带检索功能

el-select的下拉框内容改造成树结构

<template>
  <div>
    <el-select
      class="undergroundMap_top_select"
      v-model="filter.keyword"
      clearable
      placeholder="请选择"
      @clear="handleClear"
      @focus="blurInput"
      ref="treeSelect"
      filterable
      :filter-method="filterMethod"
    >
      <el-option
        hidden
        key="upResId"
        :value="filter.keyword"
        :label="filter.upResName"
      >
      </el-option>
      <el-tree
        :data="treeData"
        node-ket="nodeId"
        :props="defaultProps"
        :expand-on-click-node="false"
        :check-on-click-node="true"
        @node-click="handleNodeClick"
        ref="selecteltree"
        :filter-node-method="filterNode"
      >
        <span
          class="custom-tree-node"
          :title="node.label"
          slot-scope="{ node, data }"
        >
          <span :class="filter.keyword == data.nodeId ? 'name_active' : ''">
            {{ data.nodeName }}
          </span>
        </span>
      </el-tree>
    </el-select>
  </div>
</template>
<script>
export default {
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "nodeName",
      },
      treeData: [
        {
          nodeId: 1,
          nodeName: "节点1",
          children: [
            {
              nodeId: 11,
              nodeName: "节点1-1",
            },
            {
              nodeId: 12,
              nodeName: "节点1-2",
              children: [
                {
                  nodeId: 121,
                  nodeName: "节点1-2-1",
                },
                {
                  nodeId: 122,
                  nodeName: "节点1-2-2",
                },
              ],
            },
          ],
        },
      ],
      filter: {
        keyword: "",
        upResName: "",
      },
    };
  },
  methods: {
    filterMethod(value) {
      this.$refs["selecteltree"].filter(value.trim());
    },
    blurInput() {
      this.$refs["selecteltree"].filter("");
    },
    filterNode(value, data, node) {
      if (!value) return true;
      let parentNode = node.parent,
        labels = [node.label],
        level = 1;
      while (level < node.level) {
        labels = [...labels, parentNode.label];
        parentNode = parentNode.parent;
        level++;
      }
      return labels.some((label) => label.indexOf(value) !== -1);
      // if (data.video_code) return data.catalog_name.indexOf(value) !== -1;
    },
    // 节点点击事件
    handleNodeClick(data) {
      console.log("选择", data);
      this.filter.upResName = data.nodeName;
      this.filter.keyword = data.nodeId;
      this.$refs.treeSelect.blur(); // 失去焦点 隐藏下拉
    },
    // 选择器配置可以清空选项,用户点击清空按钮时触发
    handleClear() {
      this.filter.upResName = "";
      this.filter.keyword = "";
    },
  },
};
</script>
<style lang="scss" scoped>
.name_active {
  color: #358efe;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值