vue3 基于el-tree增加、删除节点(非TypeScript 写法)

话不多说,直接贴代码

java 复制代码
<template>
  <div class="custom-tree-container">
    <!-- <p>Using render-content</p>
    <el-tree style="max-width: 600px" :data="dataSource" show-checkbox node-key="id" default-expand-all
      :expand-on-click-node="false" :render-content="renderContent" /> -->
    <p>Using scoped slot</p>
    <el-tree style="max-width: 600px" :data="dataSource" show-checkbox node-key="id" default-expand-all
      :expand-on-click-node="false">
      <template #default="{ node, data }">
        <span class="custom-tree-node">
          <span>{{ node.label }}</span>
          <span>
            <a @click="append(data)"> Append </a>
            <a style="margin-left: 8px" @click="remove(node, data)"> Delete </a>
          </span>
        </span>
      </template>
    </el-tree>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
// import {Node} from 'element-plus/es/components/tree/src/model/node'

export default {
  name: 'part',
  setup() {
    const state = reactive({
      dataSource: [
        {
          id: 1,
          label: 'Level one 1',
          children: [
            {
              id: 4,
              label: 'Level two 1-1',
              children: [
                {
                  id: 9,
                  label: 'Level three 1-1-1'
                }
              ]
            }
          ]
        }
      ]
    })

    const append = (data) => {
      let treeNodeId = data.$treeNodeId;
      console.info(data)
      // alert('当前id'+data.id)
      // alert(data.$treeNodeId)
      let id =data.id*100+1
      const newChild = { id: id, label: data.label+'-'+id, children: [] };
      if (!data.children) {
        data.children = [];
      }
      data.children.push(newChild);
      // state.dataSource = [data];
    }

    const remove = (node, data) => {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex(d => d.id === data.id);
      children.splice(index, 1);
      // dataSource.value = [...dataSource.value];
    };

    return {
      ...toRefs(state),
      append,
      remove
    }
  }
}




</script>

<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>

效果如下

相关推荐
李剑一13 分钟前
前端转AI要了解的技术,其他人不用看。前端架构基础之:让Js的计算运行在GPU上
前端·aigc·ai编程
IT_陈寒14 分钟前
Python的列表拷贝坑得我原地打转
前端·人工智能·后端
恋猫de小郭20 分钟前
Dart 3.13 的到底改了什么?为什么很重要?有什么坑?
android·前端·flutter
这是个栗子26 分钟前
前端开发中的常用工具函数(十):JavaScript 数组遍历
javascript·map·遍历·filter·foreach·for·for...of
FYKJ_201034 分钟前
django学习成绩预警系统10905
java·javascript·spring boot·python·spark·django·php
云浪35 分钟前
给大模型装上"手":三分钟看懂 AI Agent 的 Tool 工具调用
javascript·人工智能·node.js
风月说与山鬼36 分钟前
六、React事件
前端·javascript·react.js
岁岁种桃花儿37 分钟前
Vue核心语法第十四篇:列表过滤
前端·javascript·vue.js
晓得迷路了37 分钟前
栗子前端技术周刊第 143 期 - npm 攻击事件、Bun 1.4...
前端·javascript
YHHLAI37 分钟前
实战 Vibe Coding:用 AI 从零搭一个 React 待办清单
前端·人工智能·react.js