当前位置: 代码网 > 服务器>软件设计>开源 > Element UI CascaderPanel级联组件使用和踩坑总结

Element UI CascaderPanel级联组件使用和踩坑总结

2024年08月03日 开源 我要评论
Element UI CascaderPanel级联组件使用和踩坑总结

element ui cascaderpanel级联组件使用和踩坑总结

问题背景

需求中需要用到element ui的 cascaderpanel组件,并且支持多选,定制化需求,比如某节点被选择,等价于该节点下面所有子节点都被选择, cascaderpanel组件返回的选择数据为选择的所有节点,需要过滤到只返回到最上方被选中的层级。如图所示:
在这里插入图片描述

问题分析

(1)el-cascader-panel组件基本使用

                <el-cascader-panel ref="test" :options="myoptions" @change="handnodechange('test')"  v-model="myoptionsselectedareas" 
                :props="{ multiple: true }">

(2)过滤所有节点,过滤方法:!(item.parent && item.parent.checked),即排除掉父节点存在且被选中的节点,得到过滤后的节点:

            // 监听级联组件数据变化,隐藏父组件选择状态下的子组件。
            handnodechange(value) {
              let checkednodelist = this.$refs[value].getcheckednodes();
              checkednodelist = checkednodelist.filter(item => {
                return !(item.parent && item.parent.checked);
              });
              this[value] = checkednodelist;
              this.myoptionsstandardselectedareas = [];
              checkednodelist.foreach(item => {
                this.myoptionsstandardselectedareas.push(item.path.join('-'));
              });
            },

(3)坑:最后一级即叶子结点,也有children只不过children=[],这就导致组件无法确定最后一级,所以无法触发change事件。把叶子节点的children删除即可(该坑在element plus中测试已没有)。

          // 遍历得到需要的树结构
          dealnodetree(node) {
            if (!node) {
              return null;
            }
            let temnode = this.changenode(node);
            if (temnode.children) {
              // 处理children字段
              temnode.children = temnode.children.map(item => {
              return this.dealnodetree(item);
            });
            }
            return temnode;
          },

(4)服务器返回的字符串,或者选中数组直到选中节点最高父级,需要获取所有选中的子节点,才能让级联组件正常显示。
dfs对根节点进行深度遍历,获取所有路径,存到全路径列表中,和最高级父节点进行匹配,显示匹配的节点即可。

            getallpath(node, path = []) {
              if (!node) {
                return null; // 如果为空节点则返回null
              }

              const currentval = node.value; // 当前节点值
              path.push(currentval); // 将当前节点值添加到路径数组中

              if (!node.children || node.children.length === 0) {
                // console.log('路径:', path); // 输出完整路径
                this.myresult.push(path);
              } else {
                for (let i = 0; i < node.children.length; i++) {
                  this.getallpath(node.children[i], [...path]); // 对每个子节点进行递归调用
                }
              }

              // path.pop(); // 移除最后一个元素,因为已经处理过该节点了
            },
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com