element Cascader +tree融合完美解决方案

 element-ui Cascader 组件官方地址Element - The world's most popular Vue UI framework

 

该组件,运用在现实场景中依旧不满足甲方的胃口。

改呀~改呀~改呀~改呀~改呀~改呀~改呀~改呀~改呀~改呀~改呀~改呀~改呀~改呀~改呀~改呀!

最终效果

代码实现

<template>
 <div>
   <el-select
     :title="multiple? optionData.name : ''"
     ref="select"
     :value="value"
     :placeholder="placeholder"
     clearable
     :disabled="disabled"
     :filterable="filterable"
     :filter-method="filterMethod"
     style="width: 100%;"
     @clear="clear"
     @visible-change="visibleChange"
   >
     <el-option
       ref="option"
       class="tree-select__option"
       :value="optionData.id"
       :label="optionData.name"
     >
       <el-tree
         style="max-height: 150px;overflow: auto"
         ref="tree"
         class="tree-select__tree"
         :class="`tree-select__tree--${multiple ? 'checked' : 'radio'}`"
         :node-key="nodeKey"
         :data="data"
         :props="props"
         :default-expanded-keys="[value]"
         :show-checkbox="multiple"
         :highlight-current="!multiple"
         :expand-on-click-node="multiple"
         :filter-node-method="filterNode"
         @node-click="handleNodeClick"
         @check-change="handleCheckChange"
       ></el-tree>
     </el-option>
   </el-select>
 </div>
</template>

<script>
export default {
  name: 'TreeSelect',
  props: {
    placeholder:{
      type:String
    },
    // v-model绑定
    value: {
      type: [String, Number],
      default: ''
    },
    multiple: {
      type: Boolean,
      default: true
    },
    // 树形的数据
    data: {
      type: Array,
      default: function () {
        return []
      }
    },
    // 每个树节点用来作为唯一标识的属性
    nodeKey: {
      type: [String, Number],
      default: 'id'
    },
    filterable: {
      type: Boolean,
      default: true
    },
    disabled: {
      type: Boolean,
      default: false
    },
    // tree的props配置
    props: {
      type: Object,
      default: function () {
        return {
          label: 'deptName',
          children: 'children'
        }
      }
    }
  },
  data() {
    return {
      optionData: {
        id: '',
        name: ''
      },
      filterFlag: false
    }
  },
  watch: {
    value: {
      handler(val) {
        if (!this.isEmpty(this.data)) {
          this.init(val)
        }
      },
      immediate: true
    },
    data: function (val) {
      if (!this.isEmpty(val)) {
        this.init(this.value)
      }
    }
  },
  created() {},
  methods: {
    // 是否为空
    isEmpty(val) {
      for (let key in val) {
        return false
      }
      return true
    },
    handleNodeClick(data) {
      if (this.multiple) {
        return
      }
      this.$emit('input', data[this.nodeKey])
      this.$refs.select.visible = false
    },
    handleCheckChange() {
      let arr=[]
      const nodes = this.$refs.tree.getCheckedNodes()
      nodes.map((item) => arr.push(item[this.nodeKey]))
      const value = nodes.map((item) => item[this.nodeKey]).join(',')
      this.$emit('input', arr)
    },
    init(val) {
      // 多选
      if (this.multiple) {
        const arr = val
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys(arr)
          const nodes = this.$refs.tree.getCheckedNodes()
          this.optionData.id = val
          this.optionData.name = nodes
            .map((item) => item[this.props.label])
            .join(',')
        })
      }
      // 单选
      else {
        val = val === '' ? null : val
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(val)
          if (val === null) {
            return
          }
          const label = this.props.label || 'name'
          const node = this.$refs.tree.getNode(val)
          this.optionData.id = val
          this.optionData[label] = node.label
        })
      }
      this.flag=true
    },
    visibleChange(e) {
      if (e) {
        const tree = this.$refs.tree
        this.filterFlag && tree.filter('')
        this.filterFlag = false
        let selectDom = null
        if(this.multiple) {
          selectDom = tree.$el.querySelector('.el-tree-node.is-checked')
        } else {
          selectDom = tree.$el.querySelector('.is-current')
        }
        setTimeout(() => {
          this.$refs.select.scrollToOption({ $el: selectDom })
        }, 0)
      }
    },
    clear() {
      this.$emit('input', [])
    },
    filterMethod(val) {
      this.filterFlag = true
      this.$refs.tree.filter(val)
    },
    filterNode(value, data) {
      if (!value) return true
      const label = this.props.label || 'name'
      return data[label].indexOf(value) !== -1
    }
  }
}
</script>

<style lang="scss">
.tree-select__option {
  &.el-select-dropdown__item {
    height: auto;
    line-height: 1;
    padding: 0;
    background-color: #fff;
  }
}

.tree-select__tree {
  color: #333333;
  padding: 4px 20px;
  font-weight: 400;
  &.tree-select__tree--radio {
    .el-tree-node.is-current > .el-tree-node__content {
      font-weight: 700;
    }
  }
}
</style>

 标签引用

 <els-tree-select :placeholder="'请选择数据'" :multiple="true"  v-model="dataForm.datas"  :data="Options"></els-tree-select>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值