element-plus el-tree-v2大数据量勾选节点卡顿问题

卡顿原因解析 juejin.cn/post/731161...

html 复制代码
<template>
  <div>
    <button @click="setCheck">勾选</button>
    <el-tree-v2
        ref="treeRef"
        :data="data"
        :height="600"
        :props="defaultProps"
        show-checkbox
        :check-strictly="checkStrictly"
        :default-expanded-keys="[]"
        node-key="id"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue'

const checkStrictly = ref(false)
const treeRef = ref()
const data = ref([
  {
    id: 1,
    label: '一级 1',
    children: [
      {
        id: 4,
        label: '二级 1-1',
        children: [
          { id: 9, label: '三级 1-1-1' },
          { id: 10, label: '三级 1-1-2' }
        ]
      },
      { id: 99, label: 'asasa' },
      { id: 88, label: 'vcvcvc' }
    ]
  },
  {
    id: 2,
    label: '一级 2',
    children: [
      { id: 5, label: '二级 2-1' },
      { id: 6, label: '二级 2-2' }
    ]
  },
  {
    id: 3,
    label: '一级 3',
    children: [
      { id: 7, label: '二级 3-1' },
      { id: 8, label: '二级 3-2' }
    ]
  }
])

const defaultProps = reactive({
  children: 'children',
  label: 'label'
})

const ids = ref([10, 8])

// 初始化大数据量
for (let i = 0; i < 20000; i++) {
  ids.value.push(i + 100)
  data.value[0].children.push({
    id: i + 100,
    label: i.toString()
  })
}

/**
 * 勾选逻辑
 */
const setCheck = () => {
  console.time('勾选耗时')
  let node = null
  let obj = {};
  let key = null
  checkStrictly.value = true; // 父子节点不关联

  ids.value.map(item => {
    node = treeRef.value.getNode(item)

    key = node.parent.key || node.key

    treeRef.value.setChecked(item, true, true)

    // 用于,触发勾选函数,实现级联勾选
    if (!obj[key]) {
      obj[key] = node.data;
    }
  })

  checkStrictly.value = false;  // 父子节点关联

  // 触发勾选函数,实现级联勾选
  nextTick(() => {
    for (let key in obj) {
      treeRef.value.setChecked(obj[key], true);
    }
  });

  console.timeEnd('勾选耗时')
}
</script>
相关推荐
Dontla1 天前
Tailwind CSS介绍(现代CSS框架,与传统CSS框架Bootstrap对比)Tailwind介绍
前端·css·bootstrap
yinuo1 天前
uniapp微信小程序安卓手机Touchend事件无法触发
前端
你的人类朋友1 天前
【Node】Node.js 多进程与多线程:Cluster 与 Worker Threads 入门
前端·后端·node.js
闲人编程1 天前
使用Celery处理Python Web应用中的异步任务
开发语言·前端·python·web·异步·celery
excel1 天前
前端读取文件夹并通过 SSH 上传:完整实现方案 ✅
前端
一只游鱼1 天前
vue+springboot项目部署到服务器
服务器·vue.js·spring boot·部署
谢尔登1 天前
【Nest】日志记录
javascript·中间件·node.js
双向331 天前
【征文计划】基于Rokid CXR-M SDK 打造AI 实时会议助手:从连接到自定义界面的完整实践
前端
Lei活在当下1 天前
【业务场景架构实战】6. 从业务痛点到通用能力:Android 优先级分页加载器设计
前端·后端·架构
你的人类朋友1 天前
什么是基础设施中间件
前端·后端