iview 级联选择器 Cascader的使用,将树结构解析为多级选择

iview 级联选择器 Cascader的使用

iview 级联选择器 Cascader的使用

最近在做项目时遇到了使用Cascader将后端的树形数据解析成三级选项,并实现选择,

html

<FormItem label="请选择维度">
            <template>
              <Cascader :data="results" v-model="select" @on-change="handleChange"></Cascader>
            </template>
          </FormItem>

从后端接收数据,并解析

  //引入树结构
        getTreeData().then(res => {
          if (res.data != null) {
            console.log(res.data.data)
            this.treeData = res.data.data;
            this.results = this.$options.methods.convertTree(this.treeData);
          }
        }),
convertTree(treeData) {
        const result = []
        // 遍历 tree
        treeData.forEach((item) => {
          // 解构赋值
          let {
            id: value,
            name: label,
            children: children
          } = item
          // 如果有子节点,递归
          if (children) {
            children = this.convertTree(children)
          }
          result.push({
            value,
            label,
            children
          })
        })
        // console.log("调用成功")


        return result
        // console.log("调用成功")
      },

后端传回来的数据

{
    "success": 1,
    "message": "请求成功!",
    "data": [
        {
            "id": 1001,
            "name": "阿萨斯",
            "children": [
                {
                    "id": 10011,
                    "name": "1.1按时发散分散",
                    "children": [
                        {
                            "id": 100111,
                            "name": "碍事法师法师",
                            "children": []
                        }
                    ]
                },
             

回传数据的方法

      //选择维度
      handleChange(value, selectedData) {//selectData是被选中的数据,可按照被选中的数据顺序取值
        console.log(selectedData)
        this.addOne.id_dimension = selectedData[0].value;
        this.addOne.id_master = selectedData[1].value;
        this.addOne.id_node = selectedData[2].value;
        this.addOne.name_node = selectedData[2].label;
        this.addOne.name_master = selectedData[1].label;
        this.addOne.name_dimension = selectedData[0].label;
      },
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值