vue與ant-tree結(jié)合偽造懶加載并可以查詢
構(gòu)造原因
ant-tree異步加載數(shù)據(jù)查詢之后,節(jié)點不支持再次請求數(shù)據(jù),直接渲染全部節(jié)點后,數(shù)據(jù)量特別大(生成dom進行操作),會造成瀏覽器卡頓
思路:
1、switcherIcon重寫自定義樹節(jié)點的展開/折疊圖標
2、通過loaded狀態(tài)判斷是否完成了懶加載,當懶加載進行時,用loading狀態(tài)更改圖標
3、每次懶加載之后,給數(shù)據(jù)新建一個空的子節(jié)點,進行偽造懶加載(只有節(jié)點下有孩子節(jié)點時,才會有圖標)
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é)點,方便首次加載默認展開層級
async loadTree(treeId,treeNode,isSearch){
let params={treeId}
let [err,res] = await loadTree(params);
if(!err){
if(res.result.code == 200){
//存在樹節(jié)點
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;}
//首次加載默認加載到第四層
this.expandedKeys = []
this.initTree(this.treeData,1,4)
}
}
}
}
}initTree 初始化循環(huá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:添加一個空數(shù)組,偽造懶加載
getUpdateTree(data){
data.map(item=>{
if(存在子節(jié)點){
item.children = [{}]
}
})
}getAllKey:獲取樹節(jié)點所有的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;//當點擊時,表示已經(jīng)加載
this.onLoadData(node.node);
this.expandedKeys = event
}以上就是vue與ant-tree結(jié)合偽造懶加載并可以查詢的詳細內(nèi)容,更多關(guān)于vue ant-tree偽造懶加載查詢的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3.0如何使用computed來獲取vuex里數(shù)據(jù)
這篇文章主要介紹了vue3.0如何使用computed來獲取vuex里數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

