element?table?數(shù)據(jù)量大頁面卡頓的解決
element table數(shù)據(jù)量大頁面卡頓
table顯示醫(yī)院列表,這里后臺未做分頁,總共數(shù)據(jù)大約8000條。
一次性全部賦值給table整個頁面都會卡頓好幾秒。
查看了請求接口到數(shù)據(jù)返回的時間為192ms,可以接受。
應(yīng)該是頁面渲染的問題。
這邊就在前端做了分頁處理。
調(diào)用接口
// 獲取醫(yī)院列表 getHospitalList() { this.$api.Hospital.GetHospitalList().then(res => { if (res.status == 200) { this.tableData = res.data.response; this.total = res.data.response.length; } }); }, // 分頁 handleCurrentChange(currentPage) { this.currentPage = currentPage; }, // 搜索 searchList() { let params = ""; switch (this.select) { case "1": if (this.input3) { params = this.input3; this.$api.Hospital.QueryHospitalsByName(params).then(res => { if (res.data.length > 0) { this.tableData = res.data; this.currentPage = 1; this.total = res.data.length; } else { this.$message({ message: "未查詢到醫(yī)院信息", type: "info" }); } console.log(res); }); } break; case "2": if (this.input3) { params = this.input3; this.$api.Hospital.QueryHospitalsByCode(params).then(res => { if (res.data.length > 0) { this.tableData = res.data; this.currentPage = 1; this.total = res.data.length; } else { this.$message({ message: "未查詢到醫(yī)院信息", type: "info" }); } console.log(res); }); } break; default: console.log(111); } },
table組件
<el-table :data=" tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize ) " border style="width: 100%" height="400" size="mini" highlight-current-row > …… </el-table> <el-pagination layout="prev, pager, next" background :page-size="pageSize" :total="total" @current-change="handleCurrentChange" > </el-pagination>
data里使用到的數(shù)據(jù)
data(){ return { total: 0, currentPage: 1, pageSize: 50, } }
el-table大數(shù)據(jù)量渲染卡頓的一種思路
現(xiàn)需要呈現(xiàn)一個表格,有近500行,30多列,使用vue+elementUI呈現(xiàn)。
這個數(shù)據(jù)量不算大,但可能列數(shù)比較多,渲染時速度很慢,滾動會有卡頓,使用體驗不佳。
但并不想做分頁處理,想要盡可能接近excel的呈現(xiàn)。
思路
假設(shè)全部數(shù)據(jù)為allData(數(shù)組),現(xiàn)在使用一個displayData(數(shù)組),displayData = allData.slice(scorll, scorll+ displayCount),scroll表示當(dāng)前滾動到的index, displayCount表示要展示的行數(shù)。把displayData設(shè)為el-table的數(shù)據(jù)源,只渲染該部分?jǐn)?shù)據(jù)。通過對表格添加滾動事件監(jiān)聽,來動態(tài)更新scroll,并且對scroll添加watch,當(dāng)scroll發(fā)生變化,就自動更新displayData。
滾動監(jiān)聽
監(jiān)聽滾動需要考慮到兼容性,火狐是DOMMouseScroll,其他的是mousewheel。
?? ??? ??? ?/*指定table的ref*/ ? ? ? ? ? ? this.table = this.$refs.mytable.bodyWrapper; ?? ??? ??? ? ?? ??? ??? ?/*瀏覽器兼容*/ ? ? ? ? ? ? let userAgent = navigator.userAgent; //取得瀏覽器的userAgent字符串 ? ? ? ? ? ? let ff = userAgent.indexOf("Firefox") > -1; //判斷是否Firefox瀏覽器 ? ? ? ? ? ? ? ? ? ? ? if (ff) { ? ? ? ? ? ? ?this.table.addEventListener('DOMMouseScroll', (event) => { ? ? ? ? ? ? ? ? ? ? let detail = event.detail; ? ? ? ? ? ? ? ? ? ? //滾動方向 ? ? ? ? ? ? ? ? ? ? let down = detail > 0; ? ? ? ? ? ? ? ? ? ? /** ? ? ? ? ? ? ? ? ? ? 根據(jù)滾動方向和距離修改scroll的值,需要注意scroll的范圍不能越界。 ? ? ? ? ? ? ? ? ? ? **/ ? ? ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? this.table.addEventListener('mousewheel', (event) => { ? ? ? ? ? ? ? ? ? ? let wheel = event.deltaY; ? ? ? ? ? ? ? ? ? ? //滾動方向 ? ? ? ? ? ? ? ? ? ? let down = wheel > 0; ? ? ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? }
slider
除了滾動表格,還需要一個模擬滾動條。這里選用slider控件,和scroll綁定。
發(fā)現(xiàn)elementUI的slider數(shù)值方向只能從下到上,且不能有太多的定制化。找到另外一個可深度定制化的vue slider控件:vue-slider-component。通過參數(shù)配置及css修改使其盡可能像滾動條。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue electron前端開啟局域網(wǎng)接口實現(xiàn)流程詳細(xì)介紹
用electron寫了一個自己用的小軟件,無后端,純本地的數(shù)據(jù)。最近想著開發(fā)一個手機端app,將PC端的數(shù)據(jù)進(jìn)行同步。為了這小小的功能單獨寫個后端又麻煩。干脆前后端不分離哈哈,直接在前端軟件中開啟接口2022-10-10快速了解Vue父子組件傳值以及父調(diào)子方法、子調(diào)父方法
這篇文章主要介紹了Vue父子組件傳值以及父調(diào)子方法、子調(diào)父方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07vue+element的表格實現(xiàn)批量刪除功能示例代碼
這篇文章主要介紹了vue+element的表格實現(xiàn)批量刪除功能示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08想到頭禿也想不到的Vue3復(fù)用組件還可以這么hack的用法
這篇文章主要為大家介紹了想到頭禿也想不到的Vue3復(fù)用組件還可以這么hack的用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04