Ant-design-vue 树形控件tree 新增节点,删除节点,编辑节点的解决方案

Ant-design-vue 树形控件tree 新增节点,删除节点,编辑节点的解决方案

最近项目需求如下,想做一个菜单管理,用tree的形式,用过element-ui的都知道怎么处理,但是由于ant-design-vue并未提供操作节点方法,遂自己的解决方案如下(案例文件链接下载:案例文件链接下载在这里插入图片描述

<template>
  <div class="page-header-index-wide">
    <a-card :bordered="false">
      <a-button type="primary">添加顶级菜单</a-button>
      <a-tree
        :treeData="treeData"
      >
        <template slot="custom" slot-scope="item">
          <span>{{ item.title }}</span>
          <a-button
            type="primary"
            class="but_type"
            style="right:200px;"
            @click="()=> append(item)"
          >新增</a-button>
          <a-button
            type="primary"
            class="but_type"
            style="right:120px;"
            @click="()=> edit(item)"
          >编辑</a-button>
          <a-button type="primary" class="but_type" @click="(e)=> remove(item)">删除</a-button>
        </template>
      </a-tree>
    </a-card>
  </div>
</template>

<script>
const treeData = [
  {
    title: '0-0',
    key: '0-0',
    scopedSlots: { title: 'custom' },
    children: [
      {
        title: '0-0-0',
        key: '0-0-0',
        scopedSlots: { title: 'custom' },
        children: [
          { title: '0-0-0-0', key: '0-0-0-0', scopedSlots: { title: 'custom' } },
          { title: '0-0-0-1', key: '0-0-0-1', scopedSlots: { title: 'custom' } },
          { title: '0-0-0-2', key: '0-0-0-2', scopedSlots: { title: 'custom' } }
        ]
      },
      {
        title: '0-0-1',
        key: '0-0-1',
        scopedSlots: { title: 'custom' },
        children: [
          { title: '0-0-1-0', key: '0-0-1-0', scopedSlots: { title: 'custom' } },
          { title: '0-0-1-1', key: '0-0-1-1', scopedSlots: { title: 'custom' } },
          { title: '0-0-1-2', key: '0-0-1-2', scopedSlots: { title: 'custom' } }
        ]
      },
      {
        title: '0-0-2',
        key: '0-0-2',
        scopedSlots: { title: 'custom' }
      }
    ]
  },
  {
    title: '0-1',
    key: '0-1',
    scopedSlots: { title: 'custom' },
    children: [
      { title: '0-1-0-0', key: '0-1-0-0', scopedSlots: { title: 'custom' } },
      { title: '0-1-0-1', key: '0-1-0-1', scopedSlots: { title: 'custom' } },
      { title: '0-1-0-2', key: '0-1-0-2', scopedSlots: { title: 'custom' } }
    ]
  },
  {
    title: '0-2',
    key: '0-2',
    scopedSlots: { title: 'custom' }
  }
]

export default {
  data () {
    return {
      treeData,
    }
  },
  methods: {
    // 递归查找
    searchOption (option, arr, type = 'delect') {
      console.log(option, arr)
      for (let s = 0; s < arr.length; s++) {
        console.log(arr[s].key, option.key)
        if (arr[s].key === option.key) {
          if (type === 'delect') {
            arr.splice(s, 1)
          } else {
          //这是模拟数据编辑数据
            this.$set(arr, s, {
              title: '12121212',
              key: '12121212',
              scopedSlots: { title: 'custom' }
            })
          }
          break
        } else if (arr[s].children && arr[s].children.length > 0) { // 递归条件
          this.searchOption(option, arr[s].children)
        } else {
          continue
        }
      }
    },
    append (data) {
    //模拟添加
      const newChild = { title: 'ceshi1',
        key: 'ceshi1',
        scopedSlots: { title: 'custom' },
        children: [] }
      if (!data.children) {
        this.$set(data, 'children', [])
      }
      data.children.push(newChild)
    },
    remove (data) {
    	//先请求后端接口,删除成功后执行
      this.searchOption(data, this.treeData)
    },
    edit (data) {
    	//先请求后端接口,编辑成功后执行
      this.searchOption(data, this.treeData, 'edit')
    },
  }
}
</script>
<style lang="less" scoped>
.ant-tree-title {
  width: 100%;
}
.title {
  float: left;
}
.ant-card-body {
  :global {
    .ant-tree {
      line-height: 3;
      li {
        position: relative;
      }
    }
  }
}
.ant-card-body .but_type {
  float: right;
  position: absolute;
  right: 40px;
}
</style>

  • 10
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 28
    评论
Ant-Design-VueTree组件提供了add方法来新增节点和remove方法来删除节点。以下是示例代码: ```html <template> <a-tree :tree-data="treeData" :draggable="true" :block-node="true" :show-line="true" :default-expanded-keys="defaultExpandedKeys" @select="onSelect"> <template #title="{ key, title }"> <span> {{ title }} <a @click.stop="addNode(key)">Add</a> <a @click.stop="removeNode(key)">Delete</a> </span> </template> </a-tree> </template> <script> export default { data() { return { treeData: [ { title: 'Parent 1', key: '0-0', children: [ { title: 'Child 1', key: '0-0-0' }, { title: 'Child 2', key: '0-0-1' } ] } ], defaultExpandedKeys: ['0-0'] } }, methods: { addNode(parentKey) { const newNode = { title: 'New Node', key: `${parentKey}-${this.treeData.length}` } this.$set(this.treeData.find(node => node.key === parentKey), 'children', [...this.treeData.find(node => node.key === parentKey).children, newNode]) }, removeNode(key) { const parentKey = key.split('-').slice(0, -1).join('-') this.$set(this.treeData.find(node => node.key === parentKey), 'children', this.treeData.find(node => node.key === parentKey).children.filter(node => node.key !== key)) }, onSelect(selectedKeys) { console.log(selectedKeys) } } } </script> ``` 在这个示例中,我们使用了Tree组件自带的title slot来添加按钮。点击Add按钮会调用addNode方法,该方法会在选定节点添加一个新节点。点击Delete按钮会调用removeNode方法,该方法会删除选定节点。这两个方法都使用了Vue的$set方法来更新数据。同时,我们也监听了Tree组件的select事件来打印出所选中的节点

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值