Elementui Tree 树形控件删除功能

简介: Elementui Tree 树形控件删除功能

vue-element-admin一个基于 vue2.0 和 Eelement 的控制面板 UI 框架,这是使用vue技术栈开发的前端程序员的首选管理系统模板,模板以及非常的成熟了,并且有相关的社区和维护人员,开发时候遇到问题也不要慌。

推荐指数:

今天就来说一下,怎么使用Tree Table 树形表格。废话不多说,首先看一下官方文档吧,看看官方文档是怎么给到的例子吧:

组件:https://element.eleme.cn/#/zh-CN/component/tree

功能:当鼠标划过Tree 树形控件的节点的时候,会出现一个删除的按钮

点击删除按钮,会出现弹框询问是否删除

选中删除,则删除树节点(最上层的父节点不可删除)

1:在views底下新建一个test文件夹

里面新建一个vue文件和一个json文件

2:使用

mock.json

{
    "msg": "success",
    "code": 1,
    "data": [
        {
            "id": 1,
            "organName": "yy有限公司",
            "parentId": 0,
            "manager": "zs",
            "phone": "zs",
            "companyId": 1,
            "address": null
        },
        {
            "id": 2,
            "organName": "test311",
            "parentId": 1,
            "manager": "zs",
            "phone": "21",
            "companyId": 1,
            "address": null
        },
        {
            "id": 10,
            "organName": "test4",
            "parentId": 1,
            "manager": "zs",
            "phone": null,
            "companyId": 1,
            "address": null
        },
        {
            "id": 11,
            "organName": "2121",
            "parentId": 1,
            "manager": "212",
            "phone": "13661725475",
            "companyId": 1,
            "address": null
        },
        {
            "id": 12,
            "organName": "212121212",
            "parentId": 2,
            "manager": "212",
            "phone": "13661725475",
            "companyId": 1,
            "address": null
        },
        {
            "id": 13,
            "organName": "www群46",
            "parentId": 11,
            "manager": "ww",
            "phone": "123",
            "companyId": 1,
            "address": null
        },
        {
            "id": 14,
            "organName": "www",
            "parentId": 11,
            "manager": "ww",
            "phone": "123",
            "companyId": 1,
            "address": null
        },
        {
            "id": 15,
            "organName": "",
            "parentId": null,
            "manager": "",
            "phone": "",
            "companyId": 1,
            "address": null
        },
        {
            "id": 16,
            "organName": "21212",
            "parentId": null,
            "manager": "",
            "phone": "",
            "companyId": 1,
            "address": null
        },
        {
            "id": 17,
            "organName": "2131314",
            "parentId": null,
            "manager": "",
            "phone": "",
            "companyId": 1,
            "address": null
        },
        {
            "id": 18,
            "organName": "q313",
            "parentId": 2,
            "manager": "",
            "phone": "",
            "companyId": 1,
            "address": null
        },
        {
            "id": 19,
            "organName": "8888",
            "parentId": 1,
            "manager": "11",
            "phone": "",
            "companyId": 1,
            "address": null
        },
        {
            "id": 20,
            "organName": "21",
            "parentId": 1,
            "manager": "12",
            "phone": "21",
            "companyId": 1,
            "address": null
        },
        {
            "id": 21,
            "organName": "wwww",
            "parentId": 1,
            "manager": "www",
            "phone": "13661725475",
            "companyId": 1,
            "address": null
        },
        {
            "id": 22,
            "organName": "1313",
            "parentId": 1,
            "manager": "313",
            "phone": "31",
            "companyId": 1,
            "address": null
        },
        {
            "id": 23,
            "organName": "test",
            "parentId": 1,
            "manager": "zs",
            "phone": "",
            "companyId": 1,
            "address": null
        },
        {
            "id": 24,
            "organName": "test_01",
            "parentId": 23,
            "manager": "zs",
            "phone": "",
            "companyId": 1,
            "address": null
        },
        {
            "id": 25,
            "organName": "w121",
            "parentId": 1,
            "manager": "212",
            "phone": "212",
            "companyId": 1,
            "address": null
        }
    ]
}

3:

test.vue

<template>
  <div>
    <el-col :span="6">
      <div class="ztree">
        <el-tree
          :data="treeData"
          show-checkbox
          default-expand-all
          node-key="id"
          ref="tree"
          highlight-current
          :props="defaultProps"
          @node-click="nodeClickHandler"
          @check="checkHandler"
          :render-content="renderContent"
        >
        </el-tree>
      </div>
    </el-col>
  </div>
</template>
<script>
//调用接口
// import { deleteSubject } from "@/api/data/organ";
export default {
  data() {
    return {
      setTree: [],
      defaultProps: {
        children: "children",
        label: "organName",
      },
      treeData: [],
      organList: [],
    };
  },
  created() {
    //加载部门管理节点接口定义
    this.getQuerycheckList();
  },
  methods: {
    renderContent(h, { node, data, store }) {
      console.log(data);
      return (
        <span
          class="custom-tree-node"
          on-mouseenter={() => (data.isHover = true)}
          on-mouseleave={() => (data.isHover = false)}
        >
          <span>{data.organName}</span>
          {data.parentId !== 0 && data.isHover && (
            <i
              class="el-icon-error danger"
              on-click={(e) => {
                e.stopPropagation();
                this.remove(node, data);
              }}
            ></i>
          )}
        </span>
      );
    },
    remove(node, data) {
      this.$confirm("此操作将永久删除该条目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.handleDelete(null, data);
          if (data.parentId === 0) {
            return;
          }
          const parent = node.parent;
          const children = parent.data.children || parent.data;
          const index = children.findIndex((d) => d.id === data.id);
          children.splice(index, 1);
          // 发请求删除
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //勾选
    checkHandler(...value) {
      console.log(value[1]);
      console.log(value[1].checkedNodes.map((a) => a.organName));
    },
    //部门管理节点接口定义
    getQuerycheckList() {
      const params = {};
      this.dataLoading = true;
      import("./mock").then((res) => {
        this.setTree = res.data;
        this.organList = res.data.map((a) => ({
          label: a.organName,
          value: a.id,
        }));
        this.getListData();
        this.dataLoading = false;
      });
    },
    //对json的格式的转化
    getListData() {
      let dataArray = [];
      this.setTree.forEach(function (data) {
        let parentId = data.parentId;
        if (parentId === 0) {
          let objTemp = {
            id: data.id,
            organName: data.organName,
            manager: data.manager,
            phone: data.manager,
            parentId: parentId,
          };
          dataArray.push(objTemp);
        }
      });
      this.treeData = this.data2treeDG(this.setTree, dataArray);
    },
    data2treeDG(datas, dataArray) {
      for (let j = 0; j < dataArray.length; j++) {
        let dataArrayIndex = dataArray[j];
        let childrenArray = [];
        let Id = dataArrayIndex.id;
        for (let i = 0; i < datas.length; i++) {
          let data = datas[i];
          let parentId = data.parentId;
          if (parentId == Id) {
            //判断是否为儿子节点
            let objTemp = {
              id: data.id,
              organName: data.organName,
              manager: data.manager,
              phone: data.phone,
              parentId: parentId,
              isHover: false,
            };
            childrenArray.push(objTemp);
          }
        }
        dataArrayIndex.children = childrenArray;
        if (childrenArray.length > 0) {
          //有儿子节点则递归
          this.data2treeDG(datas, childrenArray);
        }
      }
      return dataArray;
    },
    //调用删除接口
    handleDelete(index, row) {
      const params = {
        id: row.id,
      };
      this.dataLoading = true;
      deleteSubject(params).then((res) => {
        this.$notify({
          message: "删除成功",
          type: "success",
          duration: 2000,
        });
        this.getQuerycheckList();
        console.log(this.pvData);
        this.dataLoading = false;
      });
    },
    nodeClickHandler(...rest) {
      console.log(rest);
      this.form = rest[0];
    },
  },
};
</script>
<style lang="scss">
.danger {
  color: red;
}
</style>

渲染:

相关文章
easyui的tree节点的获取和选中
easyui的tree节点的获取和选中
236 0
|
JSON JavaScript 数据格式
Elementui Tree 树形控件删除子节点
Elementui Tree 树形控件删除子节点
108 1
|
JSON JavaScript 前端开发
vue如何获取Elementui Tree 树形控件当前选中的节点
vue如何获取Elementui Tree 树形控件当前选中的节点
152 0
|
3月前
element中tree组件的选中获取和返显
本文介绍了如何在Element UI的tree组件中获取选中的节点值以及如何进行节点的默认选中(返显)。主要通过使用`getCheckedKeys()`和`getHalfCheckedKeys()`方法来获取完全和半选中的节点,然后使用`setCheckedKeys()`方法来设置默认选中的节点。
273 2
element中tree组件的选中获取和返显
|
4月前
|
前端开发 虚拟化
简单记录使用 ElementPlus 的虚拟化树形控件(el-tree-v2)心得
这篇文章分享了作者使用ElementPlus的虚拟化树形控件`el-tree-v2`的心得,展示了其基本用法和如何通过自定义模板来增强树节点的交互性。
1675 1
简单记录使用 ElementPlus 的虚拟化树形控件(el-tree-v2)心得
|
4月前
|
JavaScript API
Antd——如何给树形控件增加编辑删除
Antd——如何给树形控件增加编辑删除
236 0
|
5月前
Element UI 树形控件Tree 【详解】el-tree 展开指定节点,判断是否存在指定节点
Element UI 树形控件Tree 【详解】el-tree 展开指定节点,判断是否存在指定节点
946 0
|
6月前
|
存储 前端开发 JavaScript
elementUI Tree 树形控件单选实现
【6月更文挑战第3天】Element UI 的树形控件(el-tree)默认不支持单选,但可以通过监听节点点击事件并手动更新选中状态实现。以下是一个简单的示例,展示如何创建单选树: ```html &
289 0
element组件里的tree树形控件的使用?
element组件里的tree树形控件的使用?
|
7月前
element plus 可选择树形组件(el-tree) 怎样一键展开/收起?实现方法详解
element plus 可选择树形组件(el-tree) 怎样一键展开/收起?实现方法详解
905 2

热门文章

最新文章