Element UI form表单及select下拉框实现动态添加和删除

这篇具有很好参考价值的文章主要介绍了Element UI form表单及select下拉框实现动态添加和删除。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

需求为可动态选择用户及部门,并具备回显功能

结合antv x6 流程图,实现需求,每个流程图节点均可设置当前节点对应审批部门和用户,部门会签节点可设置多部门多用户,动态添加部门及用户。

当前节点已配置人员部门可回显,当前节点已配置人员删除,回显为空。文章来源地址https://www.toymoban.com/news/detail-506105.html

<template>
  <div>
    <el-dialog
      title="设置"
      :visible.sync="dialogFormVisible"
      width="52%"
      :before-close="handleClose"
      :close-on-click-modal="false"
    >
      <el-form ref="dataForm" :model="dataForm">
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="当前节点:" label-width="120px">
              <div style="font-size: 16px; color: #999; font-weight: 600">
                {{ title }}
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              v-if="[2, 3].includes(type)"
              label="所属公司:"
              label-width="120px"
            >
              <div style="font-size: 16px; color: #999; font-weight: 600">
                {{ companyName }}
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              v-if="type === 3"
              label="项目名称:"
              label-width="120px"
            >
              <div style="font-size: 16px; color: #999; font-weight: 600">
                {{ projectCompanyName }}
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <div v-for="(item, index) in dataForm.selectForm">
          <el-form-item
            :key="index + '.region'"
            label="选择部门:"
            :prop="'selectForm.' + index + '.region'"
            :rules="{
              required: true,
              message: '请选择部门',
              trigger: 'change'
            }"
          >
            <el-select
              v-model="item.region"
              placeholder="请选择部门"
              clearable
              @change="
                (val) => {
                  changeDeptId(val, index, item)
                }
              "
            >
              <el-option
                v-for="(item, index) in departmentList"
                :key="item.value + index"
                :label="item.label"
                :value="item.value"
                :disabled="
                  title === '部门会签'
                    ? selectedDeptId.includes(item.value)
                    : false
                "
              />
            </el-select>
            <i
              v-if="title === '部门会签'"
              :class="
                index === 0
                  ? 'el-icon-user-solid solid-btn'
                  : 'el-icon-delete-solid solid-btn'
              "
              @click="depIdListChange(index)"
            ></i>
          </el-form-item>
          <el-form-item
            :key="index + '.name'"
            label="选择用户:"
            :prop="'selectForm.' + index + '.name'"
            :rules="{
              required: true,
              message: '请选择用户',
              trigger: 'change'
            }"
          >
            <el-select v-model="item.name" placeholder="请选择用户" clearable>
              <el-option
                v-for="(item, index) in userList[index]"
                :key="item.value + index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleCheck">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ViewDeails',
  components: {},
  props: {},
  data() {
    return {
      dialogFormVisible: false, // 弹框开关
      dataForm: {
        selectForm: []
      },
      departmentList: [], // 选择部门
      userList: {
        0: []
      }, // 选择用户
      x: null,
      y: null,
      cell: null,
      e: null,
      view: null,
      title: null,
      id: '',
      changeId: null, // 修改配置参数
      deptid: [], // 部门列表
      userid: [], //用户列表
      selectList: [], // 部门会签列表
      assigneeName: null, // 节点常量
      companyName: null, // 所属公司
      projectCompanyName: null, // 所属companyName部门名称
      companyCode: null, // 部门code
      type: null, // 1 - 集团 2- 工程 3- 项目工区 4- 指挥部
      selectedDeptId: [], // 选中的部门id数组
      userMessage: null // 选择用户是否存在
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    // this.constantNode()
  },
  methods: {
    show(
      e,
      x,
      y,
      cell,
      name,
      code,
      projectName,
      projectCode,
      id,
      changeId,
      deptid,
      userid
    ) {
      this.x = x
      this.y = y
      this.cell = cell
      this.id = id
      this.changeId = changeId
      this.companyName = name
      this.companyCode = code
      this.projectCompanyCode = projectCode
      this.projectCompanyName = projectName
      this.title = cell.store.data.attrs.text.text
        ? cell.store.data.attrs.text.text
        : e.target.innerHTML
      this.type = this.$route.query.companyType
      this.dialogFormVisible = true
      this.dataForm.selectForm = []
      this.queryCompanyList()
      // 判断部门及用户数据是否已配置
      if (deptid || userid) {
        this.depEcho(deptid, userid)
      } else {
        this.dataForm.selectForm.push({
          region: '',
          name: ''
        })
      }
    },

    // 部门回显  - 函数提取出来,减少代码多次调用,提高性能
    // 当前回显数据和数据库当前部门下用户匹配则正确回显,若当前人员已在数据库删除,则清空回显数据
    depEcho(deptid, userid) {
      this.deptid = deptid.split(',')
      this.userid = userid.split(',')
      for (var i = 0; i < this.deptid.length; i++) {
        const item = this.deptid[i]
        this.dataForm.selectForm.push({
          region: item,
          name: ''
        })
        this.getUserList(item, i, true)
      }
    },

    //  部门改变事件
    changeDeptId(deptId, index, val) {
      this.getSelectedIdList()
      this.getUserList(deptId, index)
      val.name = ''
    },
    // 更新选中部门的id数组
    getSelectedIdList() {
      this.selectedDeptId = this.dataForm.selectForm.map((item) => {
        return item.region
      })
    },
    // 获取用户列表
    async getUserList(deptId, index, isEdit) {
      const res = await this.$api2.processConfiguration.getUserList({
        deptId
      })
      this.$set(this.userList, index, res)
      if (!isEdit) return
      this.userEcho(res,index)
    },
    // 用户回显
    userEcho(userList,index) {
      for (let i = 0; i < userList.length; i++) {
        const value = userList[i].value
        if (this.userid.includes(value)) {
          this.dataForm.selectForm[index].name = value
          break
        } else {
          this.dataForm.selectForm[index].name = ''
        }
      }
    },
    // 部门、人员 添加/删除事件
    depIdListChange(index) {
      if (index === 0) {
        this.dataForm.selectForm.push({ region: '', name: '' })
      } else {
        this.dataForm.selectForm.splice(index, 1)
        this.getSelectedIdList()
      }
      this.userList[index] = []
    },
    //  获取部门列表
    async queryCompanyList() {
      // type: null // 1 - 集团 2- 工程 3- 项目工区 4- 指挥部
      if (this.type === 1 || this.type === 2) {
        this.departmentList = await this.$api2.processConfiguration.getDeptList(
          {
            companyCode: this.companyCode
          }
        )
      } else if (this.type === 3 || this.type === 4) {
        this.departmentList = await this.$api2.processConfiguration.getDeptList(
          {
            companyCode: this.projectCompanyCode
          }
        )
      }
    },

    // 提交保存节点配置
    handleCheck() {
      this.constantNode()
      this.$refs.dataForm.validate((valid) => {
        let regionList = [] // 部门会签节点 -部门数据
        let userList = [] // 部门会签节点 -用户数据
        this.dataForm.selectForm.forEach((item) => {
          regionList.push(item.region)
          userList.push(item.name)
        })
        if (valid) {
          this.$api2.processConfiguration
            .saveFlowConfigDetail({
              assigneeName: this.assigneeName,
              deptIds: regionList.toString(),
              flowConfigId: this.id,
              id: this.changeId,
              userIds: userList.toString()
            })
            .then(() => {
              this.dialogFormVisible = false
              this.$message.success('配置成功')
              this.$emit('change', this.x, this.y, this.cell)
            })
            .catch(() => {})
        } else {
          return false
        }
      })
    },
    initData() {
     //  造成多次点击确定然后打开新增弹框触发校验的原因,就是新增了region 和 name 为空,触发表单校验,关闭弹框应将全部数据置空,而不是新增属性触发校验
     // this.dataForm = { selectForm: [{ region: '', name: '' }] }
      this.dataForm = {
        selectForm: []
      }
      this.userList[0] = []
      this.departmentList = []
      this.selectedDeptId = []
      this.$refs.dataForm.clearValidate()
    },
    // 取消按钮
    handleClose() {
      this.initData()
      this.dialogFormVisible = false
      this.$refs.dataForm.resetFields()
    },
    // 常量节点 - 对应后台常量解析前台对应node节点
    // type: 1 - 集团 2- 工程 3- 项目工区 4- 指挥部
    // this.$route.query.flowType: 1 - 标准流程配置 2-变更注销流程
    /* 指挥部变更标准流程和工程公司变更标准流程相同*/
    constantNode() {
      const nodeMapping = {
        1: {
          1: {
            node1: 'groupCompanyAdministratorFormalExaminationAssignee',
            node2: 'groupCompanyExpertCrewList',
            node3: 'groupCompanyDesigningInstituteAdministratorAssignee',
            node4: 'groupCompanyDesigningInstituteAuditorAssignee',
            node5: 'groupCompanyExpertLeaderAssignee',
            node6: 'groupCompanyDesigningInstituteAdministratorAssignee',
            node7: 'groupCompanyDesigningInstituteDeanAssignee',
            node8: 'groupCompanyAdministratorExamineAssignee',
            node9: 'groupCompanyTechnologyMinisterAssignee',
            node10: 'groupCompanyChiefEngineerAssignee',
            node11: 'groupCompanyAdministratorReturnAssignee'
          },
          2: {
            node1: 'changeGroupCompanyAdministratorAssignee',
            node2: 'changeGroupCompanyTechnologyMinisterAssignee',
            node3: 'changeGroupCompanyChiefEngineerAssignee',
            node5: 'changeGroupCompanyAdministratorReturnAssignee'
          }
        },
        2: {
          1: {
            node1: 'engineeringCompanyAdministratorAssignee',
            node3: 'engineeringCompanyDeptCounterSignList',
            node5: 'engineeringCompanyExpertList',
            node6: 'engineeringCompanyAdministratorReturnAssignee',
            node7: 'engineeringCompanySafetyDirectorAssignee',
            node8: 'engineeringCompanyChiefEngineerAssignee'
          },
          2: {
            node1: 'changeEngineeringCompanyAdministratorAssignee',
            node2: 'changeEngineeringCompanyChiefEngineerAssignee',
            node3: 'changeEngineeringCompanyAdministratorReturnAssignee'
          }
        },
        3: {
          1: {
            node1: 'projectInitiatorAssignee',
            node2: 'projectDeptCounterSignList',
            node3: 'projectSafetyDirectorAssignee',
            node4: 'projectChiefEngineerAssignee',
            node5: 'projectManagerAssignee',
            node6: 'projectInformantArchiveAssignee'
          },
          2: {
            node1: 'changeProjectChiefEngineerInitiatorAssignee',
            node2: 'changeProjectManagerAssignee'
          }
        },
        4: {
          1: {
            node1: 'headquartersDeptCounterSignList',
            node2: 'headquartersSafetyDirectorAssignee',
            node3: 'headquartersChiefEngineerAssignee',
            node4: 'headquartersCommanderAssignee',
            node5: 'headquartersAdministratorReturnAssignee'
          },
          2: {
            node1: 'changeEngineeringCompanyAdministratorAssignee',
            node2: 'changeEngineeringCompanyChiefEngineerAssignee',
            node3: 'changeEngineeringCompanyAdministratorReturnAssignee'
          }
        }
      }
      const type = this.type
      const queryFlowType = this.$route.query.flowType
      const node = this.cell.id
      if (
        !nodeMapping[type] ||
        !nodeMapping[type][queryFlowType] ||
        !nodeMapping[type][queryFlowType][node]
      ) {
        return
      }
      this.assigneeName = nodeMapping[type][queryFlowType][node]
    }
  }
}
</script>

<style scoped>
::v-deep .el-form-item__content {
  display: flex;
}
.solid-btn {
  margin-left: 10px;
  cursor: pointer;
  font-size: 16px;
}
</style>

到了这里,关于Element UI form表单及select下拉框实现动态添加和删除的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • element-ui form表单的动态rules校验

    在vue 项目中,有时候可能会用到element-ui form表单的动态rules校验,比如说选择了哪个选项,然后动态显示或者禁用等等。 我们可以巧妙的运用element-ui form表单里面form-item想的校验规则来处理(每一个form-item项都可以单独校验)。 上代码: 重点是这个: :rules=“sqyxForm.jtpslx

    2024年02月15日
    浏览(37)
  • 【element-ui】form表单动态修改rules校验项

    在项目开发过程中,该页面有暂存和提交两个按钮,其中暂存和提交必填项校验不一样,此时需要动态增减必填项校验 ,解决方法如下: 增加rules校验项 删除rules校验项

    2024年02月04日
    浏览(43)
  • el-form 动态表单增减项 (vue+element ui)

    1、点击”+“,弹出弹窗,新增一项,点击”-“,删除当前项 代码展示: html代码: 注意: el-form-item(表单项)循环,绑定的数组写在form当中 表单: 新增参数弹框: data: methods: 1、点击新增,弹出新增弹窗,添加表单项 2、点击”-“,删除当前表单项

    2024年02月02日
    浏览(37)
  • Vue+Element-UI el-form表单动态检验

    业务需求: 表单el-form 有一表单项:发布时间 ,有5个选项:今天、24小时、近3天、近7天和自定义时间,其中当选择自定义时间时,后面跟着的日期时间选择器是必填的,选中其他选项时则不需要。这就需要做到表单的动态检验。 最开始实现方式是在当前表单项中设置规则

    2024年02月11日
    浏览(41)
  • 【vue2+element ui】添加修改共用表单的下拉框回显问题分析以及解决方案(附共用表单代码)

    简介 本人前端水平不佳,本文分享在编写个人项目前端代码的时候遇到的回显问题的解决办法,仅供参考。 问题复原 首先展示表单中的问题代码,本次前端的设计是添加和修改操作共用表单,但是其中有一个下拉框展示形式,按照惯例通过:value进行双向绑定,保证点击修改

    2024年02月07日
    浏览(32)
  • element UI —— form表单中Radio单选框进行切换 & 表单验证rule动态校验-validator & 保存前进行form表单校验后才能上传-validate

    element UI —— form表单中Radio单选框进行切换 表单验证rule动态校验-validator 保存前进行form表单校验后才能上传-validate 1、效果图 2、代码 结构 数据

    2024年02月07日
    浏览(40)
  • element-ui表单动态添加必填校验

    业务场景:根据form表单中的某些下拉框选中值,动态切换一些属性的必填校验。 效果图: 当活动区域非必填时,活动名称取消必填校验,否则活动名称必填。

    2024年02月11日
    浏览(35)
  • 关于element ui select,date-picker添加:popper-append-to-body=“false“后,下拉框下拉方向不会自动调整的问题

    element ui select,date-picker添加 :popper-append-to-body=\\\"false\\\" 后,下拉框下拉方向不会自动调整; 比如说select的位置处于页面最下方时,不加 popper-append-to-body=\\\"false\\\" 下拉框会自动向上展开,但是加上 popper-append-to-body=\\\"false\\\" 后下拉框就不会自动向上展开,依然保持默认的向下展开,将

    2024年02月15日
    浏览(33)
  • Element+Vue实现动态表单(多个下拉框组件)

    表单的内容为巡检计划,巡检计划可以选择多个巡检点位,每个巡检点位可以选择多个设备和对应操作。 点击加号图标增加一个下拉框 减号图标删除对应下拉框 下拉框备选项目相同 点击设置动作按钮,弹出可编辑表格,可以为该巡检点位设置多个动作 表格每行内容可编 设

    2024年02月15日
    浏览(30)
  • element-ui的el-form表单和el-table校验嵌套使用校验el-input和el-select

    场景:前端开发中,经常会遇到比较多的表单填写页面,其中有el-form,el-table,表格的每一列中又嵌套着输入框或者下拉框,然后每个还需要做单独的校验 效果:   点击保存可校验el-input和el-select是否有值,不符合校验规则就标红提醒   1.el-form里面嵌套el-table 2.在el-table-column自定义内

    2024年02月02日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包