js树转平与平转树结构

树形结构转平面结构

let data = [
            {
                id: 1, pid: 0, name: '沃尔玛',
                childrens: [
                    {
                        id: 2, pid: 1, name: '生鲜区',
                        childrens: [
                            {
                                 id: 4,
                                 pid: 2,
                                 name: '鱼',
                                 childrens:[
                                    {
                                        id:8,
                                        pid:4,
                                        name:'金鱼',
                                    },
                                    {
                                        id:9,
                                        pid:4,
                                        name:'带鱼',
                                    }
                                 ]
                             },
                            { id: 5, pid: 2, name: '牛肉' }
                        ]
                    },
                    {
                        id: 3, pid: 1, name: '日用品区',
                        childrens: [
                            { id: 6, pid: 3, name: '卫生纸' },
                            { id: 7, pid: 3, name: '牙刷' }
                        ]
                    }
                ]
            }   
        ];
        function tree(arr) {
            var newarr = [];
            arr.forEach(item => {
                if (item.childrens) {
                    newarr.push(...tree(item.childrens))
                    delete item.childrens
                }
                newarr.push({ ...item })
            });
            return newarr
        };

        var way = tree(data);
        console.log(way);

平面结构转树形结构

 const source = [
            { id: 1, pid: null, name: '一级' },
            { id: 2, pid: 1, name: '二级' },
            { id: 3, pid: 1, name: '二级' },
            { id: 4, pid: 2, name: '三级' },
            { id: 5, pid: 2, name: '三级' },
            { id: 6, pid: 3, name: '三级' },
            { id: 7, pid: 3, name: '三级' },
            { id: 8, pid: 7, name: '四级' },
            { id: 9, pid: 7, name: '四级' }
        ];
        function arrt(arr, pid = null) {
            const res = [];
            source.forEach(item => {
                if (item.pid === pid) {
                    const children = arrt(arr.filter(e => e.pid != pid), item.id)
                    if (children.length) {
                        res.push({ ...item, children })
                    } else {
                        res.push({ ...item })
                    }
                }
            });
            return res
        }

        var tree = arrt(source);
        console.log(tree);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Web平台上实现文件夹一样的树形结构,可以使用HTML、CSS和JavaScript来实现。 首先,我们需要准备一个基本的HTML结构,包括一个div容器和一些ul和li元素,用于组成树形结构: ```html <div id="tree"> <ul> <li>文件夹1 <ul> <li>文件1</li> <li>文件2</li> </ul> </li> <li>文件夹2 <ul> <li>文件3</li> <li>文件4</li> </ul> </li> </ul> </div> ``` 接下来,我们需要使用CSS样式来美化这些元素,使它们看起来像一个树形结构: ```css #tree ul { list-style: none; margin: 0; padding: 0; } #tree li { position: relative; padding-left: 20px; line-height: 20px; } #tree li:before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 10px; border-left: 1px solid #ccc; } #tree li:last-child:before { height: 20px; } #tree li:after { content: ""; position: absolute; left: 0; top: 10px; width: 10px; height: 1px; background-color: #ccc; } #tree li:last-child:after { display: none; } #tree ul ul:before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 10px; border-left: 1px solid #ccc; margin-left: 10px; } #tree ul ul li:last-child:before { height: auto; min-height: 20px; margin-top: -1px; margin-bottom: -1px; } ``` 最后,我们需要使用JavaScript来添加事件处理程序,以便用户可以展开或折叠树形结构: ```javascript var tree = document.getElementById("tree"); var nodes = tree.getElementsByTagName("li"); for (var i = 0; i < nodes.length; i++) { var node = nodes[i]; var childNode = node.getElementsByTagName("ul")[0]; if (childNode) { childNode.style.display = "none"; var icon = document.createElement("span"); icon.innerHTML = "+"; icon.className = "expand"; node.insertBefore(icon, node.firstChild); icon.onclick = function() { var childNode = this.parentNode.getElementsByTagName("ul")[0]; if (childNode.style.display == "none") { childNode.style.display = "block"; this.innerHTML = "-"; this.className = "collapse"; } else { childNode.style.display = "none"; this.innerHTML = "+"; this.className = "expand"; } }; } } ``` 这段代码会遍历所有的li元素,并为每个子元素添加一个可折叠的图标,在用户单击该图标时会展开或折叠子元素。 综上所述,我们可以使用HTML、CSS和JavaScript来实现文件夹一样的树形结构。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值