ivew组件使用Vue+Select+Tree实现单选

实现的功能如图,Select实现tree的单选,默认使用option选择全部。
在这里插入图片描述

选择tree的数据后,关闭选择器。
在这里插入图片描述

话不多说上代码,
这里是用的View Design+vue实现的~

<template>
  <div class="my-tree">
    <FormItem :label="companyName">
      <Select v-model="companyTree" :id="setTreeId" @on-change="changeOpt">
        <Option
          :key="treeId"
          :value="treeId"
          :label="treeName"
          style="height: auto"
          hidden
        />
        <Option value="ALL">全部</Option>
        <Tree :data="treeData" @on-select-change="selectTreeData" />
      </Select>
    </FormItem>
  </div>
</template>

<script>
export default {
  name: "MyTree",
  data() {
    return {
      treeData: [], //树的数据
      treeId: "", //树回显Id
      treeName: "", //树回显名称
      companyTree: "ALL"
    };
  },
  props: {
    //名字
    companyName: {
      type: String,
      default: () => {
        return "树形下拉选择框";
      }
    },
    //一个页面多个下拉框,唯一id
    setTreeId: {
      type: String,
      default: "selectTree"
    }
  },
  created() {
    this.treeData = [
      {
        title: "四川省",
        id: "sichuan",
        expand: true,
        children: [
          {
            title: "成都市",
            id: "chengdu",
            expand: true,
            children: [
              {
                title: "高新区",
                id: "gaoxin"
              },
              {
                title: "金堂",
                id: "jitang"
              }
            ]
          },
          {
            title: "资阳市",
            id: "ziyang",
            expand: true,
            children: [
              {
                title: "安岳县",
                id: "anyue"
              }
            ]
          }
        ]
      }
    ];
  },
  methods: {
    selectTreeData(val) {
        if (!val[0].id) {
          return;
        }
        this.treeName = val[0].title;
        this.treeId = val[0].id;
        this.companyTree = val[0].id;
  
      console.log(val);
      this.$emit("treeVal", val);

      //下拉选择后关闭选择的树
      let selectTree = document.getElementById(`${this.setTreeId}`);
      selectTree.lastElementChild.style.display = "none";
    },
    changeOpt(val) {
      this.$emit("treeVal", val);
    }
  }
};
</script>

<style lang='less' scoped>
.my-tree {
}
</style>

看打印结果
在这里插入图片描述

最后父组件里面用@treeVal方法接接收传过来的值就可以

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jet_closer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值