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>
相关推荐
平头哥~3 小时前
Day 07 _ 那条 1px 的线,为什么在手机上忽粗忽细
前端·css·样式·学习资料
小江的记录本3 小时前
【CSS】CSS 动画:transition、animation、transform、CSS3 新特性(附《思维导图》)
前端·css·安全·spring·前端框架·css3·动画
circuitsosk3 小时前
任务规划器的三种范式对比:ReAct、Plan-and-Execute 与 Tree-of-Thought 在真实业务中的取舍
前端·javascript·python·react.js·llm·ai agent
hzxpaipai4 小时前
企业官网技术架构拆解:前端、后台、数据库、服务器如何协同
前端·数据库·架构
小磊哥er4 小时前
深入解构Claude Code - 第 12 篇 · 整体串起来
javascript·ai编程
小磊哥er4 小时前
深入解构Claude Code - 第 11 篇 · 工程上的讲究
javascript·ai编程
深念Y6 小时前
Lenovo XiaoXinAir 14 — 性能模式切换
前端·网络
计算机魔术师7 小时前
Meta突然杀进第一梯队:一个新模型,把AI推理成本打下来8倍
前端
IT_陈寒7 小时前
Redis缓存雪崩把我坑惨了,这次长记性了
前端·人工智能·后端