vue与ant-tree结合伪造懒加载并可以查询

功能:
1、树懒加载
2、查询
3、关键字高亮显示
4、初始化加载展开4层

构造原因:ant-tree异步加载数据查询之后,节点不支持再次请求数据,直接渲染全部节点后,数据量特别大(生成dom进行操作),会造成浏览器卡顿

思路:
1、switcherIcon重写自定义树节点的展开/折叠图标
2、通过loaded状态判断是否完成了懒加载,当懒加载进行时,用loading状态更改图标
3、每次懒加载之后,给数据新建一个空的子节点,进行伪造懒加载(只有节点下有孩子节点时,才会有图标)

view视图



    
    

加载树

async loadTree(treeId,treeNode,isSearch){
    let params={treeId}
    let [err,res] = await loadTree(params);
    if(!err){
        if(res.result.code == 200){
            //存在树节点
            if(treeId){
                treeNode.dataRef.children = this.getUpdateTree(res.result.data);
                this.treeData = [...this.treeData];
            }else{
                if(isSearch){
                    this.treeData = res.result.data;
                    this.expandedKeys = [];
                    this.getAllKey(this.treeData)
                }else{
                    this.treeData = this.getUpdateTree(res.result.data);
                    if(!this.treeData.length){return;}
                    //首次加载默认加载到第四层
                    this.expandedKeys = []
                    this.initTree(this.treeData)
                }
            }
        }
    }
}

initTree:初始化循环树,默认展示到第几层

async initTree(treeData,index){
    if(index > 4 || !treeData.length){return};
    treeData[0].loaded = true;
    treeData[0].loading = true;
    let data={dataRef:treeData[0]};
    let returnData = await this.onLoadData(data);
    if(returnData.length){
        treeData[0].loading = false;
        this.expandedKeys.push(treeData[0].id);
        index++;
        if(index == 2){
            treeData = returnData[0].children
        }else if(index == 3){
            treeData = returnData[0].children[0].children
        }else if(index == 4){
            treeData = returnData[0].children[0].children[0].children
        }
        this.initTree(treeData,index)
    }
}

onLoadData:懒加载

onLoadData(treeNode){
    return new Promise(resolve=>{
        setTimeout(()=>{
            treeNode.dataRef.loading = false;
            resolve(this.loadTree(treeNode.dataRef.id,treeNode))
        },10)
    })
}

getUpdateTree:添加一个空数组,伪造懒加载

getUpdateTree(data){
    data.map(item=>{
        if(存在子节点){
            item.children = [{}]
        }
    })
}

getAllKey:获取树节点所有的key

getAllKey(data){
    data.map(item=>{
        this.expandedKeys.push(item.id);
        if(item.children){
            this.getAllKey(item.children)
        }
    })
}

onSearch:搜索

onSearch(){
    this.expandedKey = []
    this.searchKeywords = this.searchStr;
    //替换特殊字符
    this.replaceKeywords = this.searchStr.split("\\").join("\\\\");
    this.replaceKeywords = this.replaceKeywords.split("[").join("\\[");
    this.replaceKeywords = this.replaceKeywords.split("]").join("\\]");
    this.replaceKeywords = this.replaceKeywords.split("$").join("\\$");
    this.replaceKeywords = this.replaceKeywords.split("^").join("\\^");
    this.replaceKeywords = this.replaceKeywords.split("*").join("\\*");
    this.replaceKeywords = this.replaceKeywords.split("(").join("\\(");
    this.replaceKeywords = this.replaceKeywords.split(")").join("\\)");
    this.replaceKeywords = this.replaceKeywords.split("+").join("\\+");
    this.replaceKeywords = this.replaceKeywords.split("|").join("\\|");
    this.replaceKeywords = this.replaceKeywords.split("?").join("\\?");
    this.loadTree("","",true)
}

changeExpand:点击判断是否异步加载

changeExpand(event,node){
    if(node.node.dataRef.loaded){
        this.expandedKeys = event;
        return;
    }
    node.node.dataRef.loading = true;//添加懒加载效果
    node.node.dataRef.loaded = true;//当点击时,表示已经加载
    this.onLoadData(node.node);
    this.expandedKeys = event
}

你可能感兴趣的:(懒加载查询)