当前位置:首页 > VUE

vue实现部门管理功能

2026-02-20 18:30:26VUE

实现部门管理功能的核心模块

基础数据结构设计 部门数据通常采用树形结构存储,建议使用以下格式:

{
  id: 1,
  name: '技术部',
  parentId: 0,
  children: [
    {
      id: 2,
      name: '前端组',
      parentId: 1
    }
  ]
}

组件结构规划

  • DeptTree.vue:树形展示组件
  • DeptForm.vue:表单编辑组件
  • DeptTable.vue:表格列表组件
  • index.vue:主入口文件整合各组件

树形组件实现方案

采用Element UI的el-tree组件构建部门树:

<template>
  <el-tree
    :data="deptData"
    :props="treeProps"
    node-key="id"
    default-expand-all
    @node-click="handleNodeClick">
  </el-tree>
</template>

<script>
export default {
  data() {
    return {
      treeProps: {
        label: 'name',
        children: 'children'
      },
      deptData: []
    }
  },
  methods: {
    async fetchDeptTree() {
      const res = await api.getDeptTree();
      this.deptData = res.data;
    },
    handleNodeClick(data) {
      this.$emit('node-selected', data);
    }
  }
}
</script>

表单交互逻辑

部门表单应包含基础校验规则:

formRules: {
  name: [
    { required: true, message: '请输入部门名称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在2到20个字符', trigger: 'blur' }
  ],
  parentId: [
    { required: true, message: '请选择上级部门', trigger: 'change' }
  ]
}

提交逻辑需处理父子关系:

async submitForm() {
  try {
    const valid = await this.$refs.form.validate();
    if (valid) {
      const method = this.form.id ? 'updateDept' : 'addDept';
      await api[method](this.form);
      this.$emit('success');
    }
  } catch (error) {
    console.error(error);
  }
}

表格与树形联动

实现表格数据与树节点选择联动:

watch: {
  selectedNode(val) {
    if (val) {
      this.queryParams.parentId = val.id;
      this.loadTableData();
    }
  }
},
methods: {
  loadTableData() {
    api.getDeptList(this.queryParams).then(res => {
      this.tableData = res.data;
    });
  }
}

完整功能整合

主入口文件示例:

<template>
  <div class="dept-container">
    <el-row :gutter="20">
      <el-col :span="6">
        <dept-tree @node-selected="handleNodeSelect"/>
      </el-col>
      <el-col :span="18">
        <dept-table :parent-id="currentNodeId" @edit="handleEdit"/>
      </el-col>
    </el-row>

    <dept-form 
      :visible="formVisible"
      :data="formData"
      @close="formVisible = false"
      @success="handleFormSuccess"/>
  </div>
</template>

关键API接口示例

后端接口建议包含:

// 获取部门树
GET /api/dept/tree

// 获取子部门列表
GET /api/dept/list?parentId=xxx

// 创建部门
POST /api/dept

// 更新部门
PUT /api/dept/:id

// 删除部门
DELETE /api/dept/:id

数据加载优化策略

采用懒加载提升大型部门树性能:

async loadNode(node, resolve) {
  if (node.level === 0) {
    const res = await api.getRootDepts();
    resolve(res.data);
  } else {
    const res = await api.getChildDepts(node.data.id);
    resolve(res.data || []);
  }
}

移动端适配方案

针对移动设备调整布局:

@media screen and (max-width: 768px) {
  .dept-container {
    flex-direction: column;
  }
  .tree-container {
    width: 100%;
    margin-bottom: 20px;
  }
}

vue实现部门管理功能

标签: 部门功能
分享给朋友:

相关文章

vue实现审核功能

vue实现审核功能

实现审核功能的基本思路 审核功能通常涉及状态管理、权限控制和操作记录。Vue中可以通过组件化设计、状态管理库(如Vuex或Pinia)和后端API配合实现。 审核状态管理 使用Vuex或Pinia存…

vue的艾特功能实现

vue的艾特功能实现

Vue 中的 @ 功能实现 Vue 中的 @ 符号是 v-on 指令的简写,用于绑定事件监听器。以下是具体实现方式: 基本语法 <button @click="handleClick"&…

vue实现产品使用功能

vue实现产品使用功能

Vue 实现产品功能的关键方法 组件化开发 使用Vue的单文件组件(.vue)将产品功能模块化,每个功能拆分为独立组件。例如产品列表、详情页、购物车等可分别封装为ProductList.vue、Pro…

js实现复制功能

js实现复制功能

使用 document.execCommand 方法 这种方法适用于较旧的浏览器,但在现代浏览器中可能被逐步淘汰。通过创建一个临时的 textarea 元素,将文本内容放入其中,然后执行复制命令。…

vue 实现筛选功能

vue 实现筛选功能

实现筛选功能的基本思路 在Vue中实现筛选功能通常涉及以下几个核心步骤:数据绑定、筛选逻辑处理、结果展示。以下是具体实现方法。 数据准备与绑定 准备一个包含所有数据的数组,并使用v-model或v…

vue实现粘贴功能

vue实现粘贴功能

实现粘贴功能的基本方法 在Vue中实现粘贴功能通常涉及监听paste事件,并处理剪贴板数据。以下是一个基础实现示例: <template> <div @paste="handl…