使用el-tree自定义节点内容,更改节点内容时候只更改了最后添加的节点内容

15 篇文章 0 订阅
2 篇文章 0 订阅

背景

使用户自定义节点内容 :element中的el-tree中的自定义节点内容 的例子 只是列出了添加节点内容和删除节点 ,并没有 修改节点内容的例子说明,我是以点击修改 弹窗的形式进行修改节点内容 。

问题

当一次性 新增数个节点时候(无论父子节点)
当点击编辑 弹窗 修改节点内容时候 ,无论点击的是那个节点 弹框弹的都是最后那个添加的节点的弹框,导致修改的时候,都是修改最后添加的那个节点的内容;以下请添加图片描述
是问题代码。

<template>
  <div class="custom-tree-container">
    <div class="block" style="width:50%">
      <p>使用 scoped slot</p>
      <el-tree
        :data="data"
        show-checkbox
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span>
            <el-button
              type="text"
              size="mini"
              @click="() => append(node, data)"
            >
              <i class="el-icon-plus"></i>
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="() => remove(node, data)"
            >
              <i class="el-icon-delete"></i>
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="() => editor(node, data)"
            >
              <i class="el-icon-edit"></i>
            </el-button>
            <div>
              <el-dialog
                title="名称"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose"
              >
                <el-input type="text" v-model="value"></el-input>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible = false">取 消</el-button>
                  <el-button type="primary" @click="submit(node, data)"
                    >确 定</el-button
                  >
                </span>
              </el-dialog>
            </div>
          </span>
        </span>
      </el-tree>
    </div>
  </div>
</template>
<script>
let id = 1000;
export default {
  data() {
    const data = [
      {
        id: 1,
        label: "数据源",
        parentId: null,
      },
    ];

    return {
      value: "",
      newArr: [],
      abcd: [],
      dialogVisible: false,
      data: JSON.parse(JSON.stringify(data)),
      data: JSON.parse(JSON.stringify(data)),
    };
  },

  methods: {
    submit(node, data) {
      data.label = this.value;
      this.dialogVisible = false;
    },
    // 弹窗关闭
    handleClose(done) {
      this.$confirm("确认关闭?")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 添加
    append(node, data) {
      const newChild = {
        parentId: node.data.id,
        id: id++,
        label: "testtest",
        children: [],
      };
      if (!data.children) {
        this.$set(data, "children", []);
      }
      data.children.push(newChild);
    },
    // 移除
    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);
    },
    editor(node, data) {
      this.value = "";
      this.dialogVisible = true;
    },
  },
};
</script>

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

上面的是将弹窗放在了el-tree中的 slot-scope下 当点击编辑的时,拿到的是对应节点的数据,但当点击弹框确定的时候 拿到的是最后添加节点的数据。
将 弹框从 el-tree中独立出来,点击编辑后取到对应节点数据,保存到本地data里面 ,点击确定的时候在赋值;以下是更改后的代码

<template>
  <div class="custom-tree-container">
    <div class="block" style="width:50%">
      <p>使用 scoped slot</p>
      <el-tree
        :data="data"
        show-checkbox
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span>
            <el-button
              type="text"
              size="mini"
              @click="() => append(node, data)"
            >
              <i class="el-icon-plus"></i>
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="() => remove(node, data)"
            >
              <i class="el-icon-delete"></i>
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="() => editor(node, data)"
            >
              <i class="el-icon-edit"></i>
            </el-button>

          </span>
        </span>
      </el-tree>
      <div>
              <el-dialog
                title="名称"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose"
              >
                <el-input type="text" v-model="value"></el-input>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible = false">取 消</el-button>
                  <el-button type="primary" @click="submit"
                    >确 定</el-button
                  >
                </span>
              </el-dialog>
            </div>
    </div>
  </div>
</template>
<script>


let id = 1000;
export default {
  data() {
    const data = [
      {
        id: 1,
        label: "数据源",
        parentId: null,
      },
    ];

    return {
      dataTree:'', //编辑的节点数据
      value: "",
      newArr: [],
      abcd: [],
      dialogVisible: false,
      data: JSON.parse(JSON.stringify(data)),
      data: JSON.parse(JSON.stringify(data)),
    };
  },

  methods: {
    submit() {
      this.dataTree.label = this.value;
      this.dialogVisible = false;
    },
    // 弹窗关闭
    handleClose(done) {
      this.$confirm("确认关闭?")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 添加
    append(node, data) {
      const newChild = {
        parentId: node.data.id,
        id: id++,
        label: "testtest",
        children: [],
      };
      if (!data.children) {
        this.$set(data, "children", []);
      }
      data.children.push(newChild);
    },
    // 移除
    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);
    },
    editor(node, data) {
      this.dataTree = data
      this.value = "";
      this.dialogVisible = true;
    },
  },
};
</script>

<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值