动态表单:el-form-renderer

简介: 动态表单:el-form-renderer

一、需求分析:

现要展示基础信息表单,表单字段是动态的,根据场景不同所需字段内容与类型会不同,这时候我们引出今天要分析的内容:el-form-renderer

二、实现步骤:

2.1、安装:yarn add @femessage/el-form-renderer

       注:安装前需确认是否已正确安装并使用了element-ui

2.2、

使用el-form-renderer

<el-form-renderer
            :content="formContent"
            ref="form"
            class="form-box"/>

配置formContent:

这时候我们就可以动态的控制表单项的内容了,数据从接口获取即可:

export const formContent = [
  {
    $type: 'input',
    $id: 'operationDoctor',
    label: '字段名',
    $el: {
      placeholder: '请输入'
    },
    rules: [{
      required: true,
      message: '请输入',
      trigger: 'blur'
    }]
  },
  {
    $type: 'input',
    $id: 'diagnoseWay',
    label: '字段名',
    $el: {
      placeholder: '请输入'
    },
    rules: [{
      required: true,
      message: '请输入',
      trigger: 'blur'
    }]
  },
  {
    $type: 'date-picker',
    $id: 'operationDate',
    label: '字段名',
    $el: {
      type: 'datetime',
      placeholder: '选择日期',
      valueFormat: 'yyyy-MM-dd HH:mm:ss', // 不添加此属性将获取到一个空对象,如果需要校验在rules中不能加type:date属性否则报错
      prefixIcon: 'el-icon-date',
      pickerOptions: {
        disabledDate: time => {
          return time.getTime() < Date.now() - 8.64e7 // 8.64e7科学计数法,1天的毫秒数,1000 * 60 * 60 * 24 = 8.64 * 10 的 7 次方
        }
      }
    },
    rules: [{
      required: true,
      message: '请选择时间',
      trigger: 'change'
    }]
  },
  {
    $type: 'input',
    $id: 'sickName',
    label: '字段名',
    $el: {
      placeholder: '请输入'
    }
  },
  {
    $type: 'input',
    $id: 'sickAge',
    label: '字段名',
    $el: {
      placeholder: '请输入'
    }
  },
  {
    $type: 'input',
    $id: 'operationRoom',
    label: '字段名',
    $el: {
      placeholder: '请输入'
    }
  },
  {
    $type: 'select',
    $id: 'sickSex',
    label: '字段名',
    $el: {
      placeholder: '请选择'
    },
    $options: [
      {
        label: '男',
        value: 0
      },
      {
        label: '女',
        value: 1
      }
    ]
  },
  {
    $type: 'input',
    $id: 'admissionNumber',
    label: '字段名',
    $el: {
      placeholder: '请输入'
    }
  },
  {
    $type: 'input',
    $id: 'illArea',
    label: '字段名',
    $el: {
      placeholder: '请输入'
    }
  },
  {
    $type: 'input',
    $id: 'bedNum',
    label: '字段名',
    $el: {
      placeholder: '请输入'
    }
  }
]

2.3、对于复杂表单可以自定义组件(component):

2.3.1、定义组件:

<template>
  <div>
    <el-input :value="value" @input="onInput" />
    <el-button @click="onInput('输入')">输入</el-button>
  </div>
</template>
<script>
export default {
  props: {
    value: String
  },
  methods: {
    onInput(val) {
      this.$emit('input', val)
    }
  }
}
</script>

使用:

<template>
  <el-form-renderer :content="content"/>
</template>
<script>
import MyInput from './components/my-input.vue'
export default {
  data() {
    return {
      content: [
        {
          component: MyInput, // 使用组件
          id: 'myInput',
          label: '自定义输入框组件'
        }
      ]
    }
  },
}
</script>

三、效果如图:

 

四、欢迎留言,交流指正。

相关文章
|
9月前
|
人工智能 自然语言处理 测试技术
2026年TestOne 接口/界面/移动自动化实测,AI脚本生成到底好不好用?亲测揭秘
作为一名干了5年的测试老司机,最近团队要替换老旧的自动化测试工具,选型这块自然落到了我头上。前后对比了几款工具,最终先锁定了TestOne,花了整整一周时间,从安装部署到日常用例执行,从界面操作到核心功能实测,全程沉浸式使用,不吹不黑,记录下最真实的使用感受,给各位同行做个参考,也算是给自己的选型工作留个复盘。毕竟做测试的都懂,选对工具能少走一半弯路,尤其是TestOne这样主打一站式自动化测试的平台,实测体验才是硬道理。
|
前端开发 JavaScript
乾坤qiankun(微前端)样式隔离解决方案--使用插件替换前缀
乾坤qiankun(微前端)样式隔离解决方案--使用插件替换前缀
3344 9
|
Java
java操作内存,简单讲解varhandle的使用
本文介绍了Java中VarHandle的使用,它是一种从JDK 9开始引入的用于高效访问对象字段的特性。文章通过示例代码展示了如何通过VarHandle操作对象的字段,包括设置和获取字段值,以及如何通过MethodHandles.lookup().findVarHandle()方法获取VarHandle实例。VarHandle提供了一种比反射更高效的内存操作方式,并且支持原子操作。
635 0
java操作内存,简单讲解varhandle的使用
|
存储 JSON 开发框架
循序渐进VUE+Element 前端应用开发(27)--- 数据表的动态表单设计和数据存储
循序渐进VUE+Element 前端应用开发(27)--- 数据表的动态表单设计和数据存储
|
JavaScript 前端开发 数据处理
在vue中的form表单中下拉框中的数据来自数据库查询到的数据
这篇文章介绍了如何在Vue框架的表单中将下拉框的数据通过后端接口从数据库动态查询并加载,包括前端HTML代码、JavaScript数据处理、后端接口实现以及表单提交的完整流程。
在vue中的form表单中下拉框中的数据来自数据库查询到的数据
|
存储 安全 Java
“Spring Security 中的 Principal 是什么?
【8月更文挑战第21天】
2043 0
|
Java Apache
BeanUtils.populate()的用法总结
BeanUtils.populate()的用法总结
|
Java Linux
Centos安装openjdk11并配置JAVA_HOME
Centos安装openjdk11并配置JAVA_HOME
2022 0