日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Element穿梭框性能優(yōu)化

本文轉(zhuǎn)載自微信公眾號「微醫(yī)大前端技術(shù)」,作者陳建波。轉(zhuǎn)載本文請聯(lián)系微醫(yī)大前端技術(shù)公眾號。

為錫林郭勒盟等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及錫林郭勒盟網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、做網(wǎng)站、錫林郭勒盟網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

element 穿梭框性能優(yōu)化

背景

穿梭框處理大數(shù)據(jù)量時,由于渲染的 DOM 節(jié)點過多,造成頁面卡頓的問題。在盡量不改變組件原有邏輯的前提下,進(jìn)行優(yōu)化。

解決思路

懶加載 - InfiniteScroll 組件

先從 packages/transfer 中將原組件拷出(或者改源碼重新打包維護(hù)私有庫使用

 
 
 
  1. v-infinite-scroll="pageDown" 
  2. :infinite-scroll-immediate="false" 

添加到

 
 
 
  1.         v-show="!hasNoMatch && data.length > 0" 
  2.         v-model="checked" 
  3.         :size="size" 
  4.         :class="{ 'is-filterable': filterable }" 
  5.         class="el-transfer-panel__list" 
  6.         v-infinite-scroll="pageDown" 
  7.         :infinite-scroll-immediate="false" 
  8.       > 
  9.         
  10.           class="el-transfer-panel__item" 
  11.           :label="item[keyProp]" 
  12.           :disabled="item[disabledProp]" 
  13.           :key="item[keyProp]" 
  14.           v-for="item in filteredData"> 
  15.              
  16.          
  17.  

在data中定義pageSize: 20 用來表示每頁數(shù)據(jù)個數(shù)showData: [] 僅用來展示使用,替換上述代碼中實際需要操作的數(shù)據(jù) filteredData

 
 
 
  1. v-for="item in showData"> 

同時在watch中相應(yīng)的處理

 
 
 
  1. data (data) { 
  2.     const checked = []; 
  3.     this.showData = data.slice(0, this.pageSize); 
  4.  
  5.     const filteredDataKeys = this.filteredData.map( 
  6.     (item) => item[this.keyProp] 
  7.     ); 
  8.     this.checked.forEach((item) => { 
  9.     if (filteredDataKeys.indexOf(item) > -1) { 
  10.         checked.push(item); 
  11.     } 
  12.     }); 
  13.     this.checkChangeByUser = false; 
  14.     this.checked = checked; 
  15. }, 
  16. filteredData (filteredData) { 
  17.     this.showData = filteredData.slice(0, this.pageSize); 
  18.  } 

初始化展示數(shù)量隨意這里取 20。

最后添加滾動到底部時調(diào)用的方法

 
 
 
  1. pageDown () { 
  2.     const l = this.showData.length; 
  3.     const totalLength = this.filteredData.length 
  4.     l < totalLength &&  
  5.     (this.showData = this.filteredData.slice(0, l + this.pageSize > totalLength ? 
  6.     totalLength : l + this.pageSize)); 
  7. }, 

往下滾動的時候 展示的數(shù)據(jù)長度增加 20(數(shù)量隨意), 超出時展示最大長度。

由此基本解決大數(shù)據(jù)量操作卡頓的問題。由于展示和邏輯層分開,組件的所有操作邏輯無須修改,最小程度減少差異。

新問題

手動滾動到列表末端,再進(jìn)行搜索操作依然存在卡頓問題。

進(jìn)階

在滾動過程中,實際上頂端的數(shù)據(jù)依舊無法看見,該數(shù)據(jù)不展示,對用戶體驗也沒有影響, 所以只需展示當(dāng)前頁的 20 條數(shù)據(jù)。我們?yōu)閑l-checkbox-group添加一個 ref=scrollContainer 以便操作滾動條,

在data中定義當(dāng)前頁數(shù) curIndex: 1

并對 pageDown 方法進(jìn)行修改

 
 
 
  1. pageDown () { 
  2.   const totalLength = this.filteredData.length 
  3.   if((this.curIndex*this.pageSize) < totalLength){ 
  4.     this.curIndex ++ 
  5.     const targetLength = this.curIndex * this.pageSize  
  6.     const endPoint = targetLength > totalLength ? totalLength : targetLength 
  7.     const startPoint = endPoint - this.pageSize  > 0 ? endPoint - this.pageSize : 0 
  8.     this.showData = this.filteredData.slice(startPoint, endPoint); 
  9.     this.$refs.scrollContainer.$el.scrollTop = "1px" //滾動條到最上端,銜接下一頁,為 0 可能會觸發(fā)邊界問題 
  10.   } 

為此我們還需要添加向上翻頁的方法 InfiniteScroll 指令 只提供向下滾動,我們可以拓展該指令亦可自行添加上滑滾動監(jiān)聽

 
 
 
  1. mounted(){ 
  2.         this.$refs.scrollContainer.$el.addEventListener('scroll', this.pageUp) 
  3.     }, 
  4.     beforeDestroy(){ 
  5.         this.$refs.scrollContainer.$el.removeEventListener('scroll', this.pageUp) 
  6.     }, 

注冊pageUp 方法

 
 
 
  1. pageUp(e){ 
  2.       if(e.target.scrollTop ===0 && this.curIndex>1){ 
  3.         this.curIndex -- 
  4.         const endPoint = this.curIndex * this.pageSize  
  5.         const startPoint = (this.curIndex-1)* this.pageSize  
  6.         this.showData = this.filteredData.slice(startPoint, endPoint); 
  7.         const el = this.$refs.scrollContainer.$el 
  8.         el.scrollTop = el.scrollHeight - el.clientHeight - 1 // 滾動到最底部,銜接上一頁, -1 防止邊界問題。 
  9.       } 
  10.     }, 

當(dāng)進(jìn)行數(shù)據(jù)操作的時候,頁面內(nèi)容變化,滾動條也會隨之變化,為防止不能預(yù)知的翻頁,數(shù)據(jù)改變時,重置滾動條和當(dāng)前頁碼。

 
 
 
  1. initScroll(){ 
  2.         this.curIndex = 1 
  3.         this.$refs.scrollContainer.$el.scrollTop = 0 
  4.     }, 

同時地,在watch中相應(yīng)時候執(zhí)行 initScroll

 
 
 
  1. data(){ 
  2.       ... 
  3.       this.initScroll() 
  4.       ... 
  5.   }, 
  6.   filteredData (filteredData) { 
  7.     ... 
  8.     this.initScroll() 
  9.   } 

至此大數(shù)據(jù)量的穿梭框,性能大為改善。


分享文章:Element穿梭框性能優(yōu)化
當(dāng)前鏈接:http://www.dlmjj.cn/article/ccdojpj.html