vue實現table表格樹結構-使用懶載入時-解決子節點增刪改後,不重新整理子節點資料問題
阿新 • • 發佈:2022-03-27
借鑑部落格:https://blog.csdn.net/weixin_43953518/article/details/105557934
一、在使用element-ui的table元件時,使用樹形結構,並使用了懶載入,可出現了一個問題,在對當前節點新增一個子節點資料,或刪除一個子節點資料時,當前節點的子節點資料並不自動刷新出來。element-ui官方沒有提供實時重新整理子節點資料的方法。
解決辦法:
1、可以使用window.location.reload();但每次加減一個子節點資料,整個頁面都重新整理一下實在太不好看。而且節點會自動摺疊恢復剛進此頁面時的樣子。
最好的解決辦法就是:找到了開啟子節點資料懶載入時,更新資料的關鍵:this.$set(lazyTreeNodeMap, key, data);
lazyTreeNodeMap: 就是this.$refs.table.store.states.lazyTreeNodeMap key:就是table-key,相當於父節點資料的id data:就是子節點資料
怎麼使用,關鍵程式碼:
this.$set(this.$refs.table1.store.states.lazyTreeNodeMap, xxxList);
業務使用場景:
1、先給table標籤新增一個ref="table1"
2、在點選父節點要新增一個子節點,或刪除一個子節點後,已請求完後臺介面後,拿到父節點id,和最新增刪後的子節點資料xxxList
3、最後呼叫 this.$set(this.$refs.table1.store.states.lazyTreeNodeMap, id, xxxList);//根據父節點id更新子節點資料
我的業務處理場景:
1、table標籤新增ref="table1"
2、寫一個refreshRow(row)的方法:新增、刪除子節點後呼叫此方法更新子節點資料
refreshRow(){//=====================在新增、刪除、修改子節點後更新懶載入父節點方法。
//this.refreshParentRow為操作的父節點物件資料, //根據父節點id獲取子節點資料請求 this.$http.post('/menuController/queryMenuList', this.refreshParentRow).then(res => { console.log(res);if(res){ this.$set(this.$refs.table1.store.states.lazyTreeNodeMap, this.refreshParentRow.id, res.data.dataMap.menuListPage.list) }else{ this.$message({ message: '獲取菜子節點單資料列表list介面錯誤', type: 'fail' }); } }).catch((error) => { alert(error) }); },
。