树形选择器小组件(element-ui)

<template>
  <div class="hello">
    <el-select
      class="main-select-tree"
      ref="selectTree"
      v-model="selectValue"
    >
      <el-option
        v-for="item in optionData(options_D)"
        :key="item.id"
        :label="item.name"
        :value="item.id"
        style="display: none"
      />

      <el-tree
        class="main-select-el-tree"
        ref="selectelTree"
        :data="options_D"
        :props="treeProps"
        highlight-current
        @node-click="handleNodeClick"
        :expand-on-click-node="expandOnClickNode"
        default-expand-all
      />
    </el-select>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      expandOnClickNode: true,
      selectValue: '',
      treeProps: {
        children: 'children',
        label: 'name',
      },
      options_D: [
        {
          id: 1,
          name: 'test1',
          children: [
            {
              id: 11,
              name: 'test1-1',
              children: [
                {
                  id: 111,
                  name: 'test1-1-1',
                  children: [],
                },
                {
                  id: 112,
                  name: 'test1-1-2',
                  children: [],
                },
              ],
            },
            {
              id: 21,
              name: 'test2-1',
              children: [
                {
                  id: 111,
                  name: 'test2-1-1',
                  children: [],
                },
              ],
            },
          ],
        },
      ],
    };
  },
  methods: {
    handleNodeClick(node) {
      this.selectValue = node.name;
      // this.ruleForm.deptId = node.id;
      this.$refs.selectTree.blur();
    },
    optionData(array, result = []) {
      array.forEach((item) => {
        result.push({ label: item.name, value: item.id });
        if (item.children && item.children.length !== 0) {
          this.optionData(item.children, result);
        }
      });
      return JSON.parse(JSON.stringify(result));
    },
  },
};
</script>

<style scoped></style>

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值