vant:基于vant的radio组件封装自己的级联组件(Cascader)

一、效果

二、Cascader组件

js 复制代码
<template>
  <div class="demo">
    <div class="top">
      <ul>
        <li @click="one">回到顶级</li>
        <li v-for="item of parents" :key="item.value" @click="jump(item)">
          {{ item.text }}
        </li>
      </ul>
      <van-button type="primary" @click="submit" size="mini">确定</van-button>
    </div>
    <van-radio-group v-model="radio">
      <div v-for="item of list" :key="item.value">
        <van-radio :name="item.value"></van-radio
        ><span @click="next(item)">{{ item.text }}</span>
      </div>
    </van-radio-group>
  </div>
</template>
<script>
export default {
  data() {
    return {
      radio: "",
      tree: [
        {
          value: "一级 1",
          text: "一级 1",
          children: [
            {
              value: "二级 1-1",
              text: "二级 1-1",
              children: [
                {
                  value: "三级 1-1-1",
                  text: "三级 1-1-1",
                  children: [
                    { value: "四级 1-1-1-1", text: "四级 1-1-1-1" },
                    { value: "四级 1-1-1-2", text: "四级 1-1-1-2" },
                  ],
                },
                { value: "三级 1-1-2", text: "三级 1-1-2" },
              ],
            },
            {
              value: "二级 1-2",
              text: "二级 1-2",
              children: [
                { value: "三级 1-2-1", text: "三级 1-2-1" },
                { value: "三级 1-2-2", text: "三级 1-2-2" },
              ],
            },
          ],
        },
        {
          value: "一级 2",
          text: "一级 2",
          children: [
            {
              value: "二级 2-1",
              text: "二级 2-1",
              children: [
                { value: "三级 2-1-1", text: "三级 2-1-1" },
                { value: "三级 2-1-2", text: "三级 2-1-2" },
              ],
            },
            { value: "二级 2-2", text: "二级 2-2" },
          ],
        },
        {
          value: "一级 3",
          text: "一级 3",
          children: [
            { value: "二级 3-1", text: "二级 3-1" },
            { value: "二级 3-2", text: "二级 3-2" },
          ],
        },
      ],
      list: [], // 本级节点
      parents: [], // 所有的父级节点
    };
  },
  created() {
    this.list = this.tree;
  },
  methods: {
    submit() {
      console.log("选中的节点", this.radio);
    },
    one() {
      this.list = this.tree;
      this.parents = [];
      this.radio = "";
    },
    jump(item) {
      console.log(item.children, this.list);
      if (JSON.stringify(item.children) !== JSON.stringify(this.list)) {
        this.radio = "";
        this.list = item.children;
        // 设置面包屑导航
        const parents = this.getParents(this.tree, item.value);
        this.parents = parents.reverse();
      } else {
        alert("当前已经是" + item.text);
      }
    },
    next(item) {
      if (item.children) {
        this.radio = "";
        this.list = item.children;
        const parents = this.getParents(this.tree, item.value);
        this.parents = parents.reverse();
      } else {
        alert("已经是最后一级");
      }
    },
    // 找到所有父级
    getParents(tree, value) {
      for (const item of tree) {
        if (item.value === value) return [item];
        if (item.children) {
          const node = this.getParents(item.children, value);
          if (node !== undefined) return node.concat(item);
        }
      }
    },
  },
};
</script>
<style lang="less">
.demo {
  .top {
    display: flex;
    justify-content: space-between;
    > ul {
      display: flex;
      li {
        font-size: 10px;
        margin-right: 10px;
      }
    }
  }
  .van-radio-group {
    > div {
      > .van-radio {
        display: inline-block;
      }
    }
  }
}
</style>
相关推荐
weibkreuz1 小时前
收集表单数据@10
开发语言·前端·javascript
hboot1 小时前
别再被 TS 类型冲突折磨了!一文搞懂类型合并规则
前端·typescript
在西安放羊的牛油果1 小时前
浅谈 import.meta.env 和 process.env 的区别
前端·vue.js·node.js
鹏北海2 小时前
从弹窗变胖到 npm 依赖管理:一次完整的问题排查记录
前端·npm·node.js
布列瑟农的星空2 小时前
js中的using声明
前端
薛定谔的猫22 小时前
Cursor 系列(2):使用心得
前端·ai编程·cursor
用户904706683572 小时前
后端问前端:我的接口请求花了多少秒?为啥那么慢,是你慢还是我慢?
前端
深念Y2 小时前
仿B站项目 前端 4 首页 顶层导航栏
前端·vue·ai编程·导航栏·bilibili·ai开发
dragonZhang2 小时前
基于 Agent Skills 的 UI 重构实践:从 Demo 到主题化界面的升级之路
前端·ai编程·claude
王林不想说话2 小时前
提升工作效率的Utils
前端·javascript·typescript