vue與ant-tree結(jié)合偽造懶加載并可以查詢
構(gòu)造原因
ant-tree異步加載數(shù)據(jù)查詢之后,節(jié)點(diǎn)不支持再次請求數(shù)據(jù),直接渲染全部節(jié)點(diǎn)后,數(shù)據(jù)量特別大(生成dom進(jìn)行操作),會造成瀏覽器卡頓
思路:
1、switcherIcon重寫自定義樹節(jié)點(diǎn)的展開/折疊圖標(biāo)
2、通過loaded狀態(tài)判斷是否完成了懶加載,當(dāng)懶加載進(jìn)行時(shí),用loading狀態(tài)更改圖標(biāo)
3、每次懶加載之后,給數(shù)據(jù)新建一個(gè)空的子節(jié)點(diǎn),進(jìn)行偽造懶加載(只有節(jié)點(diǎn)下有孩子節(jié)點(diǎn)時(shí),才會有圖標(biāo))
view視圖
<a-input-search @search="onSearch" v-model.trim="searchStr"></a-input-search>
<a-tree
@select="onSelect"
:selectedKeys="selectedKeys"
:treeData="treeData"
:expandedKeys="expandedKeys"
@expand="changeExpand"
:replaceFields="{key:'id',title:'title',children:'children'}"
>
<template slot="switcherIcon" slot-scope="{loading}">
<a-icon type="caret-down" v-if="loading"></a-icon>
<a-icon type="loading" v-else></a-icon>
</template>
<template slot="title" slot-scope="{title}">
<span v-html="
title
? title.replace(
new RegExp(replaceKeywords,g),
'<span style=color:#f50>'+title+'</span>'
)
: null"
></span>
</template>
</a-tree>加載樹
loadingData為最后展開的一層節(jié)點(diǎn),方便首次加載默認(rèn)展開層級
async loadTree(treeId,treeNode,isSearch){
let params={treeId}
let [err,res] = await loadTree(params);
if(!err){
if(res.result.code == 200){
//存在樹節(jié)點(diǎn)
if(treeId){
treeNode.dataRef.children = this.getUpdateTree(res.result.data);
this.treeData = [...this.treeData];
this.loadingData = res.result.data
}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;}
//首次加載默認(rèn)加載到第四層
this.expandedKeys = []
this.initTree(this.treeData,1,4)
}
}
}
}
}initTree 初始化循環(huán)樹
默認(rèn)展示到第幾層
async initTree(treeData,index,maxLevel){
if(index > maxLevel || !treeData.length){return};
treeData.map(item=>{
if(item.childrenIds.length){
let initLoaded = item.loaded;
item.loaded = true;
item.loading = true;
let data = {dataRef:item}
if(initLoaded){
index++;
treeData = item.children
this.initTree(treeData,index,maxLevel);
item.loading = false;
this.expandedKeys.push(item.id);
return;
}
setTimeOut(
async ()=>{
let returnData = await this.onLoadData(data);
if(returnData.length){
index++;
treeData = this.loadingData;
this.initTree(treeData,index,maxLevel);
item.loading = false;
this.expandedKeys.push(item.id);
}
},
initLoaded ? 0 :100
)
}
})
}onLoadData:懶加載
onLoadData(treeNode){
return new Promise(resolve=>{
setTimeout(()=>{
treeNode.dataRef.loading = false;
resolve(this.loadTree(treeNode.dataRef.id,treeNode))
},10)
})
}getUpdateTree:添加一個(gè)空數(shù)組,偽造懶加載
getUpdateTree(data){
data.map(item=>{
if(存在子節(jié)點(diǎn)){
item.children = [{}]
}
})
}getAllKey:獲取樹節(jié)點(diǎn)所有的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:點(diǎn)擊判斷是否異步加載
changeExpand(event,node){
if(node.node.dataRef.loaded){
this.expandedKeys = event;
return;
}
node.node.dataRef.loading = true;//添加懶加載效果
node.node.dataRef.loaded = true;//當(dāng)點(diǎn)擊時(shí),表示已經(jīng)加載
this.onLoadData(node.node);
this.expandedKeys = event
}以上就是vue與ant-tree結(jié)合偽造懶加載并可以查詢的詳細(xì)內(nèi)容,更多關(guān)于vue ant-tree偽造懶加載查詢的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue環(huán)形進(jìn)度條組件實(shí)例應(yīng)用
在本文中我們給大家分享了關(guān)于vue環(huán)形進(jìn)度條組件的使用方法以及實(shí)例代碼,需要的朋友們跟著測試下吧。2018-10-10
vue項(xiàng)目適配屏幕分辨率與屏幕的縮放適配詳細(xì)教程
現(xiàn)在很多14寸的筆記本,出廠默認(rèn)就是150%的顯示。導(dǎo)致很多時(shí)候我們的項(xiàng)目,自己開發(fā)的時(shí)候都是按照100%比例來開發(fā)的,上線了就會發(fā)現(xiàn)這個(gè)問題,今天就這個(gè)問題給出解決方案,感興趣的朋友跟隨小編一起看看吧2022-11-11
從0到1構(gòu)建vueSSR項(xiàng)目之路由的構(gòu)建
這篇文章主要介紹了從0到1構(gòu)建vueSSR項(xiàng)目之路由的構(gòu)建,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
關(guān)于VUE點(diǎn)擊父組件按鈕跳轉(zhuǎn)到子組件的問題及解決方案
本文主要介紹了在Vue框架中,如何通過父組件的點(diǎn)擊事件打開子組件中的彈窗并展示表格內(nèi)容,主要步驟包括在父組件中定義數(shù)據(jù)屬性,創(chuàng)建并定義子組件的彈窗和表格內(nèi)容,通過props屬性和自定義事件實(shí)現(xiàn)父子組件間的數(shù)據(jù)傳遞和方法調(diào)用2024-10-10
使用watch監(jiān)聽路由變化和watch監(jiān)聽對象的實(shí)例
下面小編就為大家分享一篇使用watch監(jiān)聽路由變化和watch監(jiān)聽對象的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

