vue2+elementUI的el-tree的懶加載功能
lazy 屬性為true 時(shí)生效
lazy ----> 是否懶加載子節(jié)點(diǎn),需與 load 方法結(jié)合使用
isLeaf
可以提前告知 Tree 某個(gè)節(jié)點(diǎn)是否為葉子節(jié)點(diǎn),從而避免在葉子節(jié)點(diǎn)前渲染下拉按鈕。
HTML部分
<el-tree class="filter-tree" ref="tree" accordion :data="leftData" @node-click="handleNodeClick" node-key="listId" :current-node-key="currentNodeKey" :highlight-current="true" :props="defaultProps" :default-expanded-keys="idArr" lazy :load="loadNode" > <span class="custom-tree-node" slot-scope="{ node, data }"> <el-tooltip class="item" effect="dark" :content="node.label" placement="top-start" > <span :id="data.listId">{{ node.label }}</span> </el-tooltip> </span> </el-tree>
JS部分
import { getMenuList, getNodeMenuList, getDataList, } from "@/api/index"; export default { data() { return { defaultProps: { children: "children", label: "name", isLeaf: "isLeaf", }, currentNodeKey: "", leftData:[], } }, methods:{ // 懶加載獲取數(shù)據(jù) loadNode(node, resolve) { if (node.level === 0) {// 第一層數(shù)據(jù) getMenuList({}).then((res) => { //左面導(dǎo)航欄省份信息 if (res.data.resp_code == "200") { let data = res.data.datas; for (let key in data) { this.leftData.push({ name: key, listId: key, }); } } }); return resolve(this.leftData); } if (node.level === 1) {// 第二層數(shù)據(jù) let provinceName = { provinceName: node.label, //上海 }; let twoList = []; getNodeMenuList(provinceName).then((res) => { res.data.datas.forEach((items) => { twoList.push({ listId: items.listID, name: items.nodeName, provinceName: items.provinceName, children: [], }); twoList.isLeaf = true; }); }); setTimeout(() => { resolve(twoList); }, 1000); } if (node.level == 2) {//第三層數(shù)據(jù) let obj = { nodeName: node.data.name, provinceName: node.data.provinceName, }; let children = []; getDataList(obj).then((res) => { if (res.data.resp_code == "200") { res.data.datas.forEach((item, index) => { node.data.children.push({ description: item.description, links: item.links, listId: item.listId, name: item.name, provinceName: item.provinceName, isLeaf: true,// 判斷是不是子節(jié)點(diǎn)(最后一層數(shù)據(jù) 是否顯示下拉圖標(biāo)) 如果不顯示下拉圖標(biāo)為true 如果顯示就可以不寫 }); }); } }); resolve(node.data.children); } if (node.level > 2) { resolve([]); } }, } }
element ui 中 el-tree 實(shí)現(xiàn)懶加載
在上次 element UI 中的 el-tree 實(shí)現(xiàn) checkbox 單選框功能,及 bus 傳遞參數(shù)的方法 篇章中有提到 le-tree 的懶加載的功能,正好今天有時(shí)間來(lái)補(bǔ)充一下,讓我們嗨起來(lái) ??
html 部分
下面 el-tree 標(biāo)簽中屬性的介紹這里只介紹 props 、lazy、load屬性 ,其他屬性在上面鏈接的文章中有說(shuō)明
1、props:綁定你定義的 props 變量,這里的 props變量是個(gè)對(duì)象,里面有幾個(gè)鍵值:label、children、disabled(只有tree中帶有checkbox才需要)、isLeaf(分別代表什么意思,下面的 data 部分有說(shuō)明)。
2、lazy:為是否啟用來(lái)加載的樹型結(jié)構(gòu),true為啟用,false為禁用,默認(rèn)true。
3、load:lazy為true的時(shí)候生效,用來(lái)懶加載加載節(jié)點(diǎn)的方法,請(qǐng)求樹的數(shù)據(jù)和邏輯處理都在這個(gè)方法匯總進(jìn)行,下面的事件方法部分會(huì)有說(shuō)明。
<el-tree :props="props" :load="loadNode" lazy node-key="id" :expand-no-click-node="true" ref="tree" show-checkbox :check-strictly="true" @check="checkClick" > </el-tree>
data 部分
注:
1、下面的提到的屬性在后面事件方法部都會(huì)講到具體怎樣使用。
2、這里沒有定義 disabled 屬性,因?yàn)檫@里沒有必要定義,事件中直接使用就行,后面事件方法部會(huì)有說(shuō)明。
<script> export default { data(){ return{ props:{ // 用來(lái)控制展示節(jié)點(diǎn)內(nèi)容的字段,根據(jù)后端數(shù)據(jù)自定義 label 值是什么 label:'text', // 子級(jí)存放的位置,拼接數(shù)據(jù)時(shí)候把子級(jí)放到這里定義 childNodes 的字段中,字段名稱自定義 注意:childNodes 是個(gè)數(shù)組。 children:'childNodes', // 用來(lái)判斷是否為最后一級(jí)子節(jié)點(diǎn) isLeaf:'lastNode', // 這里的 id 是用來(lái)關(guān)聯(lián)上面 node-key="id" 因?yàn)檫@里的樹使用 checkbox 所以用到了 node-key="id" 和 id, 正常的只是單純加載tree的話是不需要的,看需求嘍 id:"id" } } } }; </script>
方法事件部分
methods: { // 功能講解 /* 默認(rèn)參數(shù): node:每加載一次當(dāng)前加載的節(jié)點(diǎn)的所有數(shù)據(jù) reslove:渲染數(shù)據(jù)的方法,把得到該節(jié)點(diǎn)的所有數(shù)據(jù)(數(shù)組)放到該方法中 return 出去,節(jié)點(diǎn)就能渲染出來(lái)了。 */ async loadNode(node, reslove) { /* 判斷是跟節(jié)點(diǎn)還是子節(jié)點(diǎn), 當(dāng) node.level == 0 的時(shí)候就是 tree 的跟節(jié)點(diǎn)(第一層; 當(dāng) node.level 不等于 0 的情況就是每一層的節(jié)點(diǎn)了,沒必要一直判斷 level 是多少級(jí)別,除非有特殊需求 */ if (node.level == 0) { // 這里是請(qǐng)求接口,根據(jù)自己的項(xiàng)目請(qǐng)求接口就可以。 let { resultValue } = await getTree(`pwi81cf45a5d07801452018091900001,${this.$store.state.userInfo.cityId}/`) // 這里就提到了 props 對(duì)象中的 disabled 屬性的用法,通過找到請(qǐng)求接口找到滿足條件的數(shù)據(jù),然后這個(gè)給滿足條件的數(shù)據(jù)添加一個(gè) disabled = true 就可以實(shí)現(xiàn) checkbox 禁用的效果了。 resultValue.nodes[0].exValue == "" || resultValue.nodes[0].exValue == 'DYDJ' ? resultValue.nodes[0].disabled = true : null // 將請(qǐng)求回來(lái)的數(shù)據(jù)放到 reslove() 方法中渲染出來(lái)。 return reslove(resultValue.nodes) } else { let endType = node.data.itemType.split('#')[1]; let { resultValue } = await getTree(`pwi81cf45a5d07801452018091900001,${node.data.id}/${endType}`) /* 這里就是 props 對(duì)象中 isLeaf 屬性的用法,上看我們定義的 isLeaf=‘lastNode', 通過循環(huán)請(qǐng)求回來(lái)的數(shù)據(jù),hasChildren 值為 flase (這個(gè)值一般都是后端設(shè)置數(shù)據(jù)的時(shí)候有帶,至于是什么字段就得看后端是怎么定義的),用來(lái)判斷是否還有下一級(jí)別, 當(dāng) hasChildren 值為 flase 的時(shí)候就可以給 請(qǐng)求回來(lái)的數(shù)據(jù)中添加一個(gè) lastNode = true 的鍵值了,這就在你點(diǎn)開改節(jié)點(diǎn)的父級(jí)的時(shí)候就不回有提示 ,還能展開的三角箭頭了。 */ resultValue.nodes.forEach(item => { item.hasChildren == false ? item.lastNode = ture : null }); /* 這里是 props 對(duì)象中 children: 'childNodes' 屬性的用法,因?yàn)槭菓屑虞d每次求情回來(lái)的數(shù)據(jù)都是沒有和上面的數(shù)據(jù)有關(guān)聯(lián)的一個(gè)數(shù)組,這時(shí)用想通過 el-tree 來(lái)實(shí)現(xiàn)樹型結(jié)構(gòu)的話我們就要拼裝數(shù)據(jù),將去回來(lái)的數(shù)據(jù)拼裝到 當(dāng)前節(jié)點(diǎn)的 childNodes 鍵值當(dāng)中即可 */ node.data.childNodes = resultValue.nodes resultValue.nodes[0].exValue == "" || resultValue.nodes[0].exValue == 'DYDJ' ? resultValue.nodes[0].disabled = true : null return reslove(resultValue.nodes) } }, // 套用模版 async loadNode(node, reslove) { if (node.level == 0) { let res = await getData() return reslove(res.data) } else { let res = await getData() // 判斷是否為最底層節(jié)點(diǎn) 根據(jù)自己接口返回的數(shù)據(jù)判斷即可 res.data.forEach(item => { item.hasChildren == false ? item.lastNode = ture : null }); return reslove(res.data) } }, },
到此這篇關(guān)于vue2+elementUI的el-tree的懶加載的文章就介紹到這了,更多相關(guān)vue2 elementUI懶加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue2+elementUI的el-tree的選中、高亮、定位功能的實(shí)現(xiàn)
- java+vue3+el-tree實(shí)現(xiàn)樹形結(jié)構(gòu)操作代碼
- 在vue3中使用el-tree-select實(shí)現(xiàn)樹形下拉選擇器效果
- Vue中el-tree樹全部展開或收起的實(shí)現(xiàn)示例
- vue之el-tree懶加載數(shù)據(jù)并且實(shí)現(xiàn)樹的過濾問題
- vue el-select與el-tree實(shí)現(xiàn)支持可搜索樹型
- Vue+Elementui el-tree樹只能選擇子節(jié)點(diǎn)并且支持檢索功能
相關(guān)文章
vue3實(shí)現(xiàn)抽獎(jiǎng)模板設(shè)置
這篇文章主要為大家詳細(xì)介紹了vue3實(shí)現(xiàn)抽獎(jiǎng)模板設(shè)置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03vue如何解決watch和methods值執(zhí)行順序問題
這篇文章主要介紹了vue如何解決watch和methods值執(zhí)行順序問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08uniapp模仿微信實(shí)現(xiàn)聊天界面的示例代碼
這篇文章主要介紹了如何利用uniapp模仿微信,實(shí)現(xiàn)一個(gè)聊天界面。文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,感興趣的可以了解一下2022-01-01vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)預(yù)覽視頻封面組件示例詳解
這篇文章主要為大家介紹了vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)預(yù)覽視頻封面組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07vue填坑之webpack run build 靜態(tài)資源找不到的解決方法
今天小編就為大家分享一篇vue填坑之webpack run build 靜態(tài)資源找不到的解決方法。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-09-09關(guān)于VanCascader默認(rèn)值(地址code轉(zhuǎn)換)
這篇文章主要介紹了關(guān)于VanCascader默認(rèn)值(地址code轉(zhuǎn)換),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07vue實(shí)現(xiàn)網(wǎng)頁(yè)語(yǔ)言國(guó)際化切換
這篇文章介紹了vue實(shí)現(xiàn)網(wǎng)頁(yè)語(yǔ)言國(guó)際化切換的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2021-11-11