vue3實現(xiàn)el-table分批渲染表格
因最近工作中遇到了無分頁情景下頁面因大數(shù)據(jù)量卡頓的問題,在分別考慮并嘗試了懶加載、虛擬滾動、分批渲染等各個方法后,最后決定使用分批渲染來解決該問題。
代碼實現(xiàn)
表格代碼
<el-table
:data="currTableData"
border
style="width: 100%;"
:max-height="getMaxHeight()"
:cell-style="CellStyle"
@cell-click="handleCellClick"
>
<!--姓名列-->
<el-table-column
style="background-color: #fff;"
:align="'center'"
prop="userName"
label="姓名"
width="80"
fixed/>
<!--工號-->
<el-table-column
v-for="(item, index) in filteredCfgColumns"
:key="index"
style="background-color: #fff;"
:align="'center'"
:prop="item.prop"
:label="item.label"
/>
<!--
這一塊牽扯到合并列及周期模式切換后的動態(tài)展示
需要特殊處理,不要寫死
-->
<el-table-column
v-for="(date, index) in dateHeaders"
:key="index"
:align="'center'"
:class-name="isWeekend(date)"
:label-class-name="isWeekend(date)"
:width="getColumnWidth()"
>
<!--星期幾/日期-->
<template #header>
<div>{{ getWeekDay(date) }}</div>
<div>{{ parseDate(date) }}</div>
</template>
<!--表格內(nèi)容 -->
<template #default="{row}">
<div
class="cell-content"
v-if="row[date]"
:data-cell-content="JSON.stringify(row[date])"
:class="`${row[date].cellKey}`"
>
<!-- 第一行 -->
<div v-if="pageSettingList.includes('顯示附加班')" class="row"
style="font-size: 8px;min-height: 12px; display: flex; align-items: center;">
<el-row style="width: 100%;">
<el-col :span="24" style="color: red;font-weight: 600;text-align: right;">
{{ row[date]?.attchDetail || '' }}
</el-col>
</el-row>
</div>
<!-- 第二行 -->
<div class="row"
style="font-size: 10px;min-height: 20px; display: flex; align-items: center;white-space: nowrap;overflow: hidden;">
<el-row style="width: 100%;">
<el-col :span="24" style="font-weight: 600;text-align: center;">
<StyledText :colorAndSchedules="colorAndSchedules"
:styledTexts="row[date]?.mainDetail || ''" />
</el-col>
</el-row>
</div>
<!-- 第三行 -->
<div class="row"
style="font-size: 8px;min-height: 12px; display: flex; align-items: center;">
<el-row style="width: 100%;">
<el-col :span="6" v-if="pageSettingList.includes('顯示上期排班')"
style="display: block;text-align: left;font-weight: 600;color: green;">
{{ 'A1' }}
</el-col>
<el-col :span="12" v-if="pageSettingList.includes('顯示申請班')"
style="display: block;text-align: center;font-weight: 600;color: green;">
{{ row[date]?.applyDetail || '' }}
</el-col>
<el-col :span="6"
style="display: block;text-align: left;font-weight: 600;color: green;">
<div class="tip-con">
<el-tooltip
style="position: absolute!important; right: 0;bottom: 0; color: red; font-size: 12px;"
placement="top"
v-if="isShowRemark(row[date]?.remarkInfo)">
<template #content>
<el-table :data="row[date]?.remarkInfo" style="width: 100%">
<el-table-column prop="shifts" label="班次名" width="180" />
<el-table-column prop="remark" label="備注" width="180" />
<el-table-column prop="type" label="班次類型" />
</el-table>
</template>
<el-icon><InfoFilled /></el-icon>
</el-tooltip>
</div>
</el-col>
</el-row>
</div>
</div>
</template>
</el-table-column>
</el-table>
分批渲染邏輯代碼
定義變量
startIndex: 0, //開始索引,用于分批渲染的 batchSize: 6, // 一次性渲染的條數(shù)
分批渲染方法
const currTableData = ref([])
const loadBatch = () => {
if (state.startIndex < props.tableData.length) {
const endIndex = Math.min(state.startIndex + state.batchSize, props.tableData.length);
currTableData.value = currTableData.value.concat(props.tableData.slice(state.startIndex, endIndex));
state.startIndex = endIndex;
requestAnimationFrame(loadBatch);
}
}
watch(() => props.tableData, newData => {
currTableData.value = []; // 重置數(shù)據(jù)
state.startIndex = 0;
loadBatch()
}, { immediate: true })
效果


注
上面便是分批渲染表格的具體實現(xiàn)方式,可以看到這個表格是相當復雜的,哪怕是使用了分批渲染,第一次也用了6秒多的時間,可想而知如果一次性渲染幾百行幾千行,消耗的時間肯定會大大影響用戶體驗。當然,這種頁面性能的優(yōu)化不僅僅分批渲染一種手段,后面我會持續(xù)探索,如果有了新的手段,也會總結(jié)成文的。
到此這篇關(guān)于vue3實現(xiàn)el-table分批渲染表格的文章就介紹到這了,更多相關(guān)vue3 el-table分批渲染表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文詳解Vue.js生產(chǎn)環(huán)境文件及優(yōu)化策略
隨著 Vue.js 在前端開發(fā)中的普及,如何高效地將 Vue 項目部署到生產(chǎn)環(huán)境成為了開發(fā)者關(guān)注的重點,本文將詳細解析 Vue.js 生產(chǎn)環(huán)境文件的使用方法、優(yōu)缺點以及優(yōu)化策略,需要的朋友可以參考下2024-12-12
Vue render渲染時間戳轉(zhuǎn)時間,時間轉(zhuǎn)時間戳及渲染進度條效果
這篇文章主要介紹了Vue render渲染時間戳轉(zhuǎn)時間,時間轉(zhuǎn)時間戳及渲染進度條效果,通過實例代碼相結(jié)合的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
VUE3中Element table表頭動態(tài)展示合計信息
本文主要介紹了在Vue中實現(xiàn)動態(tài)合計兩個字段并輸出摘要信息的方法,通過使用監(jiān)聽器和深度監(jiān)聽,確保當數(shù)據(jù)變化時能正確更新合計結(jié)果,具有一定的參考價值,感興趣的可以了解一下2024-11-11
解決打包后出現(xiàn)錯誤y.a.addRoute is not a function的
這篇文章主要介紹了解決打包后出現(xiàn)y.a.addRoute is not a function的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10

