首页 > 其他分享 >使用form-create生成表单组件

使用form-create生成表单组件

时间:2024-06-03 11:11:22浏览次数:12  
标签:form create UserSelect value 表单 options 组件 type formData

FormCreate 是一个可以通过 JSON 生成具有动态渲染、数据收集、验证和提交功能的表单生成组件。支持5个UI框架,并且支持生成任何 Vue 组件。内置20种常用表单组件和自定义组件,再复杂的表单都可以轻松搞定

FormCreate官网:https://www.form-create.com

帮助文档:https://form-create.com/v3/


1. 生成内置组件
<template>
  <div>
    <form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value">
    </form-create>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fApi: {},
      value: {},
      options: {
        onSubmit: (formData) => {
          alert(JSON.stringify(formData))
        },
        resetBtn: true
      },
      rule: [
        {type: 'input', field: 'field1', title: 'input', value: ''},
        {type: 'datePicker', field: 'field2', title: 'date', value: ''},
      ]
    }
  }
}
</script>

2. 生成UI组件

<template>
  <div>
    <form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value">
    </form-create>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fApi: {},
      value: {},
      options: {
        onSubmit: (formData) => {
          alert(JSON.stringify(formData))
        },
        resetBtn: true
      },
      rule: [
        {
          type: 'AButton',
          props: {
            size: 'mini'
          },
          children: ['AButton']
        },
        {
          type: 'AImage',
          //非表单组件不会自动生成col,自定义组件需要通过native控制是否生成col
          native: false,
          props: {
            width: '200px',
            src: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
          }
        },
      ]
    }
  }
}
</script>

 

3. 生成自定义组件

<template>
  <div>
    <form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value">
    </form-create>
  </div>
</template>

<script>
import {h, defineComponent, resolveComponent} from 'vue'
import formCrate from '@form-create/ant-design-vue'
import formCreate from "@form-create/ant-design-vue";
// import UserSelect from "./UserSelect.vue";
//自定义表单组件
const UserSelect = defineComponent({
  props: {
    //预定义
    disabled: Boolean,
    modelValue: Number,
    options: Array,
  },
  emits: ['update:modelValue'],
  data: function () {
    return {
      value: this.modelValue,
    }
  },
  render() {
    return h(resolveComponent('ASelect'), {
      disabled: this.disabled,
      value: this.value,
      'onUpdate:value': this.onChange,
      options: this.options
    });
  },
  watch: {
    modelValue(n) {
      //同步 value 的值
      this.value = n
    }
  },
  methods: {
    onChange: function (val) {
      this.value = val;
      //更新组件内部的值
      this.$emit('update:modelValue', val)
    },
  },
});

export default {
  beforeCreate() {
    //挂载组件
    formCrate.component('UserSelect', UserSelect);
  },
  data() {
    return {
      fApi: {},
      value: {},
      options: {
        onSubmit: (formData) => {
          alert(JSON.stringify(formData))
        },
        resetBtn: true
      },
      rule: [
        {
          type: 'UserSelect',
          field: 'field1',
          title: '用户选择',
          value: '小王',
          props: {
            options: [
              {label: '小王', value: '小王'},
              {label: '小李', value: '小李'},
            ]
          },
        },
      ]
    }
  }
}
</script>

 

4. 通过插槽生成组件

<template>
  <div>
    <form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value">
    </form-create>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fApi: {},
      value: {},
      options: {
        onSubmit: (formData) => {
          alert(JSON.stringify(formData))
        },
        resetBtn: true
      },
      rule: [
        {
          type: 'div',
          style: 'width:200px;height:200x;background-color:#FF7271;',
          children: [
            {
              type: 'span',
              style: 'color:#FFFFFF;',
              children: ['FormCreate']
            }
          ]
        },
      ]
    }
  }
}
</script>

 

5. 生成HTML标签

<template>
  <div>
    <form-create :rule="rule" v-model:api="fApi" :option="options" v-model="value">
      <template #type-UserSelect="scope">
        <ASelect :value="scope.model.value" v-bind="scope.prop" @update:value="scope.model.callback">
        </ASelect>
      </template>
    </form-create>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fApi: {},
      value: {},
      options: {
        onSubmit: (formData) => {
          alert(JSON.stringify(formData))
        },
        resetBtn: true
      },
      rule: [
        {
          type: 'UserSelect',
          field: 'field1',
          title: '用户选择',
          value: '小王',
          props: {
            options: [
              {label: '小王', value: '小王'},
              {label: '小李', value: '小李'},
            ]
          },
        },
      ]
    }
  }
}
</script>

 

标签:form,create,UserSelect,value,表单,options,组件,type,formData
From: https://www.cnblogs.com/xaboy/p/18228399

相关文章

  • java中SimpleDateFormat解析日期格式的问题
    在日常写代码的过程中,我们经常要处理各种格式的日期,常见的日期格式有:“20240601”,“2024-06-01”,“2024-6-1”。如何正确地处理日期格式,尤其是对外接口中参数的日期格式,就很重要了,一个不小心就可能出现意想不到的问题。举一个我遇到的真实例子:我们提供的对外接口中有一个参数是......
  • Data日期和DateFormat类
    packageJavaapi.data;/***@authorxiaowang*@creat2024/6/217:25*@DescriptionJavaLotus*/publicclassEmp{privateStringname;privateintage;privateDatabirthday;}Data类:归属包java.util作用:表示日期时间(精确到ms)使用: ......
  • WPF RenderTransform TransformGroup ScaleTransform TranslateTransform
    <Windowx:Class="WpfApp132.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • 微信小程序开发中的表单验证与数据提交
    表单验证和数据提交在微信小程序开发中是非常常见的功能。表单验证用于确保用户输入的数据符合指定的格式要求,数据提交用于将用户输入的数据发送到服务器端进行处理。下面是一个示例,演示了如何在微信小程序中进行表单验证和数据提交。首先,让我们创建一个简单的表单,包含用户名......
  • 翻译《The Old New Thing》- What a drag: Dragging a Uniform Resource Locator (URL
    Whatadrag:DraggingaUniformResourceLocator(URL)andtext-TheOldNewThinghttps://devblogs.microsoft.com/oldnewthing/20080313-00/?p=23123RaymondChen 2008年03月13日 麻烦的拖拽:拖拽统一资源定位符(URL)和文本简要        这篇文章主要讲......
  • 翻译《The Old New Thing》- What a drag: Dragging a Uniform Resource Locator (URL
    Whatadrag:DraggingaUniformResourceLocator(URL)-TheOldNewThing(microsoft.com)https://devblogs.microsoft.com/oldnewthing/20080312-00/?p=23133RaymondChen 2008年03月12日麻烦的拖拽:拖拽统一资源定位符(URL)简要本文介绍了如何在Windows程序中实......
  • 深度学习论文翻译解析(二十二):Uniformed Students Student-Teacher Anomaly Detection W
    论文标题:UniformedStudentsStudent-TeacherAnomalyDetectionWithDiscriminativeLatentEmbbeddings论文作者: PaulBergmann MichaelFauser DavidSattlegger CarstenSteger论文地址:https://openaccess.thecvf.com/content_CVPR_2020/papers/Bergmann_Uninformed......
  • Transformer 模型完全解读:代码+注释+讲解
    节前,我们组织了一场算法岗技术&面试讨论会,邀请了一些互联网大厂朋友、今年参加社招和校招面试的同学。针对大模型技术趋势、大模型落地项目经验分享、新手如何入门算法岗、该如何准备面试攻略、面试常考点等热门话题进行了深入的讨论。总结链接如下:重磅消息!《大模型面试......
  • Terraform管理OpenStack
      官方安装指南https://developer.hashicorp.com/terraform/installhttps://developer.hashicorp.com/terraform/intro/getting-started/install.html安装sudoyuminstall-yyum-utilssudoyum-config-manager--add-repohttps://rpm.releases.hashicorp.com/RHEL/h......
  • css39 CSS Forms
    https://www.w3schools.com/css/css_form.aspThelookofanHTMLformcanbegreatlyimprovedwithCSS:<!DOCTYPEhtml><html><style>input[type=text],select{width:100%;padding:12px20px;margin:8px0;display:inline-blo......