iview通过输入框过滤树节点

1 篇文章 0 订阅
1 篇文章 0 订阅

element-ui和iview中都有树形结构,可以用来展示多层级的数据,比如目录,上下级组等等:
在这里插入图片描述
可数据很多或者树有很多层的时候,我们要想找到某一级就比较麻烦了,这点element-ui有现成的搜索过滤组件,但iview里面则没有这种属性方法,此时就只能我们自己去写方法来实现了。
以下是我尝试后的方法:

<template>
    <div>
        <Input placeholder="输入关键字进行过滤" v-model="filterText"/>
        <Tree :data="data" show-checkbox></Tree>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                filterText:null,
                flag:0,
                data:[],
                data2:  [{
                    expand: true,
                    title: '一级 1',
                    children: [{
                        expand: true,
                        title: '二级 1-1',
                        children: [
                            {
                            expand: true,
                            title: '三级 1-1-2',
                            children: [
                                {
                                expand: true,
                                title: '四级 1-1-3-4'
                                }]
                            }, 
                            {
                            expand: true,
                            title: '三级 1-1-2'
                            }
                        ]
                    }]
                    }, {
                    expand: true,
                    title: '一级 2',
                    children: [{
                        expand: true,
                        title: '二级 2-1'
                    }, {
                        expand: true,
                        title: '二级 2-2'
                    }]
                    }, {
                    expand: true,
                    title: '一级 3',
                    children: [{
                        expand: true,
                        title: '二级 3-1'
                    }, {
                        expand: true,
                        title: '二级 3-2'
                    }]
                    }],
            }
        },
        methods: {
            //过滤树节点
            filterNode(val,List){
                //过滤出满足条件的数组
                let List1=List.filter(item=>{
                    //如果该元素有children,则优先处理children的
                    if(item.children){
                        //过滤children里面满足条件的
                        item.children=this.filterNode(val,item.children)
                        //如果children里面没有满足条件的,则进入,否则返回true
                        if(!item.children.some(item1=>item1.title.indexOf(val) !== -1)){
                            //判断children里面是否还有一层children,如果有则对内层children继续调用函数判断,否则返回title中含有关键字的元素
                            if(item.children.some(item1=>item1.children)){
                                item.children.forEach(item2 => {
                                    item2.children&&this.filterNode(val,item2.children)
                                });
                            }else{
                                return item.title.indexOf(val) > -1
                            }
                        }   
                        return true
                    }
                    //返回title中含有关键字的元素
                    return item.title.indexOf(val) > -1
                })
                return List1
            }
        },
        mounted(){
            this.data=this.data2
        },
        watch: {
            filterText(val) {
                let arr=JSON.parse(JSON.stringify(this.data2))
                this.data=this.filterNode(val,arr)
            }
        },
    }
</script>

效果图(依次为什么都没输-输入1-输入2-输入3-输入4):
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
即效果实现!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值