el-table樹形數(shù)據(jù)量過大,導(dǎo)致頁面卡頓問題及解決
更新時(shí)間:2024年04月25日 09:34:28 作者:青云ovo
這篇文章主要介紹了el-table樹形數(shù)據(jù)量過大,導(dǎo)致頁面卡頓問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
el-table樹形數(shù)據(jù)量過大,導(dǎo)致頁面卡頓問題
需求
el-table的 樹狀表格- 當(dāng)層級(jí)和數(shù)據(jù)過多的時(shí)候會(huì)出現(xiàn)點(diǎn)擊展開和折疊的時(shí)候卡頓
- 數(shù)據(jù)量大,頁面異??D
解決
- 采取 懶加載 的方式,將數(shù)據(jù) 一層層加上去。
- 可以在點(diǎn)擊展開的時(shí)候像后端請(qǐng)求,也可以從備份的全量數(shù)據(jù)里面找到對(duì)應(yīng)層級(jí)。
- 這種處理方式會(huì)在展開過多時(shí)慢慢變得卡頓。
注意在模板里添加
lazy
:load=“l(fā)oad”
:tree-props=“{children: ‘children', hasChildren: ‘hasChildren'}”// 模板
<el-table
row-key="planId"
ref="table"
:data="tableData"
lazy
:load="load"
:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
@selection-change="selectionChange"
>
<el-table-column type="selection" align="center" />
<el-table-column label="檢查明細(xì)" prop="inspectDetails" />
</table>
// 數(shù)據(jù)
data: () => ({ tableData: [], list: [], ids: []}),
//方法
mounted() {
this.search()
}
// 獲取數(shù)據(jù)
async search() {
// 這兒是將懶加載數(shù)據(jù)清除,防止調(diào)用方法出現(xiàn)頁面不更新的情況
this.$set(this.$refs.table.store.states, "lazyTreeNodeMap", {});
this.$set(this.$refs.table.store.states, "treeData", {});
const { rows } = await API_APPRAISE.APR_EXE_OS_R({
projectId: this.$route.query.id,
});
// 我這塊是將數(shù)據(jù)需要分組,進(jìn)行了分組 ,不需要的可以跳過這兒,比如從后端獲取到的直接是樹形數(shù)據(jù) 就可以是
// this.list = rows
// this.tableData = JSON.parse(JSON.stringify(this.list)).map(item => ({
// ..item,
// hasChildren: item.children && item.children.length > 0,
// children: null,
// idList: [item.planId],
// }))
let group = this.groupBy(rows, 'testOrgan');
// 備份數(shù)據(jù),全部數(shù)據(jù)
this.list = group.map(item => ({
id: item.id,
planId: item.id,
children: this.handleTree(item.children)
}));
// 展示數(shù)據(jù)
this.tableData = JSON.parse(JSON.stringify(this.list)).map(item => ({
...item,
// 這兒的ids 是每個(gè)節(jié)點(diǎn)的所有子節(jié)點(diǎn)及自己節(jié)點(diǎn)的planId, 我后續(xù)需要將它傳到后端處理,不需要可以刪除
ids: this.getTreeNodes(item),
// hasChildren 表示需要展示一個(gè)箭頭圖標(biāo)
hasChildren: item.children && item.children.length > 0,
// 只展示一層
children: null,
// 記住層級(jí)關(guān)系
idList: [item.planId],
}))
},
// 分組函數(shù)
groupBy(array, key) {
const groupedData = new Map();
for (const item of array) {
const group = item[key];
if (!groupedData.has(group)) {
groupedData.set(group, []);
}
groupedData.get(group).push(item);
}
return Array.from(groupedData, ([id, children]) => ({
id, children
}));
},
// 懶加載方法, 在點(diǎn)擊下拉時(shí)會(huì)調(diào)用
load (tree, treeNode, resolve) {
// 層級(jí)關(guān)系備份
const idCopy = JSON.parse(JSON.stringify(tree.idList))
// 查找下一層數(shù)據(jù)
let resolveArr = this.list
for (const planId of tree.idList) {
const tarItem = resolveArr.find((item) => item.planId === planId);
if (!tarItem) break;
resolveArr = tarItem.children || [];
}
// 處理下一層數(shù)據(jù)的屬性
resolveArr = JSON.parse(JSON.stringify(resolveArr))
resolveArr.forEach(item => {
item.ids = this.getTreeNodes(item)
item.hasChildren = item.children && item.children.length > 0
item.children = null
item.idList = [...idCopy, item.planId]
})
// 渲染子節(jié)點(diǎn)
resolve(resolveArr)
// 如果需要勾選 那么需要將每一個(gè)children重新掛載回去,再調(diào)用勾選的方法
this.$nextTick(() => {
tree.children = resolveArr
if (this.selectIds.includes(tree.paperId)) {
this.setChildrenSelect(tree.children, true)
}
})
},
/**
* getTreeNodes 遞歸函數(shù),用于遍歷樹形結(jié)構(gòu)
* @param {Object} node - 節(jié)點(diǎn)對(duì)象
* @returns {Array} - 返回包含節(jié)點(diǎn) planId 的數(shù)組
*/
getTreeNodes(node) {
const result = [];
// 遍歷當(dāng)前節(jié)點(diǎn),獲取 planId
if (node.planId) {
result.push(node.planId);
}
// 遍歷子節(jié)點(diǎn)
if (node.children && node.children.length > 0) {
for (const child of node.children) {
// 遞歸調(diào)用,獲取子節(jié)點(diǎn)的 planId
const childIds = this.getTreeNodes(child);
result.push(...childIds);
}
}
return result;
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3不能使用history.pushState修改url參數(shù)踩坑
這篇文章主要為大家介紹了vue3不能使用history.pushState修改url參數(shù)踩坑解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
使用 Vue-TCB 快速在 Vue 應(yīng)用中接入云開發(fā)的方法
這篇文章主要介紹了如何使用 Vue-TCB 快速在 Vue 應(yīng)用中接入云開發(fā),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
解決vue 子組件修改父組件傳來的props值報(bào)錯(cuò)問題
今天小編就為大家分享一篇解決vue 子組件修改父組件傳來的props值報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue單個(gè)組件實(shí)現(xiàn)無限層級(jí)多選菜單功能
這篇文章主要介紹了vue單個(gè)組件實(shí)現(xiàn)無限層級(jí)多選菜單的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04
vue2.0設(shè)置proxyTable使用axios進(jìn)行跨域請(qǐng)求的方法
這篇文章主要介紹了vue2.0設(shè)置proxyTable使用axios進(jìn)行跨域請(qǐng)求,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
vue項(xiàng)目中輪詢狀態(tài)更改方式(鉤子函數(shù))
這篇文章主要介紹了vue項(xiàng)目中輪詢狀態(tài)更改方式(鉤子函數(shù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue+element?ui表格添加多個(gè)搜索條件篩選功能(前端查詢)
這篇文章主要給大家介紹了關(guān)于vue+element?ui表格添加多個(gè)搜索條件篩選功能的相關(guān)資料,最近在使用element-ui的表格組件時(shí),遇到了搜索框功能的實(shí)現(xiàn)問題,需要的朋友可以參考下2023-08-08

