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)展示 需要特殊處理,不要寫(xiě)死 --> <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="班次類(lèi)型" /> </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, //開(kāi)始索引,用于分批渲染的 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)文章
詳細(xì)聊聊vue組件是如何實(shí)現(xiàn)組件通訊的
組件間通信簡(jiǎn)單來(lái)說(shuō)就是組件間進(jìn)行數(shù)據(jù)傳遞,就像我們?nèi)粘5拇螂娫?就是通訊的一種方式,下面這篇文章主要給大家介紹了關(guān)于vue組件是如何實(shí)現(xiàn)組件通訊的相關(guān)資料,需要的朋友可以參考下2022-05-05webpack開(kāi)發(fā)vue-cli的項(xiàng)目實(shí)踐
本文主要介紹了webpack開(kāi)發(fā)vue-cli的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07一文詳解Vue.js生產(chǎn)環(huán)境文件及優(yōu)化策略
隨著 Vue.js 在前端開(kāi)發(fā)中的普及,如何高效地將 Vue 項(xiàng)目部署到生產(chǎn)環(huán)境成為了開(kāi)發(fā)者關(guān)注的重點(diǎn),本文將詳細(xì)解析 Vue.js 生產(chǎn)環(huán)境文件的使用方法、優(yōu)缺點(diǎn)以及優(yōu)化策略,需要的朋友可以參考下2024-12-12Vue render渲染時(shí)間戳轉(zhuǎn)時(shí)間,時(shí)間轉(zhuǎn)時(shí)間戳及渲染進(jìn)度條效果
這篇文章主要介紹了Vue render渲染時(shí)間戳轉(zhuǎn)時(shí)間,時(shí)間轉(zhuǎn)時(shí)間戳及渲染進(jìn)度條效果,通過(guò)實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07vue實(shí)現(xiàn)頁(yè)面打印自動(dòng)分頁(yè)的兩種方法
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)頁(yè)面打印自動(dòng)分頁(yè)的兩種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09解決vue下載后臺(tái)傳過(guò)來(lái)的亂碼流的問(wèn)題
這篇文章主要介紹了解決vue下載后臺(tái)傳過(guò)來(lái)的亂碼流的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12VUE3中Element table表頭動(dòng)態(tài)展示合計(jì)信息
本文主要介紹了在Vue中實(shí)現(xiàn)動(dòng)態(tài)合計(jì)兩個(gè)字段并輸出摘要信息的方法,通過(guò)使用監(jiān)聽(tīng)器和深度監(jiān)聽(tīng),確保當(dāng)數(shù)據(jù)變化時(shí)能正確更新合計(jì)結(jié)果,具有一定的參考價(jià)值,感興趣的可以了解一下2024-11-11解決打包后出現(xiàn)錯(cuò)誤y.a.addRoute is not a function的
這篇文章主要介紹了解決打包后出現(xiàn)y.a.addRoute is not a function的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10