当前位置: 首页 > article >正文

element plus 使用 el-tree 组件设置默认选中和获取所有选中节点id

1. 设置默认选中:

使用 default-checked-keys 属性,设置默认要选中的节点,以数组形式,如下:

<el-tree
   ref="treeRef"
   :data="data"
   show-checkbox
   node-key="id"
   :props="defaultProps"
   :default-checked-keys="[5]"
/>

2. 调用 Tree 实例对象的 getCheckedKeys 方法来获取选中的所有节点,如下:

const treeRef = ref();
const checkedId = ref([]);

function getCheckedId() {
  checkedId.value = treeRef.value?.getCheckedKeys();
}

完整代码如下:

<template>
  <div>
    <el-tree
      ref="treeRef"
      :data="data"
      show-checkbox
      node-key="id"
      :props="defaultProps"
      :default-checked-keys="[5]"
    />
    <el-button @click="getCheckedId">获取选中节点: {{ checkedId }}</el-button>
  </div>
</template>

<script setup>
const defaultProps = {
  children: "children",
  label: "label",
};
const data = [
  {
    id: 1,
    label: "Level one 1",
    children: [
      {
        id: 2,
        label: "Level two 1-1",
        children: [
          {
            id: 3,
            label: "Level three 1-1-1",
          },
          {
            id: 4,
            label: "Level three 1-1-2",
          },
        ],
      },
    ],
  },
  {
    id: 5,
    label: "Level two 1",
  },
];
const treeRef = ref();
const checkedId = ref([]);

function getCheckedId() {
  checkedId.value = treeRef.value?.getCheckedKeys();
}
</script>


http://www.kler.cn/a/501032.html

相关文章:

  • Q_OBJECT宏报错的问题
  • Liunx-搭建安装VSOMEIP环境教程 执行 运行VSOMEIP示例demo
  • 网络安全-防火墙
  • DolphinScheduler自身容错导致的服务器持续崩溃重大问题的排查与解决
  • 第41章 使用 Docker Compose 进行容器迁移的技术指南及优势
  • 二十三种设计模式-原型模式
  • 深度学习张量的秩、轴和形状
  • HarmonyOS鸿蒙开发 弹窗及加载中指示器HUD功能实现
  • Redis数据库——Redis快的原因
  • Erlang语言的软件工程
  • ⭐MySQL的底层原理与架构
  • Unity编程与游戏开发-编程与游戏开发的关系
  • Helm部署activemq
  • 【1】Word:邀请函
  • Unity3D仿星露谷物语开发20之道具简介弹出窗
  • Python Selenium库入门使用,图文详细。附网页爬虫、web自动化操作等实战操作。
  • 【数据结构】航班查询系统:链表的实际运用
  • 大数据学习(34)-mapreduce详解
  • 指令的修饰符
  • STM32F103ZET6战舰版单片机开发板PCB文件 电路原理图