关于Element-UI的穿梭框数据量大时,点击‘全选’卡顿的解决方案


现象:
我们渲染了9999条数据,由于transfer组件会一次性渲染所有数据,所以一次性渲染这么多,卡个几十秒很正常好吧。所以懒加载或者分页是基本操作,方案二是分页操作。
懒加载的方式可以用EUI的无限滚动:https://element.eleme.cn/#/zh-CN/component/infiniteScroll
即便我们做了懒加载之后,点击全选依旧是卡顿6秒以上,所以方案一解决的是:即便做了懒加载或者分页操作后,用户点击分页,依旧会卡顿几秒的情况。
这个是因为transfer的源码中‘全选判断’代码性能差的原因,方案一就是修改transfer的源码。
我提交了一个pr,地址是: hhttps://github.com/ElemeFE/element/pull/20282
 
方案一:复制EUI的transfer组件,然后进行修改,再引入项目目录
EUI的transfer组件目录路径:node_modules\element-ui\packages\transfer,复制文件夹,然后放入vue项目路径的

在调用EUI的transfer的地方引入公共的组件transfer,

 1 <template>
 2   <Transfer v-model="value" :data="data"></Transfer>
 3 </template>
 4 
 5 <script>
 6 import Transfer from '../common/transfer'
 7 export default {
 8   components:{
 9     Transfer:Transfer
10   },
11  //省略
12 </script>

开始修改transfer代码:

打开src/common\transfer\src\transfer-panel.vue的组件,

找到updateAllChecked函数,updateAllChecked函数作用是:我们点击一个item就需要判断,看代码注释。

 1  updateAllChecked() {
 2       /*
 3         源码
 4         this.checkableData是对象数组  我们需要的是每个对象中的key
 5         所以checkableDataKeys保存着对象的key的数组 含义是'可通过点击进行选择的item项'的集合
 6       */
 7       let start = new Date().getTime();
 8       const checkableDataKeys = this.checkableData.map(
 9         item => item[this.keyProp]
10       );
11 
12       this.allChecked =
13         checkableDataKeys.length > 0 &&
14       /*
15         从2.4.0到现在都没改变 诶,不得不说开发团队是真的忙啊
16         this.checked保存着'用户通过点击item选中的item数组'
17         如果this.checked存在着checkableDataKeys的每一项的话,那么allChecked就是true,但凡有一项不存在就为false。allChecked代表是否全部选中了。
18         这里的时间复杂度是n^2,狠垃圾  
19       */
20       checkableDataKeys.every(item => this.checked.indexOf(item) > -1);
21       console.log("updateAllCheckedEnd", new Date().getTime() - start);
22 
23     },

来看源码的耗时:

然后我们开始重写updateAllChecked函数:

updateAllChecked() {
      /*
        修改
        这里就是高效数组中含有另一个数组的元素的算法
        构建元素对象
      */
      let start = new Date().getTime();
      let checkableDataKeys = this.checkableData.map((item) => {
        let keyProps = {};
        keyProps[item[this.keyProp]] = true;
        return keyProps;
      });
      // 通过对象的k-v对应,n(1)的方式寻找数组中是否存在某元素
      this.allChecked =
        checkableDataKeys.length > 0 &&
        this.checked.length > 0 &&
        this.checked.every((item) => checkableDataKeys[item]);
      // 上面被注释的源码是最耗时的,所有一直看耗时就可以了
      console.log("updateAllCheckedEnd", new Date().getTime() - start);
    },

这样性能就高好多了,其实就是基本的前端算法题,目测EUI的开发者是因为懒才不写的。

来看修改代码后的耗时:

明显快多了。

 接下来是文件:\src\common\transfer\src\main.vue,找到addToRight函数

 1 addToRight() {
 2       let currentValue = this.value.slice();
 3       const itemsToBeMoved = [];
 4       const key = this.props.key;
 5       let start = new Date().getTime();
 6       // 此处套了两层循环,耗时长
 7       this.data.forEach((item) => {
 8         const itemKey = item[key];
 9         if (
10           this.leftChecked.indexOf(itemKey) > -1 &&
11           this.value.indexOf(itemKey) === -1
12         ) {
13           itemsToBeMoved.push(itemKey);
14         }
15       });
16       console.log("addToRightEnd", new Date().getTime() - start);
17 
18       currentValue =
19         this.targetOrder === "unshift"
20           ? itemsToBeMoved.concat(currentValue)
21           : currentValue.concat(itemsToBeMoved);
22       this.$emit("input", currentValue);
23       this.$emit("change", currentValue, "right", this.leftChecked);
24     },

 移动选中的耗时:

修改addToRight函数,

 1  addToRight() {
 2       let start = new Date().getTime();
 3       let currentValue = this.value.slice();
 4       const itemsToBeMoved = [];
 5       const key = this.props.key;
 6 
 7       // 修改
 8       let leftCheckedKeyPropsObj = {};
 9       this.leftChecked.forEach((item, index) => {
10         leftCheckedKeyPropsObj[item] = true;
11       });
12 
13       let valueKeyPropsObj = {};
14       this.value.forEach((item, index) => {
15         valueKeyPropsObj[item] = true;
16       });
17       this.data.forEach((item) => {
18         const itemKey = item[key];
19         if ( leftCheckedKeyPropsObj[itemKey] && !valueKeyPropsObj[itemKey] ) {
20           itemsToBeMoved.push(itemKey);
21         }
22       });
23       console.log("addToRightEnd", new Date().getTime() - start);
24 
25       currentValue =
26         this.targetOrder === "unshift"
27           ? itemsToBeMoved.concat(currentValue)
28           : currentValue.concat(itemsToBeMoved);
29       this.$emit("input", currentValue);
30       this.$emit("change", currentValue, "right", this.leftChecked);
31     },

移动选中耗时:

耗时明显减少了,这方案的前提就是懒加载或者分页,我试了一下10w的数据量,依旧是不错的。

 

方案二:分页操作
 
分析
checkBox-group有个check数组(用来记录已经选中的item数组)和renderItem数组(实际渲染的item,由于是分页,所有不会渲染所有),
如果`check数组`中有`renderItem数组`的一项,那么该项就会被标记为已选,否则是未选。实现原理就是单纯的check数组和renderItem数组进行比较。
当用户点击全选的时候,check数组变成上万条数据的数组,此时我们渲染了100条数据,那么就要进行10000x100级别的循环,这就是耗时的原因所在。
其实,页面只渲染了100条数据,我们没必要将上万条数据一次性放入check数组中,我们只需要把这100条数组放入check数组,显示这100条数据为已选即可。当页面渲染了更多数据的同时,将新增的数据添加进check数组即可。这样性能大大提升。
 
方案
我采用的方案如下:
1. 只显示100条数据。
2. 下拉显示下100条数据,上拉显示上100条数据。
3. 当下拉或者上拉增加渲染数据的同时,把新增数据添加进check数组。

 

这些只是大致思路,我已经实现了。还有很多细节要处理,想要完善,还得利用对象的键值对实现删除等。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM