基于el-tree實現(xiàn)懶加載穿梭條的示例代碼
一、關(guān)鍵代碼
<template>
<div>
<!-- 左側(cè)待選列表 -->
<div class="left-box">
<p>待選列表</p>
<el-input placeholder="輸入關(guān)鍵詞過濾" 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)鍵詞過濾" 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é)點是否為葉子節(jié)點
// 配置禁選的節(jié)點
disabled: function(data, node) {
// 如這里配置父節(jié)點、帶有disable屬性的節(jié)點禁選
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)鍵詞過濾節(jié)點
filterNode(value, data) {
if(!value) return true;
// labeName 為defaultProps中配置的label值,未配置默認(rèn)為label
return data.labeName.indexOf(value) !== -1;
},
// 懶加載出樹結(jié)構(gòu)的最后一層節(jié)點
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é)點
removeData() {
// 右側(cè)選中節(jié)點
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é)點
removeData() {
// 獲取左側(cè)選中節(jié)點,作為右側(cè)的數(shù)據(jù)
let checkNodes = this.$refs.treeLeft.getCheckedNodes();
let checkKeys = this.$refs.treeLeft.getCheckedKeys();
this.rightData = checkNodes;
},
}
</script>
?? 過濾節(jié)點函數(shù):filterNode
1、watch 監(jiān)聽關(guān)鍵詞;filterNode 必須有返回值,否則數(shù)據(jù)顯示不出來;
2、關(guān)鍵詞不為空時,函數(shù)的返回值 data.labeName.indexOf(value) !== -1; 其中 labeName 為defaultProps中配置的label值,未配置默認(rèn)為label
?? 異步加載函數(shù):loadNode
根據(jù) node.level 去匹配數(shù)據(jù)層級,判斷是否需要調(diào)用接口獲取數(shù)據(jù)
?? 樣式自定義
二、最終效果:(效果圖僅供參考)
(1) 左側(cè)列表為樹形結(jié)構(gòu),且最后一級節(jié)點懶加載;(數(shù)據(jù)量大時,可以有效提高加載速度)
(2)右側(cè)選中的列表無樹形結(jié)構(gòu),為左側(cè)選中的所有節(jié)點


到此這篇關(guān)于基于el-tree實現(xiàn)懶加載穿梭條的文章就介紹到這了,更多相關(guān)el-tree懶加載穿梭條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Ant design vue table 單擊行選中 勾選checkbox教程
這篇文章主要介紹了Ant design vue table 單擊行選中 勾選checkbox教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue自定義標(biāo)簽和單頁面多路由的實現(xiàn)代碼
這篇文章主要介紹了vue自定義標(biāo)簽和單頁面多路由的實現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
深入解析el-col-group強大且靈活的Element表格列組件
這篇文章主要為大家介紹了el-col-group強大且靈活的Element表格列組件深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
關(guān)于vue屬性使用和不使用冒號的區(qū)別說明
這篇文章主要介紹了關(guān)于vue屬性使用和不使用冒號的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

