vue自定义实现树状下拉选择器

<template>
  <el-select clearable ref="selectTree" v-model="selectValue" :placeholder="placeholder" popper-class="popperTreeSelect">
    <el-option
        v-for="item in tree"
        :label="item[label]"
        :value="item[nodeKey]"
        :key="item[nodeKey]"
        style="display: none;"/>
    <el-tree
        ref="tree"
        :data="tree"
        :node-key="nodeKey"
        :props="{children:children,label:label}"
        @node-click="handleNodeClick"
    >
    </el-tree>
  </el-select>
</template>

<script>
export default {
  name: "selectTree",
  model:{
    prop:'value',
    event:'input'
  },
  props:{
    value:{
      type:[String,Number],
      default:''
    },
    tree:{
      type:Array,
      default: function () {
        return []
      }
    },
    children:{
      type:String,
      default:'children'
    },
    label:{
      type:String,
      default:'label'
    },
    placeholder:{
      type:String,
      default:'请选择'
    },
    nodeKey:{
      type:String,
      default:'id'
    }
  },
  data () {
    return {
      selectValue:''
    }
  },
  methods:{
    handleNodeClick(data, node, b){
      this.$emit('input', data.id);
      this.$emit('change', data)
      this.selectValue = data.name;
      this.$refs.selectTree.blur();
    },
    getValueName(arr){
      arr.map(item => {
        if(item[this.nodeKey] == this.value){
          console.log( item[this.label])
          this.selectValue = item[this.label]
          return false
        }
        if(item[this.children] && item[this.children].length > 0){
          this.getValueName(item[this.children])
        }
      })
    }
  },
  watch:{
    tree:{
      handler(newDate){
        this.getValueName(newDate)
      },
      immediate:true
    }
  }
}
</script>

<style scoped>

</style>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容