基于el-tree實(shí)現(xiàn)懶加載穿梭條的示例代碼
一、關(guān)鍵代碼
<template>
<div>
<!-- 左側(cè)待選列表 -->
<div class="left-box">
<p>待選列表</p>
<el-input placeholder="輸入關(guān)鍵詞過(guò)濾" v-model="leftFilterText" clearable/>
<el-tree
ref="treeLeft"
:data="leftData"
show-checkbox
node-key="id"
props="defaultProps"
:load="loadNode"
lazy
:filter-node-method="filterNode"
>
</el-tree>
</div>
<!-- 穿梭按鈕 -->
<div class="oper-box">
<el-button circle type="primary" icon="el-icon-arrow-left" @click="removeData"></el-button>
<el-button circle type="primary" icon="el-icon-arrow-right" @click="addData"></el-button>
</div>
<div class="right-box">
<p>已選列表</p>
<el-input placeholder="輸入關(guān)鍵詞過(guò)濾" v-model="rightFilterText" clearable/>
<el-tree
ref="treeRight"
:data="rightData"
show-checkbox
node-key="id"
props="defaultProps"
:filter-node-method="filterNode"
>
</el-tree>
</div>
</div>
</template>
<script>
data(){
return {
checkAll: false,
leftFilterText: '',
rightFilterText: '',
defaultProps: {
chilren: 'children',
label: 'labelName', // 適配后端下發(fā)的數(shù)據(jù)字段名
isLeaf: 'leaf', // leaf 字段判斷節(jié)點(diǎn)是否為葉子節(jié)點(diǎn)
// 配置禁選的節(jié)點(diǎn)
disabled: function(data, node) {
// 如這里配置父節(jié)點(diǎn)、帶有disable屬性的節(jié)點(diǎn)禁選
if('children' in data || data.disable) {
return true;
} else {
return false;
}
}
},
leftData: [],
rightData: []
}
},
watch: {
leftFilterText(val) {
this.$refs.treeLeft.filter(val);
},
rightFilterText(val) {
this.$refs.treeRight.filter(val);
}
},
methods: {
// 根據(jù)關(guān)鍵詞過(guò)濾節(jié)點(diǎn)
filterNode(value, data) {
if(!value) return true;
// labeName 為defaultProps中配置的label值,未配置默認(rèn)為label
return data.labeName.indexOf(value) !== -1;
},
// 懶加載出樹(shù)結(jié)構(gòu)的最后一層節(jié)點(diǎn)
async loadNode(node, resolve) {
if(node.level === 0) {
return resolve(node.data); // 頂層數(shù)據(jù)默認(rèn)展示
} else {
if(node.data.children && node.data.children.length > 0) {
return resolve(node.data.children);
} else { // 最后一層數(shù)據(jù),異步懶加載
let tempData = await this.getDynamicData(node.data.id);
return resolve(tempData);
}
}
},
// 獲取數(shù)據(jù)接口
getDynamicData(id) {
},
// 移除節(jié)點(diǎn)
removeData() {
// 右側(cè)選中節(jié)點(diǎn)
let removeKeys = this.$refs.treeRight.getCheckedKeys();
this.rightData = this.rightData.filter(item => !removeKeys.includes(item.id));
// 左側(cè):僅保留右側(cè)列表中有的數(shù)據(jù)為勾選狀態(tài)
let leftCheckKeys = this.rightData.map(item => item.id);
this.$refs.treeLeft.setCheckedKeys(leftCheckKeys);
},
// 添加節(jié)點(diǎn)
removeData() {
// 獲取左側(cè)選中節(jié)點(diǎn),作為右側(cè)的數(shù)據(jù)
let checkNodes = this.$refs.treeLeft.getCheckedNodes();
let checkKeys = this.$refs.treeLeft.getCheckedKeys();
this.rightData = checkNodes;
},
}
</script>
?? 過(guò)濾節(jié)點(diǎn)函數(shù):filterNode
1、watch 監(jiān)聽(tīng)關(guān)鍵詞;filterNode 必須有返回值,否則數(shù)據(jù)顯示不出來(lái);
2、關(guān)鍵詞不為空時(shí),函數(shù)的返回值 data.labeName.indexOf(value) !== -1; 其中 labeName 為defaultProps中配置的label值,未配置默認(rèn)為label
?? 異步加載函數(shù):loadNode
根據(jù) node.level 去匹配數(shù)據(jù)層級(jí),判斷是否需要調(diào)用接口獲取數(shù)據(jù)
?? 樣式自定義
二、最終效果:(效果圖僅供參考)
(1) 左側(cè)列表為樹(shù)形結(jié)構(gòu),且最后一級(jí)節(jié)點(diǎn)懶加載;(數(shù)據(jù)量大時(shí),可以有效提高加載速度)
(2)右側(cè)選中的列表無(wú)樹(shù)形結(jié)構(gòu),為左側(cè)選中的所有節(jié)點(diǎn)


到此這篇關(guān)于基于el-tree實(shí)現(xiàn)懶加載穿梭條的文章就介紹到這了,更多相關(guān)el-tree懶加載穿梭條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解nginx配置vue h5 history去除#號(hào)
這篇文章主要介紹了詳解nginx配置vue h5 history去除#號(hào),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
詳解關(guān)于vue-area-linkage走過(guò)的坑
這篇文章主要介紹了詳解關(guān)于vue-area-linkage走過(guò)的坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
詳解Vue2+Echarts實(shí)現(xiàn)多種圖表數(shù)據(jù)可視化Dashboard(附源碼)
本篇文章主要介紹了詳解Vue2+Echarts實(shí)現(xiàn)多種圖表數(shù)據(jù)可視化Dashboard(附源碼),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
使用Vue與WebSocket創(chuàng)建實(shí)時(shí)通知系統(tǒng)
在現(xiàn)代應(yīng)用開(kāi)發(fā)中,實(shí)時(shí)性已成為用戶(hù)體驗(yàn)的一個(gè)重要組成部分,ue 作為一款流行的前端框架,配合 WebSocket,可以輕松構(gòu)建實(shí)時(shí)通知系統(tǒng),在本文中,我們將通過(guò)一個(gè)簡(jiǎn)單的示例,使用 Vue 3 的 Composition API(setup 語(yǔ)法糖)來(lái)創(chuàng)建一個(gè)實(shí)時(shí)通知系統(tǒng)2024-11-11
vue單頁(yè)應(yīng)用在頁(yè)面刷新時(shí)保留狀態(tài)數(shù)據(jù)的方法
今天小編就為大家分享一篇vue單頁(yè)應(yīng)用在頁(yè)面刷新時(shí)保留狀態(tài)數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
說(shuō)說(shuō)Vuex的getters屬性的具體用法
這篇文章主要介紹了說(shuō)說(shuō)Vuex的getters屬性的具體用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
Vue項(xiàng)目打包成exe可執(zhí)行文件的實(shí)現(xiàn)過(guò)程(無(wú)瑕疵,完美版)
突然接到公司需求,說(shuō)客戶(hù)想讓我們把項(xiàng)目直接打包,所以下面這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目打包成exe可執(zhí)行文件的實(shí)現(xiàn)過(guò)程,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11

