基于element自动表单设计

news/2024/7/10 2:31:57 标签: 前端, javascript, html, vue, elementui
htmledit_views">

需求是根据JSON文件生成表单,包含配置和自动model属性以及表单验证,数据回显。

目录

动态表单数据示例:

表单设置JSON示例:

表单输入JSON示例:

表单按钮JSON示例:

抛出数据示例:

动态表单示例:

HTML模板部分:

PROPS部分:

表单验证器:

事件部分:

为了数据回显

props新增defaultValue:

computed增加监听:

data中申明

组件引用示例:

HTML 部分:(导入就不说了)

效果如下:


动态表单数据示例:

(page/home/index.js)

表单设置JSON示例:

html" title=javascript>javascript">/**
 * 搜索表单设置
 * @rules:  是否验证       {Boolean}
 * @inline: 是否内联       {Boolean}
 * @width:  label宽度     {String}
 * @align:  label对齐方式  {String}
 */
const searchFormSetting = {
  rules: false,
  inline: true,
  ref: 'searchForm',
  width: '40px',
  align: 'left'
}

表单输入JSON示例:

html" title=javascript>javascript">/**
 * 搜索表单
 * @type: 输入框的类型
 * @label: 输入框的label
 * @module: 输入框的v-module属性,不写会默认咦中文首字母拼音作为改属性
 * 注:select需要配合options使用
 */
const searchFormGroup = [
  { label: '早餐', type: 'input', module: 'zaofan' },
  { label: '日期', type: 'date' },
  { label: '地点', type: 'select', options: addressOptions }
]

表单按钮JSON示例:

html" title=javascript>javascript">/**
 * 搜索表单按钮事件
 * @name: 按钮名称
 * @event: 按钮事件名(子组件直接@eventName=handleCustomizeEvent)
 * @primary:按钮类型(按钮的颜色)
 * @icon:按钮的小图标
 */
const searchFormButton = [
  { name: '查询', event: 'search', type: 'primary', icon: 'el-icon-search' },
  { name: '重置', event: 'reset', icon: 'el-icon-refresh-left' },
  { name: '一键导出', event: 'export', icon: 'el-icon-download' }
]

抛出数据示例:

html" title=javascript>javascript">export { searchFormSetting, searchFormGroup, searchFormButton}

动态表单示例:

(components/autoForm/index.vue

HTML模板部分:

html"><template>
  <el-form ref="autoForm" :model="autoForm" :rules="autoRules" :label-width="setting.width" :inline="setting.inline" :label-position="setting.align" :id="setting.inline">
    <el-form-item v-for="(item, key) in form" :key="key" :label="item.label" :prop="chineseToPinYin(item.label)">
      <!--input-->
      <template v-if="item.type === 'input'">
        <template>
          <el-input v-model="autoForm[item.module || chineseToPinYin(item.label)]" :placeholder="'请输入'+item.label" @input="handleRefresh"/>
        </template>
      </template>
      <!--select-->
      <template v-if="item.type === 'select'">
        <el-select filterable v-model="autoForm[item.module || chineseToPinYin(item.label)]" :placeholder="'请选择'+item.label" @change="handleRefresh">
          <el-option v-for="(item_, key_) in item.options" :key="key+'_'+key_" :label="item_.label" :value="item_.value"></el-option>
        </el-select>
      </template>
      <!--date-->
      <template v-if="item.type === 'date'">
        <el-date-picker type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" :placeholder="'请选择'+item.label" v-model="autoForm[chineseToPinYin(item.label)]" @change="handleRefresh"></el-date-picker>
      </template>
      <!--radio-->
      <template v-if="item.type === 'radio'">
        <el-radio-group v-model="autoForm[item.module || chineseToPinYin(item.label)]" @change="handleRefresh">
          <el-radio v-for="(item_, key_) in item.options" :key="key_" :label="item_.label" :value="item_.value"></el-radio>
        </el-radio-group>
      </template>
      <!--textarea-->
      <template v-if="item.type === 'textarea'">
        <el-input type="textarea" v-model="autoForm[item.module || chineseToPinYin(item.label)]"/>
      </template>
    </el-form-item>
    <!--BUTTON--GROUP-->
    <el-form-item v-if="button">
      <el-button v-for="(item, key) in button" :key="'btn-'+key" :class="item.float" :icon="item.icon" :type="item.type" @click="handleButton(item.event, 'autoForm')">{{ item.name }}</el-button>
    </el-form-item>
  </el-form>
</template>

PROPS部分:

html" title=javascript>javascript">props: {
    setting: {
      type: Object,
      default: () => ({
        ref: 'form',
        width: '80px',
        align: 'left'
      })
    },
    button: {
      type: Array,
      default: () => [
        { name: '查询', event: 'search', type: 'primary', icon: 'el-icon-search' },
        { name: '重置', event: 'reset', type: 'success', icon: 'el-icon-refresh-left' }
      ]
    },
    form: {
      type: Array,
      default: () => [
        { label: '类型', type: 'select' },
        { label: '分数', type: 'input' },
        { label: '时间', type: 'date' }
      ]
    }
  }

表单验证器:

html" title=javascript>javascript">    this.form.forEach(item => {
      this.autoRules[chineseToPinYin(item.label)] = verify(item)
    })
    function verify (item) {
      let rules = []
      if (item.required && item.type === 'input') {
        rules = [{ required: true, message: `请输入${item.label}`, trigger: ['blur', 'change'] }]
      } else if (item.required && item.type === 'select') {
        rules = [{ required: true, message: `请选择${item.label}`, trigger: ['blur', 'change'] }]
      } else if (item.required && item.type === 'textarea') {
        rules = [{ required: true, message: `请输入${item.label}`, trigger: ['blur', 'change'] }]
      } else if (item.required && item.type === 'date') {
        rules = [{ type: 'date', required: true, message: `请选择${item.label}`, trigger: ['blur', 'change'] }]
      } else if (item.required && item.type === 'radio') {
        rules = [{ required: true, message: `请选择${item.label}`, trigger: ['blur', 'change'] }]
      }
      return rules
    }

事件部分:

html" title=javascript>javascript">    handleButton (event, autoForm) {
      if (event === 'reset' || event === 'cancel') {
        this.autoForms = {}
      } else {
        this.$refs[autoForm].validate((valid) => {
          if (valid) {
            this.$emit(event, this.autoForms, this.setting.ref)
          } else {
            this.$message({
              type: 'warning',
              message: '请检查您的输入'
            })
          }
        })
      }
    },
    handleRefresh () {
      this.$forceUpdate()
    }

为了数据回显

props新增defaultValue:

html" title=javascript>javascript">    defaultForm: {
      type: Object,
      default: () => ({})
    }

computed增加监听:

html" title=javascript>javascript">    autoForm () {
      return this.autoForms
    }

data中申明

(注意区分autoForms和autoForm)

html" title=javascript>javascript">    return {
      autoForms: this.defaultForm
    }

组件引用示例:

(page/home/index.vue

HTML 部分:(导入就不说了)

html"><auto-form :setting="searchFormSetting" :form="searchFormGroup" :button="searchFormButton" :default-form="searchForm" @search="handleClickSearchFormSearch" @export="handleClickSearchFormExport"/>

效果如下:


http://www.niftyadmin.cn/n/5210176.html

相关文章

2023 Unite 大会关于“Muse“ AI 大模型训练

Unity Muse 借助强大的 AI 能力帮助你探索、构思和迭代&#xff0c;其中包括纹理和精灵两项功能&#xff0c;可将自然语言和视觉输入转化为可用资产。 将 AI 引入 Unity Editor 中的 Muse 提供了更快将想法转化为实物的选项。您可以调整并使用文本提示、图案、颜色和草图&…

MySQL数据库入门到大牛_基础_15_存储过程与函数

前面我们介绍了数据自带的函数&#xff0c;此处提到的函数&#xff0c;指的是用户自定义函数。存储函数是一定会有返回值的&#xff0c;存储过程是可以没有返回值的。 MySQL从5.0版本开始支持存储过程和函数。存储过程和函数能够将复杂的SQL逻辑封装在一起&#xff0c;应用程序…

MySQL 中的 JSON_CONTAINS 函数详解

在处理 MySQL 中的 JSON 数据时&#xff0c;我们经常需要检查一个 JSON 文档是否包含特定的值。这时&#xff0c;JSON_CONTAINS 函数就显得非常有用。 JSON_CONTAINS函数介绍 JSON_CONTAINS 是 MySQL 提供的一个 JSON 函数&#xff0c;用于测试一个 JSON 文档是否包含特定的值…

对 .NET程序2G虚拟地址紧张崩溃 的最后一次反思

一&#xff1a;背景 1. 讲故事 最近接连遇到了几起 2G 虚拟地址紧张 导致的程序崩溃&#xff0c;基本上 90% 都集中在医疗行业&#xff0c;真的很无语&#xff0c;他们用的都是一些上古的 XP&#xff0c;Windows7 x86&#xff0c;我也知道技术人很难也基本无法推动硬件系统和…

经典的回溯算法题leetcode子集问题思路代码详解

目录 子集问题 leetcode78题.子集 leetcode90题.子集II 如果各位对回溯不太了解可以看我昨天写的文章&#xff0c;以及上一篇着重整列了回溯经典的组合问题。 回溯算法详解-CSDN博客 经典的回溯算法题leetcode组合问题整理及思路代码详解-CSDN博客 子集问题 组合问题我们…

(2023码蹄杯)省赛(初赛)第二场真题(原题)(题解+AC代码)

题目1&#xff1a;MC0214捡麦子 码题集OJ-捡麦子 (matiji.net) 思路: 1.第n米在前n-1米的基础上多加一个n个麦子&#xff0c;那么直接从1开始枚举&#xff0c;累加答案即可 AC_Code:C #include<bits/stdc.h> using namespace std;int main( ) {int n; cin>>n;…

ARKit增加一个盒子

ARKit增加一个盒子 体验一下ARKit的能力&#xff0c;在室内随便加点小球&#xff0c;然后在AR中显示出来。 效果如下图&#xff1a; 以下为操作流程。 新建项目 新建一个空项目&#xff0c;项目一定要选择 Augmented Reality App&#xff0c;能够省很多的事。 之后的 conte…

想问问各位大佬,网络安全这个专业普通人学习会有前景吗?

网络安全是一个非常广泛的领域&#xff0c;涉及到许多不同的岗位。这些岗位包括安全服务、安全运维、渗透测试、web安全、安全开发和安全售前等。每个岗位都有自己的要求和特点&#xff0c;您可以根据自己的兴趣和能力来选择最适合您的岗位。 渗透测试/Web安全工程师主要负责模…