vue3實(shí)現(xiàn)el-table分批渲染表格
因最近工作中遇到了無(wú)分頁(yè)情景下頁(yè)面因大數(shù)據(jù)量卡頓的問(wèn)題,在分別考慮并嘗試了懶加載、虛擬滾動(dòng)、分批渲染等各個(gè)方法后,最后決定使用分批渲染來(lái)解決該問(wèn)題。
代碼實(shí)現(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/>
<!--工號(hào)-->
<el-table-column
v-for="(item, index) in filteredCfgColumns"
:key="index"
style="background-color: #fff;"
:align="'center'"
:prop="item.prop"
:label="item.label"
/>
<!--
這一塊牽扯到合并列及周期模式切換后的動(dòng)態(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('顯示申請(qǐng)班')"
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 })
效果


注
上面便是分批渲染表格的具體實(shí)現(xiàn)方式,可以看到這個(gè)表格是相當(dāng)復(fù)雜的,哪怕是使用了分批渲染,第一次也用了6秒多的時(shí)間,可想而知如果一次性渲染幾百行幾千行,消耗的時(shí)間肯定會(huì)大大影響用戶體驗(yàn)。當(dāng)然,這種頁(yè)面性能的優(yōu)化不僅僅分批渲染一種手段,后面我會(huì)持續(xù)探索,如果有了新的手段,也會(huì)總結(jié)成文的。
到此這篇關(guān)于vue3實(shí)現(xiàn)el-table分批渲染表格的文章就介紹到這了,更多相關(guān)vue3 el-table分批渲染表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)彈出框點(diǎn)擊空白頁(yè)彈框消失效果
這篇文章主要介紹了VUE實(shí)現(xiàn)彈出框點(diǎn)擊空白頁(yè)彈框消失,實(shí)現(xiàn)方法可以在Vue中實(shí)現(xiàn)彈出框然后通過(guò)點(diǎn)擊空白頁(yè)面來(lái)讓彈窗隱藏,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
vue3+Echarts頁(yè)面加載不渲染顯示空白頁(yè)面的解決
這篇文章主要介紹了vue3+Echarts頁(yè)面加載不渲染顯示空白頁(yè)面的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue實(shí)現(xiàn)將數(shù)據(jù)存入vuex中以及從vuex中取出數(shù)據(jù)
今天小編就為大家分享一篇vue實(shí)現(xiàn)將數(shù)據(jù)存入vuex中以及從vuex中取出數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue3+ts重復(fù)參數(shù)提取成方法多處調(diào)用以及字段無(wú)值時(shí)不傳字段給后端問(wèn)題
在進(jìn)行API開發(fā)時(shí),優(yōu)化參數(shù)傳遞是一個(gè)重要的考量,傳統(tǒng)方法中,即使參數(shù)值為空,也會(huì)被包含在請(qǐng)求中發(fā)送給后端,這可能會(huì)導(dǎo)致不必要的數(shù)據(jù)處理,而優(yōu)化后的方法則只會(huì)傳遞那些實(shí)際有值的字段,從而提高數(shù)據(jù)傳輸?shù)挠行院秃蠖颂幚淼男?/div> 2024-10-10
vue實(shí)現(xiàn)歌手列表字母排序下拉滾動(dòng)條側(cè)欄排序?qū)崟r(shí)更新
這篇文章主要介紹了vue實(shí)現(xiàn)歌手列表字母排序,下拉滾動(dòng)條側(cè)欄排序?qū)崟r(shí)更新,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Vue3?watchEffect的使用教程和相關(guān)概念
Vue?3?引入了?Composition?API,其中?watchEffect?是一個(gè)非常強(qiáng)大的工具,用于響應(yīng)式地追蹤依賴項(xiàng)的變化,本文將詳細(xì)介紹?watchEffect?的使用方法和相關(guān)概念,文中有詳細(xì)的代碼供大家參考,需要的朋友可以參考下2024-08-08最新評(píng)論

