el-select-tree:可搜索下拉选择树

基于vue2.6+ElementUI实现的可搜索下拉选择树

效果图

在这里插入图片描述

使用实示例
<el-form-item label="上级单位" prop="parentId">
    <el-select-tree v-if="parentOpt.length>0" :list="parentOpt" v-model="form.parentId" @change="parentChange" placeholder="请选择上级单位" />
 </el-form-item>
parentChange(data: any) {
	//data为当前选中对象
      console.log(data)
}

组件代码

html
<template>
  <div class="selectTree">
    <el-select ref="elSelect" v-model="newVal" :placeholder="placeholder" filterable :filter-method="filterTree" clearable @clear="clear" popper-append-to-body>
      <el-option :value="newVal" :label="label" class="hasTree" disabled>
        <el-tree
          :data="openSearch ? searchData : list"
          ref="tree"
          node-key="value"
          :expand-on-click-node="false"
          :default-expanded-keys="defaultExpandedKeys"
          :current-node-key="newVal"
          highlight-current
          check-on-click-node
          auto-expand-parent
          @node-click="handleNodeClick"
        ></el-tree>
      </el-option>
    </el-select>
  </div>
</template>
js
<script>
export default {
  name: 'ElSelectTree',
  props: {
    value: {
      type: String,
      require: true,
    },
    list: {
      //树状列表:{label:'',value:'',children:[]}
      type: Array,
      default: () => {
        return [];
      },
    },
    placeholder: {
      type: String,
      default: '请选择',
    },
  },

  data() {
    return {
      newVal: '', //本页面选中的数据
      label: '',
      selectTree: {},
      defaultExpandedKeys: [], //默认展开
      searchData: [], //带搜索数据
      openSearch: false, //是否开启搜索
    };
  },

  watch: {
    value: {
      handler(value) {
        this.newVal = value;
        if (value) {
          this.defaultExpandedKeys = [value];
          this.$refs.tree.setCurrentKey(value); //设置默认选择
          const check = this.findLabel(this.list, value);
          if (check) {
            this.label = check.label;
          }
        }
      },
      deep: true, // 深度监听
      immediate: true, //首次触发
    },
    newVal(value) {
      const check = this.findLabel(this.list, value);
      if (check) {
        this.label = check.label;
      }
      this.$emit('input', value);
    },
  },
  created() {
    // el-select 中加了filterable 点击箭头回收不去问题
    Object.getPrototypeOf(this.$options.components).ElSelect.options.methods.handleFocus = () => {};
  },
  mounted() {},
  methods: {
    //节点选择事件
    handleNodeClick(data) {
      this.newVal = data.value;
      this.label = data.label;
      this.$refs.elSelect.blur();
      this.searchData = [];
      this.openSearch = false;
      this.$emit('change', data);
    },
    //筛选树
    async filterTree(value) {
      if (value) {
        this.openSearch = true;
        this.searchData = [];
        await this.findItem(this.list, value);
      } else {
        this.openSearch = false;
        this.searchData = [];
      }
    },
    //递归筛选,查询时用
    findItem(arr, value) {
      return new Promise(resolve => {
        for (let i = 0; i < arr.length; i++) {
          const item = arr[i];
          if (item.label.includes(value)) {
            this.searchData.push(item);
          } else if (item.children && item.children.length > 0) {
            this.findItem(item.children, value);
          }
        }
        resolve(true);
      });
    },
    //递归筛选,回显label
    findLabel(arr, value) {
      for (let i = 0; i < arr.length; i++) {
        const item = arr[i];
        if (item.value === value) {
          return item;
        } else if (item.children && item.children.length > 0) {
          const result = this.findLabel(item.children, value);
          if (result) {
            return result;
          }
        }
      }
    },
    //清空事件
    clear() {
      this.$refs.tree.setCurrentKey(null); //清空高亮
      this.label = '';
      this.newVal = '';
      this.searchData = [];
      this.openSearch = false;
      this.$emit('change', {});
    },
  },
};
</script>
css
<style scoped lang="scss">
.selectTree {
  display: inline-block;
  width: 100%;
}
.el-select {
  width: 100%;
}
.hasTree {
  padding: 0 !important;
  margin: 0;
  overflow: auto;
  line-height: normal;
  height: auto;
  cursor: default !important;
  font-weight: 500 !important;
  ::v-deep .el-tree-node__content {
    height: 34px;
    line-height: 34px;
  }
  ::v-deep .el-tree-node__label {
    font-size: 14px !important;
  }
}
</style>

附带平级转树级方法

http://t.csdnimg.cn/I2x3t

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用 `el-select` 和 `el-tree` 配合使用来实现形结构下拉单选。 首先,需要将数据转换为 `el-tree` 的节点格式,例如以下数据: ```javascript const treeData = [ { id: 1, label: '节点1', children: [ { id: 2, label: '节点1-1' }, { id: 3, label: '节点1-2' } ] }, { id: 4, label: '节点2', children: [ { id: 5, label: '节点2-1' }, { id: 6, label: '节点2-2' } ] } ] ``` 需要转换为 `el-tree` 的节点格式: ```javascript const treeNodes = [ { id: 1, label: '节点1', children: [ { id: 2, label: '节点1-1', isLeaf: true }, { id: 3, label: '节点1-2', isLeaf: true } ] }, { id: 4, label: '节点2', children: [ { id: 5, label: '节点2-1', isLeaf: true }, { id: 6, label: '节点2-2', isLeaf: true } ] } ] ``` 其中,每个节点需要添加 `isLeaf` 属性,用于标记该节点是否为叶子节点。 接着,在 `el-select` 中使用 `el-tree` 作为下拉选项。代码示例: ```html <template> <el-select v-model="selectedItem" placeholder="请选择" clearable> <el-tree :data="treeNodes" :props="treeProps" :node-key="treeProps.id" :highlight-current="true" :default-expand-all="true" :expand-on-click-node="false" v-if="treeVisible"></el-tree> <el-option v-else v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option> </el-select> </template> <script> export default { data() { return { selectedItem: null, treeNodes: [ { id: 1, label: '节点1', children: [ { id: 2, label: '节点1-1', isLeaf: true }, { id: 3, label: '节点1-2', isLeaf: true } ] }, { id: 4, label: '节点2', children: [ { id: 5, label: '节点2-1', isLeaf: true }, { id: 6, label: '节点2-2', isLeaf: true } ] } ], treeProps: { children: 'children', label: 'label', isLeaf: 'isLeaf' }, options: [ { label: '选项1', value: '1' }, { label: '选项2', value: '2' }, { label: '选项3', value: '3' } ], treeVisible: false } }, watch: { selectedItem(val) { if (val && val.children && val.children.length > 0) { this.treeVisible = true } else { this.treeVisible = false } } } } </script> ``` 上述代码中,`el-select` 的下拉选项分为两部分:`el-tree` 和 `el-option`。根据当前选中的节点是否有子节点,判断显示 `el-tree` 还是 `el-option`。 在 `el-tree` 中,需要设置 `props` 属性,指定节点数据中的属性名,以及 `node-key` 属性,用于标识节点的唯一属性名。其他属性根据实际需求进行设置。 在 `watch` 中,根据当前选中的节点是否有子节点,控制 `el-tree` 的显示与隐藏。 注意:以上代码只实现了形结构下拉单选的基本功能,还需要根据实际需求进行修改和完善。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值