<el-tree>标签使用

简介

el-tree 标签是 Element UI 提供的树形控件组件,可以用于一些树形结构数据的展示。

本文介绍如何结合后端接口,使用该空间完成一些展示/选中逻辑。

查询数据

如下,后端接口返回了下面这样结构的数据,是一个 部门-用户列表 的结构。

如下,将接口返回数据与组件展示的数据进行绑定,在 props 中对数据结构进行解析。

html 复制代码
<template>
  <div>
    <el-tree
        :data="treeData"
        :props="props"
        show-checkbox>
    </el-tree>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      props: {
        label: function (node) {
          // 如果有 userVOList 属性,说明是部门节点,使用 departmentName
          // 如果没有 userVOList 属性,说明是用户节点,使用 userName
          return node.userVOList ? node.departmentName : node.userName;
        },
        children: 'userVOList'
      },
      treeData: []
    };
  },

  mounted() {
    this.loadTreeData();
  },
  /**
   * 调用后端接口
   */
  methods: {
    loadTreeData() {
      axios.post('http://localhost:8080/departments')
          .then(response => {
            if (response.data.status === 'success') {
              this.treeData = response.data.data;
            }
          })
          .catch(error => {
            console.error('加载数据失败:', error);
          });
    }
  }
};
</script>

启动项目,此时前端展示的数据就是后端返回的。

选中提交

要求选中某些用户,点击确定时,将选中的用户 userId 集合传递给后端,做某些操作,这是一个很常见的场景。

前端代码修改成如下。

html 复制代码
<template>
  <div>
    <!-- 确认 -->
    <el-button type="success" @click="openDialog" style="margin-bottom: 10px;">
      确认选择
    </el-button>

    <!-- 数据 -->
    <el-tree
        ref="tree"
        :props="props"
        :data="treeData"
        show-checkbox
        style="width: 300px;">
    </el-tree>

    <!-- 确认对话框 -->
    <el-dialog
        title="确认选择"
        v-model="dialogVisible"
        width="500px">
      <span>确定要提交选中的用户吗?</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleCancel">取消</el-button>
          <el-button type="primary" @click="handleConfirm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      props: {
        label: function (node) {
          // 如果有 userVOList 属性,说明是部门节点,使用 departmentName
          // 如果没有 userVOList 属性,说明是用户节点,使用 userName
          return node.userVOList ? node.departmentName : node.userName;
        },
        children: 'userVOList'
      },
      treeData: [],
      dialogVisible: false,
      selectedUserIds: []
    };
  },
  mounted() {
    this.loadTreeData();
  },
  methods: {
    /**
     * 加载部门和用户数据到树结构
     */
    loadTreeData() {
      axios.post('http://localhost:8080/departments')
          .then(response => {
            if (response.data.status === 'success') {
              this.treeData = response.data.data;
            }
          })
          .catch(error => {
            console.error('加载数据失败:', error);
          });
    },

    /**
     * 打开确认对话框
     */
    openDialog() {
      this.dialogVisible = true;
    },

    /**
     * 处理确认提交选中的用户ID
     */
    handleConfirm() {
      // 获取选中的节点
      const checkedNodes = this.$refs.tree.getCheckedNodes(true);

      // 过滤出用户节点(没有userVOList属性的节点)
      this.selectedUserIds = checkedNodes
          .filter(node => !node.userVOList)
          .map(node => node.userId);

      // 调用后端接口
      this.uploadUsers();

      // 关闭对话框
      this.dialogVisible = false;
    },

    /**
     * 处理取消提交选中的用户ID
     */
    handleCancel() {
      this.dialogVisible = false;
    },

    /**
     * 提交选中的用户ID到后端
     */
    uploadUsers() {
      // 调用后端接口
      axios.post('http://localhost:8080/uploadUsers', {
        userIds: this.selectedUserIds
      })
          .then(response => {
            console.log('接口调用成功:', response.data);
            this.$message.success('提交成功');
          })
          .catch(error => {
            console.error('接口调用失败:', error);
            this.$message.error('提交失败');
          });
    }
  }
};
</script>

启动项目,选中某些用户,点击 确认选择,可见调用接口时,将选中的用户 ID 放到集合中作为参数传递了。

相关推荐
高级程序源8 分钟前
django校企合作实习基地管理系统82506-计算机课程设计、毕业设计
vue.js·后端·python·mysql·django·课程设计·pygame
恋猫de小郭18 分钟前
Flutter GSoC 2026 提案进度解读,补上 DevTools、FFI 和原生平台的关键缺口
android·前端·flutter
尘世中一位迷途小书童26 分钟前
我为什么在 Windows 上做了一个翻译软件:SnapLingo
前端·人工智能·机器学习
志尊宝31 分钟前
Vue3 零基础每日笔记(029):插槽 slot 三连——默认、具名、作用域一次讲透
前端·javascript·vue.js·vue·前端开发
aixingpan33 分钟前
aixingpan.cn API开发文档:api_docs_bichart_natalvstertiary2接口指南
前端·php
铁皮饭盒43 分钟前
网页端, 40mb离线模型, 自动抠图, 不用 Python,不用服务器,不用 API Key, 不用显卡
前端·javascript·后端
梦曦i44 分钟前
修复 .uvue 页面路径残留问题
前端·vite
nicole bai1 小时前
vue前端全局图标闪一下的问题
前端·vue.js
ly76891 小时前
磁盘 I/O 延迟突增:用 iostat、blktrace 与火焰图定位到具体调用栈
java·linux·前端·数据库·iostat·磁盘 i/o·blktrace
IT_陈寒1 小时前
Redis并发写入踩坑记录:别让超时设置坑了你
前端·人工智能·后端