欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

element 穿梭框性能优化的实现

程序员文章站 2022-06-24 09:50:15
目录背景解决思路新问题进阶背景穿梭框处理大数据量时,由于渲染的 dom 节点过多,造成页面卡顿的问题。在尽量不改变组件原有逻辑的前提下,进行优化。解决思路懒加载 - infinitescroll 组件...

背景

穿梭框处理大数据量时,由于渲染的 dom 节点过多,造成页面卡顿的问题。
在尽量不改变组件原有逻辑的前提下,进行优化。

解决思路

懒加载 - infinitescroll 组件
先从 packages/transfer 中将原组件拷出(或者改源码重新打包维护私有库使用)

v-infinite-scroll="pagedown"
:infinite-scroll-immediate="false"

添加到

<el-checkbox-group
        v-show="!hasnomatch && data.length > 0"
        v-model="checked"
        :size="size"
        :class="{ 'is-filterable': filterable }"
        class="el-transfer-panel__list"
        v-infinite-scroll="pagedown"
        :infinite-scroll-immediate="false"
      >
        <el-checkbox
          class="el-transfer-panel__item"
          :label="item[keyprop]"
          :disabled="item[disabledprop]"
          :key="item[keyprop]"
          v-for="item in filtereddata">
            <option-content :option="item"></option-content>
        </el-checkbox>
</el-checkbox-group>

在data中定义pagesize: 20 用来表示每页数据个数showdata: [] 仅用来展示使用,替换上述代码中实际需要操作的数据 filtereddata

 v-for="item in showdata">

同时在watch中相应的处理

data (data) {
    const checked = [];
    this.showdata = data.slice(0, this.pagesize);

    const filtereddatakeys = this.filtereddata.map(
    (item) => item[this.keyprop]
    );
    this.checked.foreach((item) => {
    if (filtereddatakeys.indexof(item) > -1) {
        checked.push(item);
    }
    });
    this.checkchangebyuser = false;
    this.checked = checked;
},
filtereddata (filtereddata) {
    this.showdata = filtereddata.slice(0, this.pagesize);
 }

初始化展示数量随意这里取 20。

最后添加滚动到底部时调用的方法

pagedown () {
    const l = this.showdata.length;
    const totallength = this.filtereddata.length
    l < totallength && 
    (this.showdata = this.filtereddata.slice(0, l + this.pagesize > totallength ?
    totallength : l + this.pagesize));
},

往下滚动的时候 展示的数据长度增加 20(数量随意), 超出时展示最大长度。

由此基本解决大数据量操作卡顿的问题。由于展示和逻辑层分开,组件的所有操作逻辑无须修改,最小程度减少差异。

新问题

手动滚动到列表末端,再进行搜索操作依然存在卡顿问题。

进阶

在滚动过程中,实际上顶端的数据依旧无法看见,该数据不展示,对用户体验也没有影响,
所以只需展示当前页的 20 条数据。

我们为el-checkbox-group添加一个 ref=scrollcontainer 以便操作滚动条,

在data中定义当前页数 curindex: 1

并对 pagedown 方法进行修改

    pagedown () {
      const totallength = this.filtereddata.length
      if((this.curindex*this.pagesize) < totallength){
        this.curindex ++
        const targetlength = this.curindex * this.pagesize 
        const endpoint = targetlength > totallength ? totallength : targetlength
        const startpoint = endpoint - this.pagesize  > 0 ? endpoint - this.pagesize : 0
        this.showdata = this.filtereddata.slice(startpoint, endpoint);
        this.$refs.scrollcontainer.$el.scrolltop = "1px" //滚动条到最上端,衔接下一页,为 0 可能会触发边界问题
      }
    }

为此我们还需要添加向上翻页的方法

infinitescroll 指令 只提供向下滚动,我们可以拓展该指令亦可自行添加上滑滚动监听
    mounted(){
        this.$refs.scrollcontainer.$el.addeventlistener('scroll', this.pageup)
    },
    beforedestroy(){
        this.$refs.scrollcontainer.$el.removeeventlistener('scroll', this.pageup)
    },

注册pageup 方法

    pageup(e){
      if(e.target.scrolltop ===0 && this.curindex>1){
        this.curindex --
        const endpoint = this.curindex * this.pagesize 
        const startpoint = (this.curindex-1)* this.pagesize 
        this.showdata = this.filtereddata.slice(startpoint, endpoint);
        const el = this.$refs.scrollcontainer.$el
        el.scrolltop = el.scrollheight - el.clientheight - 1 // 滚动到最底部,衔接上一页, -1 防止边界问题。
      }
    },

当进行数据操作的时候,页面内容变化,滚动条也会随之变化,为防止不能预知的翻页,数据改变时,重置滚动条和当前页码。

    initscroll(){
        this.curindex = 1
        this.$refs.scrollcontainer.$el.scrolltop = 0
    },

同时地,在watch中相应时候执行 initscroll

    data(){
        ...
        this.initscroll()
        ...
    },
    filtereddata (filtereddata) {
      ...
      this.initscroll()
    }

至此大数据量的穿梭框,性能大为改善。

到此这篇关于element 穿梭框性能优化的实现的文章就介绍到这了,更多相关element 穿梭框性能优化内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!