el-select配合el-tree实现下拉选以及数据回显以及搜索

一、前言

        有时候就会遇到组件配合使用的情况,然后就整理了一下,后面大家需要的话可以直接拿去使用。

二、源码

<template>
  <el-select
    ref="selectTree"
    filterable
    v-model="name"
    :placeholder="'请选择'"
    :filter-method="focus"
    clearable
    @focus="focus('')"
    @clear="clearValue"
  >
    <el-option style="height: auto">
      <el-tree
        ref="tree"
        :data="data"
        :expand-on-click-node="false"
        default-expand-all
        node-key="categoryId"
        :empty-text="'无匹配数据'"
        :props="defaultProps"
        highlight-current
        @node-click="handleNodeClick"
        :filter-node-method="filterNode"
      />
    </el-option>
  </el-select>
</template>

<script>
export default {
  data() {
    return {
      name: '',
      categoryId: null,
      data: [
        {
          id: 1,
          pid: null,
          parentId: 0,
          children: [
            {
              id: 2,
              pid: null,
              parentId: 1,
              children: [
                {
                  id: 8,
                  pid: null,
                  parentId: 2,
                  children: [],
                  postCode: '',
                  postName: '添加1',
                  sort: null,
                  tenantId: 120,
                  status: 1,
                  createTime: '2023-10-30 14:28:16',
                  childPost: null,
                  postType: 2,
                  nodeType: null,
                  postAdmin: 10024
                },
                {
                  id: 9,
                  pid: null,
                  parentId: 2,
                  children: [],
                  postCode: '',
                  postName: '测试添加',
                  sort: null,
                  tenantId: 120,
                  status: 1,
                  createTime: '2023-11-01 15:30:22',
                  childPost: null,
                  postType: 2,
                  nodeType: null,
                  postAdmin: 10022
                },
                {
                  id: 11,
                  pid: null,
                  parentId: 2,
                  children: [],
                  postCode: '',
                  postName: '测试',
                  sort: null,
                  tenantId: 120,
                  status: 1,
                  createTime: '2023-11-02 13:26:24',
                  childPost: null,
                  postType: 2,
                  nodeType: null,
                  postAdmin: 10024
                },
                {
                  id: 13,
                  pid: null,
                  parentId: 2,
                  children: [],
                  postCode: '',
                  postName: '测试111',
                  sort: null,
                  tenantId: 120,
                  status: 1,
                  createTime: '2023-11-02 13:48:44',
                  childPost: null,
                  postType: 2,
                  nodeType: null,
                  postAdmin: 10022
                }
              ],
              postCode: null,
              postName: '人事部门',
              sort: null,
              tenantId: 120,
              status: 1,
              createTime: '2023-10-26 10:55:18',
              childPost: null,
              postType: 2,
              nodeType: null,
              postAdmin: null
            }
          ],
          postCode: null,
          postName: '回集团根节点',
          sort: null,
          tenantId: 120,
          status: 1,
          createTime: '2023-10-26 10:55:18',
          childPost: null,
          postType: 1,
          nodeType: 6,
          postAdmin: 10000
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'postName'
      }
    }
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true
      return data.postName.indexOf(value) !== -1
    },
    // 节点点击事件
    handleNodeClick(data, node, nodeData) {
      this.name = data.postName
      this.categoryId = data.id
      this.$refs.selectTree.blur()
    },

    // 数据集回显
    echoDataSet(val) {
      if (!val) return
      const categoryId = val.id || ''
      this.categoryId = categoryId
      this.$refs.tree.setCurrentKey(categoryId) // 设置节点高亮
    },
    focus(val) {
      this.$refs.tree.filter(val)
    },
    clearValue() {
      this.$refs.tree.setCurrentKey(null)
    }
  }
}
</script>



<style>
.el-select-dropdown__item {
  padding: 0;
}
</style>

关于回显只需要在编辑的时候进行name字段的赋值,以及触发代码中数据回显的方法,传入需要选中的id就可以了。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值