el-tree懶加載的實(shí)現(xiàn)以及局部刷新方式
el-tree懶加載的實(shí)現(xiàn)及局部刷新
整個(gè)樹結(jié)構(gòu)如下
使用懶加載實(shí)現(xiàn),第一個(gè)按鈕可以折疊收縮,第二個(gè)按鈕刷新,第三個(gè)按鈕新增第一級(jí)節(jié)點(diǎn)

新增第一級(jí)節(jié)點(diǎn)的彈窗

右側(cè)懸浮顯示操作按鈕

在第一級(jí)右側(cè)點(diǎn)擊按鈕新增第二級(jí)節(jié)點(diǎn)的彈窗

html代碼
<el-aside style="width: 15%;border-right: 1px solid rgb(238, 238, 238);">
<div style="display:flex">
<div style="width:86%">
<el-input class="tree-search-input" clearable prefix-icon="el-icon-search" placeholder="輸入關(guān)鍵字進(jìn)行過濾" size="mini" v-model="filterText" @input="filterChange($event)"></el-input>
</div>
<div style="display:flex;align-items:center;">
<i style="margin-right:5px;font-size: 18px;font-weight: bold;" :title="expandNode?'收起':'展開'" :class="{'el-icon-full-screen':!expandNode,'el-icon-crop':expandNode}" @click="nodeExpand"></i>
<i title="刷新" class="el-icon-refresh" style="margin-right:5px;font-size: 18px;font-weight: bold;" @click="getQmsProductTree"></i>
<i title="新增" class="el-icon-plus" style="font-size: 18px;font-weight: bold;" @click="showDialog({NAME:'第一級(jí)目錄',ID:0},'add')"></i>
</div>
</div>
<el-tree class="my-el-tree" :data="treeData" highlight-current :expand-on-click-node="true" ref="elTree" :props="defaultProps" @node-click="handleNodeClick" :default-expand-all="expandNode" :filter-node-method="filterNode" lazy :load="loadNode" node-key="ID">
<span class="custom-tree-node" slot-scope="{ node, data }">
<span class="show-ellipsis" style="width: 90%" :title="node.NAME">{{ data.NAME }}</span>
<el-dropdown size="mini" @command="menuHandleCommand">
<span class="el-dropdown-link">
<i title="更多操作" class="el-icon-more"></i>
</span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item icon="el-icon-plus" :command="{ node: node, data, action: 'add' }">添加</el-dropdown-item>
<el-dropdown-item icon="el-icon-edit" :command="{ node: node, data, action: 'edit' }">編輯</el-dropdown-item>
<el-dropdown-item divided icon="el-icon-delete" :command="{ node: node, data, action: 'delete' }">刪除</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</span>
</el-tree>
</el-aside>
樹節(jié)點(diǎn)新增編輯操作彈窗
<el-dialog class="custom-class" :title="modularTitle" :visible.sync="showMenuDialog" v-if="showMenuDialog" width="40%"> <el-form label-width="130px" size="mini" ref="menuRuleForm" :model="menuRuleForm" :rules="menuRules"> <el-form-item v-if="menuState===0" label="父節(jié)點(diǎn):"> <el-input style="width: 90%" v-model="menuRuleForm.PARENT_NAME" disabled></el-input> </el-form-item> <el-form-item label="目錄名稱:" prop="NAME"> <el-input style="width: 90%" v-model="menuRuleForm.NAME" placeholder="請輸入目錄名"></el-input> </el-form-item> </el-form> <div align="center"> <el-button size="mini" type="primary" @click="saveMenu">保 存</el-button> <el-button size="mini" @click="closeMenuDialog">取 消</el-button> </div> </el-dialog>
js代碼,
refreshTreeNode方法如果是操作第一級(jí)節(jié)點(diǎn)就刷新整個(gè)樹,如果操作的二級(jí)或三級(jí)節(jié)點(diǎn)則局部刷新,
let node_ = this.$refs.elTree.getNode(id); node_.loaded = false; node_.expand();
通過id獲取父節(jié)點(diǎn),通過收縮展開父節(jié)點(diǎn)實(shí)現(xiàn)父節(jié)點(diǎn)的刷新
// 懶加載樹
loadNode:function(node, resolve) {
if (node.level === 0) {
return resolve([]);
}
axios({
url:`/magic/api/qms/tree/getChildrenData`,
method:'post',
data: {
TYPE: 'PRC',
ID: node.data.ID
}
}).then(res=>{
if(res && res.data && res.data.data) {
resolve(res.data.data);
}
})
},
//樹根節(jié)點(diǎn)加載
getQmsProductTree:function(){
axios({
url:`/tree/getChildrenData`,
method:'post',
data: {
TYPE: 'PRC'
}
}).then(res=>{
if(res && res.data && res.data.data) {
if(res.data.data.length>0){
this.treeData=res.data.data
this.dic_id = res.data.data[0].ID
}
}
})
},
//樹節(jié)點(diǎn)點(diǎn)擊加載列表
handleNodeClick:function(data, node, obj) {
this.dic_id=data.ID
this.initData(1);
},
//樹形控件收起與展開功能
nodeExpand:function(){
this.expandNode = !this.expandNode
let elTree = this.$refs.elTree;
for (var i = 0; i < elTree.store._getAllNodes().length; i++) {
elTree.store._getAllNodes()[i].expanded = this.expandNode;
}
},
//樹過濾
filterNode:function(value, data, node) {
if (!value) return true;
return data.NAME.indexOf(value) !== -1;
},
//類型樹形控件查詢功能
filterChange:function (val) {
this.$refs.elTree.filter(val);
},
// 刷新樹節(jié)點(diǎn)
refreshTreeNode:function(PARENT_ID) {
let id = PARENT_ID?PARENT_ID:this.menuRuleForm.PARENT_ID
if(id && id !== '0'){
let node_ = this.$refs.elTree.getNode(id)
node_.loaded = false;
node_.expand();
}else{
this.getQmsProductTree();
}
},
//初始化調(diào)用一次接口
init: function() {
this.getQmsProductTree();
this.initData(1);
},
//樹的按鈕增刪改事件
menuHandleCommand:function(command){
let data = command.data;
let action = command.action;
switch (action) {
case 'add':
this.showDialog(data, action);
break;
case 'edit':
this.showDialog(data, action);
break;
case 'delete':
this.delSysType(data);
break;
}
},
//點(diǎn)擊按鈕打開彈框添加菜單
showDialog:function(data, action) {
this.showMenuDialog = true;
if (data) {
if (action == 'add') {
this.modularTitle = '新增';
this.menuRuleForm = {
NAME:'',
PARENT_ID: data.ID,
PARENT_NAME: data.NAME
};
this.menuState = 0;
} else if (action == 'edit') {
this.modularTitle = '編輯';
this.menuRuleForm = {
NAME: data.NAME,
ID: data.ID,
PARENT_ID:data.PARENT_ID
};
this.menuState = 1;
}
}
},
//保存菜單
saveMenu:function(){
this.$refs.menuRuleForm.validate((valid)=>{
if(valid) {
let param = {
NAME: this.menuRuleForm.NAME
}
if(this.menuState === 0) {
param.PARENT_ID = this.menuRuleForm.PARENT_ID
param.TYPE = 'PRC'
}
if(this.menuState === 1) {
param.ID = this.menuRuleForm.ID
}
axios({
url:'/tree/save',
method:'post',
data: param
}).then(res=>{
if(res.data.state){
this.$message({type: 'success',message: res.data.message});
this.showMenuDialog = false;
this.refreshTreeNode()
}else{
this.$message({type: 'error',message: res.data.message});
}
})
}
})
},
//刪除樹數(shù)據(jù)
delSysType:function(data) {
this.$confirm('是否確定刪除?', '提示', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'warning',
}).then(() => {
axios({
url:'/tree/delete',
method:'post',
data: data
}).then(res=>{
if(res.data.state){
this.$message({type: 'success',message: res.data.message});
this.showMenuDialog = false;
this.refreshTreeNode(data.PARENT_ID)
}else{
this.$message({type: 'error',message: res.data.message});
}
})
});
},
//關(guān)閉菜單添加編輯按鈕
closeMenuDialog:function(){
this.showMenuDialog=false;
},
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)登錄時(shí)滑塊驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)登錄時(shí)滑塊驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue2.0+vue-router構(gòu)建一個(gè)簡單的列表頁的示例代碼
這篇文章主要介紹了vue2.0+vue-router構(gòu)建一個(gè)簡單的列表頁的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
簡單了解Vue computed屬性及watch區(qū)別
這篇文章主要介紹了通過實(shí)例解析Vue computed屬性及watch區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
解決Vue路由導(dǎo)航報(bào)錯(cuò):NavigationDuplicated:?Avoided?redundant?navig
這篇文章主要給大家介紹了關(guān)于解決Vue路由導(dǎo)航報(bào)錯(cuò):NavigationDuplicated:?Avoided?redundant?navigation?to?current?location的相關(guān)資料,這是最近做項(xiàng)目時(shí)候遇到的一個(gè)問題,現(xiàn)將解決辦法分享出來,需要的朋友可以參考下2023-01-01
element-ui Upload上傳組件動(dòng)態(tài)配置action方式
這篇文章主要介紹了element-ui Upload上傳組件動(dòng)態(tài)配置action方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Vue通過for循環(huán)隨機(jī)生成不同的顏色或隨機(jī)數(shù)的實(shí)例
今天小編就為大家分享一篇Vue通過for循環(huán)隨機(jī)生成不同的顏色或隨機(jī)數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
用Vue?Demi同時(shí)支持Vue2和Vue3的方法
這篇文章主要介紹了用Vue?Demi同時(shí)支持Vue2和Vue3的方法,實(shí)際開發(fā)中,同一個(gè)API在不同的版本中可能導(dǎo)入的來源不一樣,比如ref方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
利用Vue3 (一)創(chuàng)建Vue CLI 項(xiàng)目
這篇文章主要介紹利用Vue3 創(chuàng)建Vue CLI 項(xiàng)目,下面文章內(nèi)容附有官方文檔鏈接,安裝過程,需要的可以參考一下2021-10-10

