树形el-select封装

<template>
<!-- 
    v-model的值(selectKey)为当前被选中的el-option的 value 属性值
 -->
   <el-select
    v-model="selectKey"
    multiple
    placeholder="请选择"
    :popper-append-to-body="true"
    :collapse-tags='isShowMultipleTags'
    clearable
    :style="styles"
    @remove-tag="removetag"
    @clear="clearall"
  >
    <el-option :value="selectTree" class="setstyle" disabled>
      <el-tree
        :ref="`tree_${id}`"
        :data="options"
        :props="defaultProps"
        show-checkbox
        :check-strictly="false"
        :expand-on-click-node="true"
        node-key="id"
        :check-on-click-node="false"
        @check="handleNodeClick"
      />
    </el-option>
  </el-select>
</template>

<script>
import { flatten } from '@/views/component/planTemplateMangement/utils/index.js'
export default {
 props: {
    // 多选时是否将选中值按文字的形式展示
    isShowMultipleTags:{
        type: Boolean,
        default: true
    },
    options: {
      type: Array,
      default() {
        return []
      }
    },
    // 用于ref
    id: {
      type: String,
      default: ''
    },
    // 父组件选中的值,就是用于回显
    echoValue: {
      type: [Array, String],
      default() {
        return []
      },
      required: true
    },
    // el-select样式
    styles: {
      type: String,
      default: ''
    }
  },
  emit: ['on-change'],
  data() {
    return {
      selectKey: this.echoValue,
      selectTree: [], // el-select选中的值
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  computed: {
    optionsFlatten() {
      return flatten(this.options)
    }
  },
  watch: {
    echoValue(newVal) {
      this.selectKey = newVal
    },
    selectKey(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.$emit('input', this.selectKey)
        this.$emit('on-change')
      }
    }
  },
  mounted() {
    if (this.echoValue.length !== this.selectTree.length) {
      this.keepState()
    }
  },
  updated() {
    if (this.echoValue.length !== this.selectTree.length) {
      this.keepState()
    }
  },
  methods: {
    handleNodeClick() {
      const datalist = this.$refs[`tree_${this.id}`].getCheckedNodes()
      this.selectTree = []
      this.selectKey = []
      datalist.forEach(item => {
        if (!item.children) {
          this.selectTree.push({ id: item.id, label: item.label })
          this.selectKey.push(item.label)
        }
      })
    },
    removetag() {
      this.selectTree.splice(0, 1)
      this.$nextTick(() => {
        this.$refs[`tree_${this.id}`].setCheckedNodes(this.selectTree)
      })
    },
    clearall() {
      this.selectTree = []
      this.$nextTick(() => {
        this.$refs[`tree_${this.id}`].setCheckedNodes([])
      })
    },
    // 当value与selectTree不一致时,强行将value的值赋给selectTree,注id一定要与初始options一致
    keepState() {
        // 先暂时注销,因为发生栈溢出
        //   this.selectTree = this.optionsFlatten.filter(
        //     item => this.value.includes(item.label) && !item.children
        //   )
    // 通过node设置 setCheckedNodes 设置目前勾选的节点
      this.$nextTick(() => {
        this.$refs[`tree_${this.id}`].setCheckedNodes(this.selectTree)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.setstyle {
  min-height: 200Px;
  padding: 0 !important;
  margin: 0;
  overflow: auto;
  cursor: default !important;
  ::v-deep .el-tree-node__label {
    font-size: 14Px;
  }
}
</style>

JS文件


// 将包含children对象的多维数组扁平化
export const flatten = arr => {
    const stack = [...arr];
    const result = [];
  
    while (stack.length) {
      const next = stack.pop();
      if (Array.isArray(next.children)) {
        stack.push(...next.children);
      }
      result.push(next);
    }
    return result;
  }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值