下拉菜单(handleCommand($event, row.id))

<template>
  <div class="container">
    <div class="app-container">
      <MyTree :data="deptList">
        <template #right="{ row }">
          <el-col :span="4" class="right">
            <span class="managerName">
              {{ row.managerName }}
            </span>
            <el-dropdown @command="handleCommand($event, row.id)">
              <span class="el-dropdown-link">
                操作
                <i class="el-icon-arrow-down el-icon--right" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                <el-dropdown-item command="del">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </template>
      </MyTree>
      <MyDialog ref="dialogRef" :title="behavior.title" :on-close="onClose" :on-confirm="onConfirm">
        <template #body>
          <MyForm
            ref="deptformRef"
            :form-item="behavior.formItem"
            :form-data="behavior.formData"
            :rules="behavior.rules"
            label-width="150px"
          />
        </template>
      </MyDialog>
    </div>
  </div>
</template>

<script>
import MyTree from '@/components/CustomTree'
import MyDialog from '@/components/CustomDialog'
import MyForm from '@/components/CustomForm'
import { getFormItem } from '@/components/CustomForm/config/deptbehaviorConfig'
import { getDept, addDept } from '@/api/department'
import { transListToTree } from '@/utils/transListToTree'
export default {
  name: 'Department',
  components: {
    MyTree,
    MyDialog,
    MyForm
  },
  data() {
    return {
      deptList: [],
      behavior: {
        type: 'add',
        title: '新增子部门',
        formItem: [],
        formData: {},
        rules: {},
        currNodeId: null
      }
    }
  },
  async created() {
    this.getDept()
    this.behavior.formItem = await getFormItem()
    // 初始化表单数据和校验规则
    this.behavior.formItem.forEach((item) => {
      this.$set(this.behavior.formData, item.field, '')
      this.$set(this.behavior.rules, item.prop, item.rule)
    })
  },
  methods: {
    // 获取组织架构
    async getDept() {
      const res = await getDept()
      this.deptList = transListToTree(res, 0)
    },
    // 显示弹框
    handleCommand(type, id) {
      this.$refs.dialogRef.dialogVisible = true
      if (type === 'add') {
        this.behavior.title = '添加子部门'
        this.behavior.type === 'add'
        this.behavior.currNodeId = id
        console.log(id)
      } else if (type === 'edit') {
        this.behavior.title = '编辑部门'
        this.behavior.type === 'edit'
      } else {
        this.onClose()
        console.log('删除')
      }
    },
    // 关闭弹框
    onClose() {
      this.$refs.dialogRef.dialogVisible = false
      this.$refs.deptformRef.$refs.formRef.resetFields()
    },
    // 确认提交
    async onConfirm() {
      await this.$refs.deptformRef.$refs.formRef.validate()
      if (this.behavior.type === 'add') {
        await addDept({ ...this.behavior.formData, pid: this.behavior.currNodeId })
        this.$message.success('添加成功')
        this.getDept()
        this.onClose()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
  .app-container {
    height: 90vh;
    // overflow: hidden;
    overflow-y: scroll;
    .el-tree {
      margin: 80px 160px 0 160px;
      .right {
        font-size: 14px;
        display: flex;
        width: 220px;
        justify-content: space-between;
        align-items: center;
      }
    }
  }
}
</style>

封装的组件
CustomTree/index.vue

<template>
  <el-tree
    :data="data"
    :show-checkbox="showCheckBox"
    node-key="id"
    default-expand-all
    :expand-on-click-node="false"
    :current-node-key="currNodeKey"
    :highlight-current="ishighlightCurrent"
    :style="`width:${width}`"
  >
    <el-row slot-scope="{ data }" style="width: 100%; height: 40px" class="custom-tree-node">
      <el-col>{{ data.name }}</el-col>
      <slot name="right" :row="data" />
    </el-row>
  </el-tree>
</template>

<script>
export default {
  props: {
    width: {
      type: String,
      default: ''
    },
    showCheckBox: {
      type: Boolean,
      default: false
    },
    data: {
      type: Array,
      default: () => []
    },
    currNodeKey: {
      type: Number,
      default: null
    },
    ishighlightCurrent: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {}
  },
  methods: {}
}
</script>

<style lang="scss" scoped>
.custom-tree-node {
  // width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>

CustomDialog/index.vue

<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :width="width"
    :fullscreen="fullscreen"
    @close="onClose"
  >
    <slot name="body" />
    <span slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onConfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '50%'
    },
    fullscreen: {
      type: Boolean,
      default: false
    },
    onClose: {
      type: Function,
      default: () => {}
    },
    onConfirm: {
      type: Function,
      default: () => {}
    }
  },
  data() {
    return {
      dialogVisible: false
    }
  },
  methods: {
    issueEvent(value, mouseEvent) {
      if (mouseEvent) {
        return mouseEvent(value)
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
element-plus 中的 el-dropdown 实现多级下拉菜单的方法如下: 1. 首先需要嵌套多个 el-dropdown-menu 组件,每个 el-dropdown-menu 代表一个下拉菜单的级别,同时需要为每个 el-dropdown-menu 设置唯一的 ref 标识。 2. 接着需要在每个 el-dropdown-menu 中定义需要展示的菜单项,使用 el-dropdown-item 组件实现。 3. 在每个 el-dropdown-menu 上添加 @command 事件监听,当菜单项被点击时,触发该事件,并将所选中的菜单项作为参数传递给事件处理函数。 4. 在事件处理函数中,判断当前点击的菜单项是否有子菜单需要展示,如果有,则通过 ref 获取对应的子菜单 el-dropdown-menu 组件,并使用其 show 方法显示该子菜单。 以下是示例代码: ``` <template> <el-dropdown> <span class="el-dropdown-link">多级下拉菜单</span> <el-dropdown-menu ref="menu1"> <el-dropdown-item @command="handleCommand">选项1</el-dropdown-item> <el-dropdown-item @command="handleCommand">选项2</el-dropdown-item> <el-dropdown-item @command="handleCommand">选项3</el-dropdown-item> <el-dropdown-item @command="handleCommand">选项4</el-dropdown-item> <el-dropdown-item @command="handleCommand">选项5</el-dropdown-item> </el-dropdown-menu> </el-dropdown> </template> <script> export default { methods: { handleCommand(command) { if (command === '选项1') { const menu2 = this.$refs.menu2; menu2.show(); } else if (command === '选项2') { // do something } else if (command === '选项3') { // do something } else if (command === '选项4') { // do something } else if (command === '选项5') { // do something } } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值