vue最強(qiáng)table vxe-table 虛擬滾動(dòng)列表 前端導(dǎo)出問(wèn)題分析
最近遇到個(gè)問(wèn)題。后臺(tái)一次性返回2萬(wàn)條列表數(shù)據(jù)。
并且需求要求所有數(shù)據(jù)必須全部展示,不能做假分頁(yè)(不能優(yōu)化了)。
這些數(shù)據(jù)的直接來(lái)源就是CS客戶端。
他們做CS客戶端就是一次性加載幾萬(wàn)條數(shù)據(jù)不分頁(yè)(說(shuō)這是客戶的要求)。
我體驗(yàn)了一把CS客戶端,數(shù)萬(wàn)條數(shù)據(jù)放在那里,著實(shí)卡頓。
他們CS開發(fā)人員非他媽嘴硬說(shuō),這一點(diǎn)也不卡,極致順滑。
真尼瑪在這里掩耳盜鈴呢,是嗎?懶得跟他們廢話。
結(jié)論就是:永遠(yuǎn)不要和傻子講道理。
不廢話,開整吧。
既然需要一次性展示數(shù)萬(wàn)條數(shù)據(jù),那么element-table基本是不行了,畢竟也不能做個(gè)假分頁(yè)。
終于,茫茫人海,遇到了 vxe-table。
官方地址:https://vxetable.cn/#/table/start/install
最高支持10w+數(shù)據(jù)的流暢滾動(dòng)。確實(shí)厲害。
那么這么厲害的虛擬滾動(dòng),如何實(shí)現(xiàn)?
官方也一語(yǔ)道破。
沒(méi)錯(cuò),就是懶加載。界面上只是在窗口可視區(qū)域范圍內(nèi)加載數(shù)據(jù),隨著鼠標(biāo)滾動(dòng),再繼續(xù)加載下一波數(shù)據(jù)。
怎么用?
第一,寫下table模板
<vxe-table border show-overflow show-header-overflow ref="tableRef" height="600" :row-config="{isCurrent: true, isHover: true, useKey: true}" :column-config="{resizable: true}" :export-config="{}" :loading="demo1.loading" :sort-config="{trigger: 'cell'}" :checkbox-config="{checkField: 'checked'}"> <vxe-column type="seq" width="100" fixed="left"></vxe-column> <vxe-column type="checkbox" width="60" fixed="left"></vxe-column> <vxe-column field="attr0" title="Attr0" width="200" sortable></vxe-column> <vxe-column field="attr1" title="Attr1" width="200"></vxe-column> <vxe-column field="attr2" title="Attr2" width="200"></vxe-column> <vxe-column field="attr3" title="Attr3" width="200"></vxe-column> <vxe-column field="attr4" title="Attr4" width="200"></vxe-column> <vxe-column field="attr5" title="Attr5" width="200"></vxe-column> <vxe-column field="attr6" title="Attr6" width="300" sortable></vxe-column> <vxe-column field="attr7" title="Attr7" width="200" sortable></vxe-column> <vxe-column field="attr8" title="Attr8" width="200"></vxe-column> <vxe-column field="attr9" title="Attr9" width="200"></vxe-column> <vxe-column field="attr10" title="Attr10" width="200"></vxe-column> <vxe-column field="attr11" title="Attr11" width="200"></vxe-column> <vxe-column field="attr12" title="Attr12" width="200"></vxe-column> <vxe-column field="attr13" title="Attr14" width="200"></vxe-column> <vxe-column field="attr14" title="Attr14" width="200"></vxe-column> <vxe-column field="attr15" title="Attr15" width="200"></vxe-column> <vxe-column field="attr16" title="Attr16" width="200" fixed="right"></vxe-column> </vxe-table>
這樣子,看起來(lái)和element-table幾乎沒(méi)有區(qū)別。只是注意一些常用字段,改了下名字。
第二,寫下數(shù)據(jù)請(qǐng)求方法
但是你發(fā)現(xiàn)這里沒(méi)有綁定數(shù)據(jù),也就是 :data="tableData"。
仔細(xì)想想朋友,10萬(wàn)+數(shù)據(jù)你交給vue雙向綁定,你是要累死誰(shuí)???不合適啊,這么干。所以,官方提供了一個(gè)方法:reloadData。
// 第一步: 接口請(qǐng)求后臺(tái)數(shù)據(jù)(可能是10萬(wàn)條數(shù)據(jù)) new Promise(resolve => { // 注意 這里引入接口方法 getList().then(res => { // 在這里將請(qǐng)求的列表數(shù)據(jù) 返回出promise resolve(res ? res : []) }) }).then(list => { /* * 在這里開始給vxe-table數(shù)據(jù)了 */ // 1. 首先通過(guò) $refs(vue內(nèi)部方法,或者原生獲取vxe-table這個(gè)dom) const VXE_TABLE = this.$refs.tableRef; // 2. 通過(guò)這個(gè)dom下掛載的方法 reloadData 方法 取數(shù)據(jù) VXE_TABLE..reloadData(list).then(() => { // 如果你有l(wèi)oading的話 可以在這里關(guān)閉 }) // 至此, 數(shù)據(jù)接收完畢。 }).catch(err => { console.log('請(qǐng)求數(shù)據(jù)報(bào)錯(cuò)了 -- ',err); })
第三,自定義(翻譯)字段
總有一些需要自己翻譯的字段。實(shí)際上和element-table很類似。
<template #default="{ row }"> vxe-button @click="showDetailEvent(row)">彈框{{ row.name }}</vxe-button> </template>
還是插槽。更多插槽,請(qǐng)移步官方文檔:vxe-table v4
第四,高亮某一行,跳轉(zhuǎn)到某一行
<vxe-table border ref="tableRef" height="300" :row-config="{isCurrent: true, isHover: true}" :data="tableData" @current-change="currentChangeEvent"> <vxe-column field="name" title="Name"></vxe-column> <vxe-column field="sex" title="Sex"></vxe-column> <vxe-column field="age" title="Age"></vxe-column> <vxe-column field="address" title="Address" show-overflow></vxe-column> </vxe-table>
配置 row-config.
然后寫一個(gè)方法 setCurrentRow.
// 老規(guī)矩 還是獲取table這個(gè)dom // talbeData[0] 設(shè)置第一行數(shù)據(jù)高亮 this.$refs.tableRef.setCurrentRow(talbeData[0])
等等?剛才說(shuō),這個(gè)data沒(méi)有綁定。那么我們應(yīng)該去哪里拿到這個(gè)數(shù)據(jù)呢?
// 通過(guò) reloadData方法獲取數(shù)據(jù)的 通過(guò)下邊方式 拿到所有的table數(shù)據(jù) const TABLE_DATA = this.$refs.tableRef.allFullData; // 然后就可以愉快的設(shè)置某一行數(shù)據(jù)了 index 就是你需要哪一行數(shù)據(jù)高亮 this.$refs.tableRef.setCurrentRow(TABLE_DATA[index]) // 想要一鍵刪除所有高亮 那么就直接調(diào)用 clearCurrentRow this.$refs.tableRef.clearCurrentRow // 如果需要跳轉(zhuǎn)到某一行 比如 跳轉(zhuǎn)定位到 第 100 行 // 首先我們要知道每一行的高度, 比如每一行高度是 40 px // 那么需要dom滾動(dòng)的距離就是 index * 40 // 所以 就是如下 document.querySelector('.body--wrapper').scrollTop = index * 40; // 至此,跳轉(zhuǎn)成功
第五,導(dǎo)出數(shù)據(jù)
本來(lái)這個(gè)導(dǎo)出數(shù)據(jù)這個(gè)事,后臺(tái)返回個(gè)流,前端處理下就完事了。
但是這個(gè)table組件厲害了??梢灾苯油ㄟ^(guò)內(nèi)置方法,直接導(dǎo)出數(shù)據(jù)。
內(nèi)置的格式有 txt,html,csv等等。
說(shuō)實(shí)話,也夠用了。
// 例如 導(dǎo)出csv文件 使用內(nèi)置 $table.exportData({ type: 'csv' }) 方法 this.$refs.tableRef.exportData({type: 'csv'})
但是,需求要求我們導(dǎo)出pdf(wocao)。
對(duì)不起,失態(tài)了。
那么,我們也可以利用 vxe-table 的插件實(shí)現(xiàn)該功能。
// 首先肯定是要安裝這個(gè)插件了 npm install xe-utils vxe-table@next vxe-table-plugin-export-pdf@next jspdf // 沒(méi)錯(cuò) 基于 jspdf做的插件 // 然后 入口文件引入 import VXETable from 'vxe-table' import VXETablePluginExportPDF from 'vxe-table-plugin-export-pdf' // ... VXETable.use(VXETablePluginExportPDF) // 然后配置字體 // 需要注意的是 中文尤其要配字體 不然導(dǎo)出就是亂碼 VXETablePluginExportPDF.setup({ // Set default font fontName: 'SourceHanSans-Normal', // Font maps fonts: [ { // Font name fontName: 'SourceHanSans-Normal', // Font library url fontUrl: 'https://cdn.jsdelivr.net/npm/vxe-table-plugin-export-pdf/fonts/source-han-sans-normal.js' } ] }) // 以上配置完畢之后 在具體的組件業(yè)務(wù)中 就可以使用導(dǎo)出pdf功能 // 導(dǎo)出功能如下 this.$refs.tableRef.exportData({ filename: 'Order details', sheetName: 'Order details ( X02514645652 )', type: 'pdf' })
與此同時(shí),你可以導(dǎo)出 xlsx格式的。
具體不再細(xì)說(shuō)。請(qǐng)參考官方文檔:vxe-table v4 集成第三方
至此,導(dǎo)出部分也講完了。
但是,導(dǎo)出功能最后交給后臺(tái),畢竟這前端不擅長(zhǎng)干這事。
結(jié)束了。
到此這篇關(guān)于vue最強(qiáng)table vxe-table 虛擬滾動(dòng)列表 前端導(dǎo)出的文章就介紹到這了,更多相關(guān)vue table vxe-table 虛擬滾動(dòng)列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于vue-cli 打包時(shí)抽離項(xiàng)目相關(guān)配置文件詳解
下面小編就為大家分享一篇基于vue-cli 打包時(shí)抽離項(xiàng)目相關(guān)配置文件詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03100行代碼理解和分析vue2.0響應(yīng)式架構(gòu)
通過(guò)100行代碼幫助大家理解和分析vue2.0響應(yīng)式架構(gòu)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03vue中使用go()和back()兩種返回上一頁(yè)的區(qū)別說(shuō)明
這篇文章主要介紹了vue中使用go()和back()兩種返回上一頁(yè)的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07vue-admin-template解決登錄和跨域問(wèn)題解決
本文主要介紹了vue-admin-template解決登錄和跨域問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05基于Vue.js實(shí)現(xiàn)簡(jiǎn)單搜索框
這篇文章主要為大家詳細(xì)介紹了基于Vue.js實(shí)現(xiàn)簡(jiǎn)單搜索框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11vue+element下日期組件momentjs轉(zhuǎn)換賦值問(wèn)題解決
這篇文章主要介紹了vue+element下日期組件momentjs轉(zhuǎn)換賦值問(wèn)題,記錄下使用momentjs轉(zhuǎn)換日期字符串賦值給element的日期組件報(bào)錯(cuò)問(wèn)題,需要的朋友可以參考下2024-02-02Vue CLI3搭建的項(xiàng)目中路徑相關(guān)問(wèn)題的解決
這篇文章主要介紹了Vue CLI3搭建的項(xiàng)目中路徑相關(guān)問(wèn)題的解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue 數(shù)據(jù)雙向綁定的實(shí)現(xiàn)方法
這篇文章主要介紹了vue 數(shù)據(jù)雙向綁定的實(shí)現(xiàn)方法,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-03-03