前端 本地搜索 el-table 多层级-树形数据

前端 本地搜索 el-table 多层级-树形数据

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let ary = [];
        let arr = [{
                id: 1,
                title: '你好吗?',
                children: [{
                        id: 11,
                        title: '很好啊',
                        children: null
                    },
                    {
                        id: 12,
                        title: '是吗',
                        children: [{
                                id: 121,
                                title: '李世民',
                                children: null
                            },
                            {
                                id: 122,
                                title: '朱元璋',
                                children: null
                            }
                        ]
                    }
                ]
            },
            {
                id: 2,
                title: '卡卡卡',
                children: [{
                    id: 21,
                    title: '非常好芬',
                    children: null
                }]
            },
            {
                id: 22,
                title: '第三方的',
                children: null
            }
        ];
        const rebuildData = (value, arr) => {
            if (!arr) {
                return []
            }
            let newarr = [];
            arr.forEach(element => {
                if (element.title.indexOf(value) > -1) {
                    const ab = rebuildData(value, element.children);
                    const obj = {
                        ...element,
                        children: ab
                    };
                    newarr.push(obj);
                    // console.log(newarr.push(obj), "236");
                } else {
                    if (element.children && element.children.length > 0) {
                        const ab = rebuildData(value, element.children);
                        const obj = {
                            ...element,
                            children: ab
                        };
                        if (ab && ab.length > 0) {
                            newarr.push(obj);
                            // console.log(newarr.push(obj), "789");
                        }
                    }
                }
            });
            // console.log(newarr, "9568");
            this.ary = newarr;
            return newarr;
        };

        console.log(rebuildData('李世民', arr));
        console.log(this.ary);
        // console.log(arr);
    </script>
</body>

</html>

打印结果-多层级表格搜索数据

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值