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>

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 實戰 7:樹形控件——Tree 本小節基于 Vue.js 的遞歸組件知識,來開發一個常見的樹形控件—Tree。 T...
    中午吃啥_f330閱讀 1,210評論 0 1
  • 基于Vue的一些資料 內容 UI組件 開發框架 實用庫 服務端 輔助工具 應用實例 Demo示例 element★...
    嘗了又嘗閱讀 1,175評論 0 1
  • UI組件 element- 餓了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的組件庫 m...
    小姜先森o0O閱讀 9,612評論 0 72
  • UI組件 element- 餓了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的組件庫 m...
    流觴小菜鳥閱讀 1,826評論 2 8
  • 昨晚看了《羞羞的鐵拳》這部電影。拳擊手艾迪生和記者蘇小,被雷擊后互換了身體。 蘇小用艾迪生的身體,發現了以前自己被...
    風飄啊飄閱讀 794評論 0 3