ant tree 增刪改

效果

image.png
<template>
  <div class="knowledgeTree">
    <a-card>
      <a-tree @select="onSelect" :treeData="treeData" showLine>
        <template slot="custom" slot-scope="item">
          <div class="node">
            <span>{{ item.title }}</span>

            <span class="toolbar">
              <a-tooltip placement="top">
                <template slot="title">
                  <span>添加</span>
                </template>
                <a-icon @click="(event)=> onAdd(event, item)" type="plus-circle-o"></a-icon>
              </a-tooltip>
              <a-tooltip placement="top">
                <template slot="title">
                  <span>修改</span>
                </template>
                <a-icon @click="(event)=> onEdit(event, item)" type="edit"></a-icon>
              </a-tooltip>
              <a-tooltip placement="top">
                <template slot="title">
                  <span>刪除</span>
                </template>
                <a-icon @click="(event)=> onRemove(event, item)" type="minus-circle-o"></a-icon>
              </a-tooltip>
            </span>
          </div>
        </template>
      </a-tree>

      <a-modal
        title="新節(jié)點(diǎn)"
        :visible="visible"
        @ok="handleOk()"
        :confirmLoading="confirmLoading"
        @cancel="handleCancel"
      >
        <a-input placeholder="節(jié)點(diǎn)名稱" v-model="node" />
      </a-modal>
    </a-card>
  </div>
</template>
export default {
  data() {
    return {
      treeData: [],
      visible: false,
      confirmLoading: false,
      node: "",
      curNode: {}
    };
  },
  mounted() {
    this.treeData = [
      {
        key: 1,
        title: "節(jié)點(diǎn)1",
        scopedSlots: { title: "custom" },
        children: [
          { key: 11, title: "節(jié)點(diǎn)11", scopedSlots: { title: "custom" } },
          { key: 12, title: "節(jié)點(diǎn)12", scopedSlots: { title: "custom" } },
          { key: 13, title: "節(jié)點(diǎn)13", scopedSlots: { title: "custom" } }
        ]
      }
    ];
  },
  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 {
            //這是模擬數(shù)據(jù)編輯數(shù)據(jù)
            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;
        }
      }
    },
    onSelect: (selectKeys, info) => {
      console.log("selected", selectKeys);
    },
    onAdd(event, item) {
      event.stopPropagation();
      console.log("add", item);
      this.node = item.title;
      this.visible = true;
      this.curNode = item


      if (item.children) {
        item.children.push({
          key: Math.random(),
          title: Math.random(),
          scopedSlots: { title: "custom" }
        });
      } else {
        this.$set(item, "children", [
          {
            key: Math.random(),
            title: Math.random(),
            scopedSlots: { title: "custom" }
          }
        ]);
      }
    },
    onEdit(event, item) {
      event.stopPropagation();
      this.$set(item, "title", "123456");
    },
    onRemove(envent, item) {
      event.stopPropagation();
      console.log("remove", item);
      this.searchOption(item, this.treeData);
    },
    handleOk() {
      this.curNode.title = '123'
      this.confirmLoading = true;

      setTimeout(() => {
        this.visible = false;
        this.confirmLoading = false;
      }, 2000);
    },
    handleCancel() {
      this.visible = false;
    }
  }
};
.toolbar {
  display: none;
  margin-left: 200px;
}
.toolbar i {
  margin-left: 16px;
}
.node:hover .toolbar {
  display: inline;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容