vue與ant-tree結(jié)合偽造懶加載并可以查詢
構(gòu)造原因
ant-tree異步加載數(shù)據(jù)查詢之后,節(jié)點不支持再次請求數(shù)據(jù),直接渲染全部節(jié)點后,數(shù)據(jù)量特別大(生成dom進行操作),會造成瀏覽器卡頓
思路:
1、switcherIcon重寫自定義樹節(jié)點的展開/折疊圖標(biāo)
2、通過loaded狀態(tài)判斷是否完成了懶加載,當(dāng)懶加載進行時,用loading狀態(tài)更改圖標(biāo)
3、每次懶加載之后,給數(shù)據(jù)新建一個空的子節(jié)點,進行偽造懶加載(只有節(jié)點下有孩子節(jié)點時,才會有圖標(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é)點,方便首次加載默認(rèn)展開層級
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;} //首次加載默認(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:添加一個空數(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;//當(dāng)點擊時,表示已經(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)文章
從0到1構(gòu)建vueSSR項目之路由的構(gòu)建
這篇文章主要介紹了從0到1構(gòu)建vueSSR項目之路由的構(gòu)建,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03關(guān)于VUE點擊父組件按鈕跳轉(zhuǎn)到子組件的問題及解決方案
本文主要介紹了在Vue框架中,如何通過父組件的點擊事件打開子組件中的彈窗并展示表格內(nèi)容,主要步驟包括在父組件中定義數(shù)據(jù)屬性,創(chuàng)建并定義子組件的彈窗和表格內(nèi)容,通過props屬性和自定義事件實現(xiàn)父子組件間的數(shù)據(jù)傳遞和方法調(diào)用2024-10-10使用watch監(jiān)聽路由變化和watch監(jiān)聽對象的實例
下面小編就為大家分享一篇使用watch監(jiān)聽路由變化和watch監(jiān)聽對象的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02