vue-giant-tree模糊搜索,自動展開匹配子節(jié)點(diǎn),再次搜索可重新加載節(jié)點(diǎn)

vue-giant-tree是對ztree的一個(gè)封裝,但有些方法是沒有封裝進(jìn)去的,例如節(jié)點(diǎn)的隱藏方法hideNode()等

1、下載安裝vue-giant-tree

npm i vue-giant-tree --save或使用淘寶鏡像?cnpm i vue-giant-tree --save

2、在頁面導(dǎo)入

import tree from "vue-giant-tree"?

3、h5頁面代碼

<div class="zTree">

????<search ref="search" @onSearch="onSearch"></search>? <!--?搜索組件 ?-->

????<div class="tree"> <tree :nodes="nodes" :setting="setting" @onCreated="handleCreated"/> </div>

</div>

4、zTree配置及vue數(shù)據(jù)參考,具體參數(shù)可查看zTree官網(wǎng)zTree API文檔

vue頁面配置

5、vue頁面方法

···

handleCreated(ztreeObj){?

?????this.zTree = ztreeObj?

?????let firstTree = ztreeObj.getNodes()[0]

?????ztreeObj.expandNode(firstTree); // 展開第一行?

?},?

onSearch(value){

?????if(value){?

?????????this.zTree.refresh()?

?????????let nodeList = this.zTree.getNodesByParamFuzzy('name', value) //模糊搜索

?????????if(this.expandNode.length > 0){?

?????????????for(let j in this.expandNode){?

?????????????????this.closeParentNode(this.expandNode[j])?

?????????????}

? ? ? ? ? ? }?

? ? ? ? ? ? this.expandNode = []?

? ? ? ? ? ? let timeout = setTimeout(() =>{

?????????????????clearTimeout(timeout)?

?????????????????for(let i in nodeList){?

?????????????????????this.firstNode = true this.getParentNode(nodeList[i])?

?????????????????}

?????????????},300) }?

?},?

?closeParentNode(node){ //關(guān)閉之前展開的節(jié)點(diǎn)

?????if(node){?

?????????let parentNode = node.getParentNode()?

?????????if(parentNode){?

?????????????this.zTree.expandNode(parentNode,false,false,false) //關(guān)閉節(jié)點(diǎn)

?????????????this.closeParentNode(parentNode)?

?????????}?

?????????this.zTree.expandNode(node,false,false,false)//關(guān)閉節(jié)點(diǎn)

?????}?

?},?

?getParentNode(node){?

?????let parentNode = node.getParentNode()?

?????this.expandNode.push(parentNode) //保存展開節(jié)點(diǎn)

? ? ? if(this.firstNode){?

?????????this.firstNode = false?

?????????node.searchNode = 'searchNode'?

?????????this.zTree.expandNode(parentNode,true,false,false)//展開節(jié)點(diǎn)

?????????this.zTree.updateNode(node) node.searchNode = ''?

?????}

?}

···

6、效果圖


效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容