element ui实现多层级复杂表单的操作(添加与回显)之表单操作交互操作

简介: element ui实现多层级复杂表单的操作(添加与回显)之表单操作交互操作


前言

现在是2022年5月3日11:47:15!劳动节假期已经过去了三天了,今天是被封家里的第7天,也是解封的第一天。

说实话,在家里的工作效率一点都不如公司的高,这两天完善了下系统中一个复杂表单的操作,大致实现的是这样的功能:

  • 一个设备下是可以添加多个采集类型的
  • 每个采集类型下面可以添加多个元素个数
  • 根据元素个数不同,来添加其他信息

由于内容有点多,所以我分了三篇来整理,分别是:

  1. element ui实现多层级复杂表单的操作(添加与回显)之表单操作交互操作
  2. element ui实现多层级复杂表单的操作(添加与回显)之添加功能实现
  3. element ui实现多层级复杂表单的操作(添加与回显)之回显功能实现

实现效果图如下:

有些地方打码了,不过不影响整个效果的展示。这个写起来还是有点复杂的,尤其是在回显内容的时候,费了不少周折,下面来分享一下这块儿功能的实现。

实现思路

  • 声明一个变量,用来存放采集数据的个数。
  • 通过v-for循环,展示页面中有多少个采集数据的表单个数。
  • 点击最下面新增的按钮时,会添加一个采集数据的表单,所以这块儿内容我们得动态操作,我这边的方法是,v-model="xxx[index]"的方式实现,index用来区分当前是第几个,在后面取值的时候也方便。
  • 继续身边一个formList变量,作为下面表格的表单,也是根据元素个数来循环的
  • 注意:凡是在循环中的,v-model都不可以一样,否则会造成数值错乱的情况(多个表单中的值一样)

实现展示多个采集数据表单

html代码:

<div v-for="(item,index) in attribute" v-if="index < checkList.length">
          <el-row :gutter="10" style="margin-top: 20px;">
            <el-row>
              <el-col :span="12"><span style="font-size: 15px;font-weight: 600;margin: 5px 0 0 30px;">采集数据</span></el-col>
            </el-row>
            <el-row style="margin-top: 20px;">
              <el-col :span="8">
                <el-form-item
                  label="采集数据类型:"
                  prop="checkList"
                  :rules="[
                    {
                      required: true,
                      message: '请选择采集数据类型',
                      trigger: 'blur',
                    },
                  ]">
                  <el-select v-model="deviceDataCheck[index]" @change="deviceDataCheckChange($event,[index])" placeholder="请选择采集数据类型">
                    <el-option
                      v-for="item in checkList"
                      :key="item.dictKey"
                      :label="item.dictValue"
                      :value="item.dictKey"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="固定:"
                  prop=""
                  :rules="[
                    {
                      required: false,
                      message: '示例:100或者250',
                      trigger: 'blur',
                    },
                  ]">
                  <el-input-number style="width: 80%;" :min="1" v-model="samplingFrequency[index]" placeholder="示例:100" :step="1"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="类别式:"
                  prop="schemeOfDataSourceType"
                  :rules="[
                    {
                      required: true,
                      message: '大于或等于1的整数',
                      trigger: 'blur',
                    },
                  ]">
                  <el-select v-model="schemeOfDataSourceType[index]" placeholder="请选择 类别描述方式">
                    <el-option
                      v-for="item in schemeOfDataSourceTypeList"
                      :key="item.dictKey"
                      :label="item.dictValue"
                      :value="item.dictKey"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
            <el-col :span="8">
                <el-form-item label="数据元素个数:" prop="countOfCompound" :rules="[{
                  required: false,
                  message: '大于或等于1的整数',
                  trigger: 'blur',}]">
                  <el-input-number style="width: 80%;"
                                   :disabled="isCountOfComw[index]" :min="1" v-model="countOfCompound[index]"
                                   @change="handleChange($event,[index])" placeholder="示例:1" controls-position="right"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="测量:"
                  prop="measurePeriodExist"
                  :rules="[
                    {
                      required: true,
                      message: '请选择',
                      trigger: 'blur',
                    },
                  ]"
                >
                  <el-select
                    v-model="measurePeriodst[index]"
                    @change="forceUpdateChange"
                    placeholder="请选择">
                    <el-option
                      v-for="item in measurePeriod"
                      :key="item.dictKey"
                      :label="item.dictValue"
                      :value="item.dictKey"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-if="countOfCompound[index] != null" :span="24" style="margin-bottom: 30px">
                <el-col :span="24" style="margin-bottom: 0px">
                  <el-col style="text-align: center;line-height: 40px;border: 1px solid #dcdfe6;margin-bottom: 0px;" :span="3">序号</el-col>
                  <el-col style="text-align: center;line-height: 40px;border: 1px solid #dcdfe6;margin-bottom: 0px;":span="5">采集数据类型</el-col>
                  <el-col
                    style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                    "
                    :span="4">类别</el-col>
                  <el-col
                    style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                    "
                    :span="4"
                  >单位</el-col
                  >
                  <el-col
                    style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                    "
                    :span="3"
                  >类型</el-col
                  >
                  <el-col
                    style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                    "
                    :span="5"
                  >类型</el-col
                  >
                </el-col>
                
          <el-divider></el-divider>
        </div>

页面中有多少采集数据的表单取决于,点了多少下新增的按钮,默认只有一个,点一下新增按钮就多加一个 ,所以此时我们需要声明一个用来记录采集数据个数的变量。在data中声明:

attribute: [1],

然后在method中写新增的点击事件:

//新增采集数据属性按钮
    addAttribute(){
      //小于采集数据类型的话可以加
      if(this.attribute.length<this.checkList.length){
        this.attribute.push(this.attribute.length+1);
      }
    },

checkList的值是采集数据类型的下拉,先声明在使用,代码如下:

checkList: [],

mounted中构造checkList集合:

//采集数据类型
    getDictionary({ code: "deviceData" }).then((res) => {
      this.checkList = res.data.data;
      let arr = [];
      for(let i = 0; i < this.checkList.length; i++) {
        arr[i] = true;
      }
      this.isCountOfCompoundShow = [arr];
    });

注意此处加了个判断,当时考虑的是,采集数据不可以无限新增,取决于采集数据下面的采集数据类型下拉,下拉中有多少个值,就可以新加多少个,为了直观展示,可以看看下面的图:

现在我们就实现了,点击新增按钮时,动态的添加一个采集数据表单,默认之后一个,最多只能添加采集数据类型下拉框中的个数。

根据元素个数,动态生成表格

因为下面表格中的行数是由上面元素个数来决定的,所以需要编写元素个数数字表单的改变事件。此处还有个地方需要验证,即上面采集数据类型如果不写的话,下面元素个数是禁用状态,无法操作。

html代码

<el-row>
            <el-col :span="8">
                <el-form-item label="数据元素个数:" prop="countOfCompound" :rules="[{
                  required: false,
                  message: '大于或等于1的整数',
                  trigger: 'blur',}]">
                  <el-input-number style="width: 80%;"
                                   :disabled="isCountOfCompoundShow[index]" :min="1" v-model="countOfCompound[index]"
                                   @change="handleChange($event,[index])" placeholder="示例:1" controls-position="right"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item
                  label="测:"
                  prop="measurePeriodExist"
                  :rules="[
                    {
                      required: true,
                      message: '请选择是',
                      trigger: 'blur',
                    },
                  ]"
                >
                  <el-select
                    v-model="measurePeriodExist[index]"
                    @change="forceUpdateChange"
                    placeholder="请选择">
                    <el-option
                      v-for="item in measurePeriodExistList"
                      :key="item.dictKey"
                      :label="item.dictValue"
                      :value="item.dictKey"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-if="countOfCompound[index] != null" :span="24" style="margin-bottom: 30px">
                <el-col :span="24" style="margin-bottom: 0px">
                  <el-col style="text-align: center;line-height: 40px;border: 1px solid #dcdfe6;margin-bottom: 0px;" :span="3">序号</el-col>
                  <el-col style="text-align: center;line-height: 40px;border: 1px solid #dcdfe6;margin-bottom: 0px;":span="5">采集数据类型</el-col>
                  <el-col
                    style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                    "
                    :span="4">数据源各元素类别</el-col>
                  <el-col
                    style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                    "
                    :span="4"
                  >数值单位</el-col
                  >
                  <el-col
                    style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                    "
                    :span="3"
                  >数据类型</el-col
                  >
                  <el-col
                    style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                    "
                    :span="5"
                  >度量属性类型</el-col
                  >
                </el-col>
                <!--表格-->
                <el-col v-for="(items, indexL) in formList[index]" :key="indexL" :span="24" style="margin-bottom: 0px; height: 40px">
                  <!-- 序号 -->
                  <el-col style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                      height: 40px;
                    " :span="3">{{ indexL + 1 }}</el-col>
                  <!-- 采集数据类型 -->
                  <el-col style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                      height: 40px;
                    " :span="5"><span v-if="deviceDataCheck[index] == 'hr'">心率</span><span v-if="deviceDataCheck[index] == 'rri'">RRI</span></el-col>
                  <!-- 数据源各元素类别 -->
                  <el-col style="text-align: center;line-height: 40px;border: 1px solid #dcdfe6;margin-bottom: 0px;height: 40px;" :span="4">
                    <el-form-item prop="dataSourceType"
                      :rules="[
                        {
                          required: true,
                          message: '请选择数据源类别',
                          trigger: 'blur',
                        },
                      ]"
                      label-width="0px">
                      <el-select v-model="items.dataSourceType" @change="forceUpdateChange" style="width: 100%" placeholder="请选择数据源各元素类别">
                        <el-option v-for="item in dataSourceTypeList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <!-- 数值单位 -->
                  <el-col style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                      height: 40px;
                    " :span="4">
                    <el-form-item
                      prop="unitCode"
                      :rules="[
                        {
                          required: true,
                          message: '请选择数值单位',
                          trigger: 'blur',
                        },
                      ]"
                      label-width="0px"
                    >
                      <el-select
                        v-model="items.unitCode"
                        @change="forceUpdateChange"
                        style="width: 100%"
                        placeholder="请选择 数值单位"
                      >
                        <el-option
                          v-for="item in unitCodeList"
                          :key="item.dictKey"
                          :label="item.dictValue"
                          :value="item.dictKey"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <!-- 数据类型 -->
                  <el-col style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                      height: 40px;
                    " :span="3">
                    <el-form-item
                      prop="dataType"
                      :rules="[
                        {
                          required: true,
                          message: '请选择采集数据类型',
                          trigger: 'blur',
                        },
                      ]"
                      label-width="0px"
                    >
                      <el-select
                        v-model="items.dataType"
                        @change="forceUpdateChange"
                        style="width: 100%"
                        placeholder="请选择 数据类型"
                      >
                        <el-option
                          v-for="item in dataTypeList"
                          :key="item.dictKey"
                          :label="item.dictValue"
                          :value="item.dictKey"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <!-- 度量属性类型 -->
                  <el-col style="
                      text-align: center;
                      line-height: 40px;
                      border: 1px solid #dcdfe6;
                      margin-bottom: 0px;
                      height: 40px;
                    " :span="5">
                    <el-form-item
                      prop="metricSpec"
                      :rules="[
                        {
                          required: true,
                          message: '请选择度量属性类型',
                          trigger: 'blur',
                        },
                      ]"
                      label-width="0px"
                    >
                      <el-select
                        v-model="items.metricSpec"
                        @change="forceUpdateChange"
                        style="width: 100%"
                        placeholder="请选择 度量属性类型"
                      >
                        <el-option
                          v-for="item in metricSpecList"
                          :key="item.dictKey"
                          :label="item.dictValue"
                          :value="item.dictKey"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-col>
              </el-col>
            </el-row>

接下来就是元素个数数字表单的改变事件:

//元素个数的改变事件
    handleChange(value,index) {
      this.formList[index] = [];
      let num = this.formList[index].length;
      for (let i = 0; i < value - num; i++) {
        this.formList[index].push({
          dataSourceType: "",
          unitCode: "",
          dataType: "",
          metricSpec: "",
          //checkType: this.checkList[0].dictValue,
        });
      }
    },

以上内容即可实现静态页面表单的交互。

相关文章
|
7月前
|
API UED 容器
深入探索 Element UI:自定义滚动条与弹出层管理的技巧
在这篇博客中,我们将深入探讨 Element UI 中的自定义滚动条及弹出层管理技巧。文章详细介绍了 el-scrollbar 组件的使用和参数设置,以及 PopupManager 如何有效管理弹出层的 z-index。我们还将探讨如何实现灵活的全屏组件,利用 vue-popper 创建自定义弹出层,最后介绍 ClickOutside 指令的用法。这些高级技巧将帮助你提升 Element UI 应用程序的用户体验与交互灵活性。
677 1
深入探索 Element UI:自定义滚动条与弹出层管理的技巧
|
7月前
|
JavaScript 索引
Vue开发中Element UI/Plus使用指南:常见问题(如Missing required prop: “value“)及中文全局组件配置解决方案
Vue开发中Element UI/Plus使用指南:常见问题(如Missing required prop: “value“)及中文全局组件配置解决方案
550 0
|
9月前
|
开发者 C# Android开发
明白吗?Xamarin与Native的终极对决:究竟哪种开发方式更适合您的项目需求,让我们一探究竟!
【8月更文挑战第31天】随着移动应用开发的普及,开发者面临多种技术选择。本文对比了跨平台解决方案Xamarin与原生开发方式的优势与劣势。Xamarin使用C#进行跨平台开发,代码复用率高,可大幅降低开发成本;但因基于抽象层,可能影响性能。原生开发则充分利用平台特性,提供最佳用户体验,但需维护多套代码库,增加工作量。开发者应根据项目需求、团队技能和预算综合考量,选择最适合的开发方式。
181 0
|
3月前
|
前端开发 安全 开发工具
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
240 90
【11】flutter进行了聊天页面的开发-增加了即时通讯聊天的整体页面和组件-切换-朋友-陌生人-vip开通详细页面-即时通讯sdk准备-直播sdk准备-即时通讯有无UI集成的区别介绍-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
1月前
|
人工智能 物联网 编译器
【01】优雅草星云物联网AI智控系统从0开发鸿蒙端适配完成流程-初始化鸿蒙编译器deveco studio项目结构-UI设计图切片下载-优雅草卓伊凡
【01】优雅草星云物联网AI智控系统从0开发鸿蒙端适配完成流程-初始化鸿蒙编译器deveco studio项目结构-UI设计图切片下载-优雅草卓伊凡
62 11
【01】优雅草星云物联网AI智控系统从0开发鸿蒙端适配完成流程-初始化鸿蒙编译器deveco studio项目结构-UI设计图切片下载-优雅草卓伊凡
|
6月前
|
搜索推荐 Android开发 开发者
探索安卓开发中的自定义视图:打造个性化UI组件
【10月更文挑战第39天】在安卓开发的世界中,自定义视图是实现独特界面设计的关键。本文将引导你理解自定义视图的概念、创建流程,以及如何通过它们增强应用的用户体验。我们将从基础出发,逐步深入,最终让你能够自信地设计和实现专属的UI组件。
|
2月前
|
关系型数据库 MySQL 数据库
基于Flink CDC 开发,支持Web-UI的实时KingBase 连接器,三大模式无缝切换,效率翻倍!
TIS 是一款基于Web-UI的开源大数据集成工具,通过与人大金仓Kingbase的深度整合,提供高效、灵活的实时数据集成方案。它支持增量数据监听和实时写入,兼容MySQL、PostgreSQL和Oracle模式,无需编写复杂脚本,操作简单直观,特别适合非专业开发人员使用。TIS率先实现了Kingbase CDC连接器的整合,成为业界首个开箱即用的Kingbase CDC数据同步解决方案,助力企业数字化转型。
315 5
基于Flink CDC 开发,支持Web-UI的实时KingBase 连接器,三大模式无缝切换,效率翻倍!
|
3月前
|
存储 人工智能 编译器
【03】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-测试hello word效果-虚拟华为手机真机环境调试-为DevEco Studio编译器安装中文插件-测试写一个滑动块效果-介绍诸如ohos.ui等依赖库-全过程实战项目分享-从零开发到上线-优雅草卓伊凡
【03】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-测试hello word效果-虚拟华为手机真机环境调试-为DevEco Studio编译器安装中文插件-测试写一个滑动块效果-介绍诸如ohos.ui等依赖库-全过程实战项目分享-从零开发到上线-优雅草卓伊凡
102 10
【03】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-测试hello word效果-虚拟华为手机真机环境调试-为DevEco Studio编译器安装中文插件-测试写一个滑动块效果-介绍诸如ohos.ui等依赖库-全过程实战项目分享-从零开发到上线-优雅草卓伊凡
|
3月前
|
JavaScript 数据安全/隐私保护
Vue Amazing UI 组件库(Vue3+TypeScript+Vite 等最新技术栈开发)
Vue Amazing UI 是一个基于 Vue 3、TypeScript、Vite 等最新技术栈开发构建的现代化组件库,包含丰富的 UI 组件和常用工具函数,并且持续不断维护更新中。另外,组件库全量使用 TypeScript,支持自动按需引入和 Tree Shaking 等,能够显著提升开发效率,降低开发成本。
205 5
Vue Amazing UI 组件库(Vue3+TypeScript+Vite 等最新技术栈开发)
|
2月前
|
开发框架 缓存 自然语言处理
HarmonyOS ArkTS声明式UI开发实战教程
本文深入探讨了ArkTS作为HarmonyOS生态中新一代声明式UI开发框架的优势与应用。首先对比了声明式与命令式开发的区别,展示了ArkTS如何通过直观高效的代码提升可维护性。接着分析了其核心三要素:数据驱动、组件化和状态管理,并通过具体案例解析布局体系、交互组件开发技巧及复杂状态管理方案。最后,通过构建完整TODO应用实战,结合调试优化指南,帮助开发者掌握声明式UI设计精髓,感受ArkTS的独特魅力。文章鼓励读者通过“破坏性实验”建立声明式编程思维,共同推动HarmonyOS生态发展。
132 3

热门文章

最新文章