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>
相关推荐
前端布道师37 分钟前
Web响应式:列表自适应布局
前端
ZeroTaboo39 分钟前
rmx:给 Windows 换一个能用的删除
前端·后端
哈里谢顿40 分钟前
Vue 3 入门完全指南:从零构建你的第一个响应式应用
vue.js
李剑一1 小时前
Vue实现大屏获取当前所处城市及当地天气(纯免费)
前端
踢足球09291 小时前
寒假打卡:2026-2-7
java·开发语言·javascript
_果果然1 小时前
这 7 个免费 Lottie 动画网站,帮你省下一个设计师的工资
前端
QT.qtqtqtqtqt1 小时前
uni-app小程序前端开发笔记(更新中)
前端·笔记·小程序·uni-app
楚轩努力变强1 小时前
iOS 自动化环境配置指南 (Appium + WebDriverAgent)
javascript·学习·macos·ios·appium·自动化
Aliex_git2 小时前
跨域请求笔记
前端·网络·笔记·学习
John_ToDebug2 小时前
引擎深处的漫游者:构建浏览器JavaScript引擎的哲学与技艺
javascript·chrome·js