树形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;
  }

 

相关推荐

  1. 树形el-select封装

    2024-03-22 20:28:05       42 阅读
  2. el-select二次封装实现可分页加载数据

    2024-03-22 20:28:05       61 阅读
  3. el-select回显

    2024-03-22 20:28:05       65 阅读
  4. el-tree 树形控件

    2024-03-22 20:28:05       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-22 20:28:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-22 20:28:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-22 20:28:05       82 阅读
  4. Python语言-面向对象

    2024-03-22 20:28:05       91 阅读

热门阅读

  1. Element Plus 文本域设置固定行数

    2024-03-22 20:28:05       40 阅读
  2. trpc-go 博客系统

    2024-03-22 20:28:05       41 阅读
  3. 使用docker搭建docker-osx

    2024-03-22 20:28:05       44 阅读
  4. 蓝桥杯 蓝肽子序列

    2024-03-22 20:28:05       48 阅读
  5. echarts数据下钻如何配置

    2024-03-22 20:28:05       43 阅读
  6. 开灯问题 C语言

    2024-03-22 20:28:05       45 阅读
  7. RabbitMQ如何实现延迟消息?

    2024-03-22 20:28:05       47 阅读
  8. 算法练习第三十天|两道hard51. N 皇后、37. 解数独

    2024-03-22 20:28:05       45 阅读
  9. C语言判断回文数

    2024-03-22 20:28:05       44 阅读
  10. 321——美团一面

    2024-03-22 20:28:05       42 阅读
  11. 【PMP】每日一练2

    2024-03-22 20:28:05       35 阅读