vue中怎么動態(tài)生成form表單
form-create 是一個可以通過 JSON 生成具有動態(tài)渲染、數(shù)據(jù)收集、驗證和提交功能的表單生成組件。支持3個UI框架,并且支持生成任何 Vue 組件。內(nèi)置20種常用表單組件和自定義組件,再復雜的表單都可以輕松搞定。
支持 UI
- element-ui
- iview/view-design
- ant-design-vue
新增功能
2.5版本主要更新了一下功能:
- 重構內(nèi)部核心代碼
- 優(yōu)化內(nèi)部渲染機制
- 優(yōu)化內(nèi)部生命周期事件
- 重構
TypeScript - 新增
nextTick方法,設置渲染后的回調(diào) - 新增 支持分頁加載組件,加速首屏渲染
- 新增 自定義配置項
effect - 新增 支持修改
type - 新增
control支持配置規(guī)則插入位置 - 優(yōu)化
control符合條件的都會生效,之前版本只能生效第一個 - 新增 支持給組件配置前綴后綴
prefix,suffix - 新增
update配置,value發(fā)送變化后觸發(fā) - 新增 支持
wrap配置項,配置FormItem - 新增
object組件 - 新增 支持自定義
title,info組件 - 新增 富文本組件
wangEditor - 新增 原生事件支持事件注入
- 支持
value.sync獲取雙向綁定的formData - 優(yōu)化 默認的表單提交按鈕
安裝
根據(jù)自己使用的 UI 安裝對應的版本
element-ui 版本
npm i @form-create/element-ui
iview@2.x|3.x 版本
npm i @form-create/iview
iview/view-design@4.x 版本
npm i @form-create/iview4
ant-design-vue@1.5+ 版本
npm i @form-create/ant-design-vue
快速上手
本文以
element-ui為例
- 在 main.js 中寫入以下內(nèi)容:
import Vue from 'vue'
import ELEMENT from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import formCreate from '@form-create/element-ui'
Vue.use(ELEMENT)
Vue.use(formCreate)
- 生成表單
<template>
<div id="app1">
<form-create v-model="fApi" :rule="rule" :option="option" :value.sync="value"></form-create>
</div>
</template>
export default {
data() {
return {
//實例對象
fApi: {},
//表單數(shù)據(jù)
value: {},
//表單生成規(guī)則
rule: [
{
type: 'input',
field: 'goods_name',
title: '商品名稱'
},
{
type: 'datePicker',
field: 'created_at',
title: '創(chuàng)建時間'
}
],
//組件參數(shù)配置
option: {
//表單提交事件
onSubmit: function (formData) {
alert(JSON.stringify(formData))
}
}
}
}
}
功能介紹
1. 自定義組件
form-create 支持的在表單內(nèi)部生成任何 vue 組件
例如生成一個el-button組件:
{
//type 可以是內(nèi)置組件名稱,也可以是vue組件名稱或者 html 標簽
type: 'el-button',
//...
children: ['按鈕內(nèi)容']
}
生成一個 html 標簽
{
//type 可以是內(nèi)置組件名稱,也可以是vue組件名稱或者 html 標簽
type: 'span',
//...
children: ['span內(nèi)容']
}
注意! 生成的組件必須掛載到全局或者通過 form-create 掛載
通過 Vue 掛載
Vue.component(component.name, component);
通過 form-create 掛載
formCreate.component(component.name, component);
2. 自定義布局
通過設置配置項col或者柵格組件可以實現(xiàn)對組件的布局
通過配置項col設置組件布局,設置一行顯示兩個組件
[
{
type:'input',
field:'input1',
title:'input1',
col:{span:12}
},{
type:'input',
field:'input2',
title:'input2',
col:{span:12}
},
]
通過柵格組件設置一行顯示三個組件
{
type:'el-row',
children: [
{
type:'el-col',
props:{
span:8
},
children: [{type:'input',field:'input1',title:'input1'}]
},
{
type:'el-col',
props:{
span:8
},
children: [{type:'input',field:'input1',title:'input1'}]
},
{
type:'el-col',
props:{
span:8
},
children: [{type:'input',field:'input1',title:'input1'}]
}
]
}
3. 組件前后綴
通過生成規(guī)則的prefix屬性配置組件的前綴,suffix屬性配置組件的后綴
{
type:'input',
field:'text',
title:'text',
prefix:'prefix',
suffix:'suffix',
}
設置前后綴為自定義組件
{
type:'input',
field:'text',
title:'text',
value:'default',
prefix:{
type:'ElButton', children:['prefix']
},
suffix:{
type:'ElButton', children:['suffix']
},
}
4.組件聯(lián)動
可以通過control配置項實現(xiàn)通過組件的值控制其他組件是否顯示
例如當評價小于3星時輸入差評原因
{
type:'rate',
field: 'star',
title:'評分',
value:5,
control:[
{
handle(val){
return val < 3
},
rule:[
{
type:'input',
field:'info',
title:'差評原因',
value:'default info',
}
]
}
]
}
| 參數(shù) | 說明 | 類型 |
|---|---|---|
| value | 當組件的值和value全等時顯示rule中的組件 |
string|Number|Bool |
| handle | 當handle方法返回true時顯示rule中的組件 |
Function |
| rule | 該組件控制顯示的組件 | Array |
| append | 設置rule中的規(guī)則追加的位置 |
string |
| prepend | 設置rule中的規(guī)則前置插入的位置 |
string |
| child | 設置rule是否插入到指定位置的children中,默認添加到當前規(guī)則的 children 中 |
Boolean |
注意! handle優(yōu)先級大于value,所有符合條件的control都會生效
5. 表單驗證
可以通過 validate 配置項設置組件的驗證規(guī)則,自定義的表單組件也支持校驗
例如設置input 組件必填
{
type:'input',
//...
validate:[{required:true, type:'string', message:'請個輸入內(nèi)容'}]
}
| 參數(shù) | 說明 | 類型 | 默認值 |
|---|---|---|---|
| enum | 枚舉類型 | string | - |
| len | 字段長度 | number | - |
| max | 最大長度 | number | - |
| message | 校驗文案 | string | - |
| min | 最小長度 | number | - |
| pattern | 正則表達式校驗 | RegExp | - |
| required | 是否必選 | boolean | false |
| transform | 校驗前轉(zhuǎn)換字段值 | function(value) => transformedValue:any | - |
| type | 內(nèi)建校驗類型,可選項 | string | 'string' |
| validator | 自定義校驗 | function(rule, value, callback) | - |
| whitespace | 必選時,空格是否會被視為錯誤 | boolean | false |
注意!type需要根據(jù)組件的value類型定義
APi 介紹
下列是常用的方法
設置表單值
覆蓋方式,未定義的字段會設置為 null
type coverValue = (formData:{[field:string]:any}) => void
- 用法:
fApi.coverValue({goods_name:'HuaWei'})
合并方式,未定義的字段不做修改
interface setValue {
(formData:{[field:string]:any}): void
(field:string, value:any): void
}
- 用法:
fApi.setValue({goods_name:'HuaWei'})
別名方法changeValue, changeField
隱藏字段
interface hidden {
//隱藏全部組件
(status:Boolean):void
//隱藏指定組件
(status:Boolean, field:string):void
//隱藏部分組件
(status:Boolean, field:string[]):void
}
- 用法:
fApi.hidden(true, 'goods_name')
獲取組件隱藏狀態(tài)
type hiddenStatus = (field:string)=>Boolean
- 用法:
const status = fApi.hiddenStatus('goods_name')
獲取規(guī)則
interface getRule {
(field:string):Rule
(field:string, origin: true): FormRule
}
- 用法:
const rule = fApi.getRule('goods_name')
插入規(guī)則
前置插入
interface prepend {
//插入到第一個
(rule:FormRule):void
//插入指定字段前面
(rule:FormRule, field:string):void
//插入到指定字段 children 中
(rule:FormRule, field:string, child:true):void
}
- 用法:
fApi.prepend({
type:"input",
title:"商品簡介",
field:"goods_info",
value:"",
props: {
"type": "text",
"placeholder": "請輸入商品簡介",
},
validate:[
{ required: true, message: '請輸入商品簡介', trigger: 'blur' },
],
}, 'goods-name')
后置追加
interface append {
//插入到最后一個
(rule:FormRule):void
//插入指定字段后面
(rule:FormRule, field:string):void
//插入到指定字段 children 中
(rule:FormRule, field:string, child:true):void
}
- 用法:
fApi.append({
type:"input",
title:"商品簡介",
field:"goods_info",
value:"",
props: {
"type": "text",
"placeholder": "請輸入商品簡介",
},
validate:[
{ required: true, message: '請輸入商品簡介', trigger: 'blur' },
],
}, 'goods-name')
刪除指定規(guī)則
type removeRule = (rule:FormRule) => FormRule
- 用法:
const rule = {type:'input', /** ... **/}
fApi.removeRule(rule)
驗證表單
type validate = (callback:(...args:any[]) => void)=> void
- 用法:
fApi.validate((valid, fail) => {
if(valid){
//todo 表單驗證通過
}else{
//todo 表單驗證未通過
}
})
驗證指定字段
type validateField = (field, callback:(...args:any[]) => void)=> void
- 用法:
fApi.validateField('goods_name', (valid, fail) => {
if(valid){
//todo 字段驗證通過
}else{
//todo 字段驗證未通過
}
})
獲取表單數(shù)據(jù)
interface formData {
//獲取全部數(shù)據(jù)
(): {[field:string]:any }
//獲取部分字段的數(shù)據(jù)
(field:string[]): {[field:string]:any }
}
- 用法:
const formData = fApi.formData()
修改提交按鈕
type submitBtnProps = (props:Object) => void
- 用法:
fApi.submitBtnProps({disabled:true})
-
快捷操作:
fApi.btn.loading(true)設置提交按鈕進入loading狀態(tài)fApi.btn.disabled(true)設置提交按鈕禁用狀態(tài)fApi.btn.show(true)設置提交按鈕顯示狀態(tài)
修改重置按鈕
type resetBtnProps = ( props:Object) => void
- 用法:
fApi.resetBtnProps({disabled:true})
-
快捷操作:
fApi.resetBtn.loading(true)設置重置按鈕進入loading狀態(tài)fApi.resetBtn.disabled(true)設置重置按鈕禁用狀態(tài)fApi.resetBtn.show(true)設置重置按鈕顯示狀態(tài)
隱藏表單
type hideForm = (hide:Boolean)=>void
- 用法:
fApi.hideForm(true)
提交表單
type submit = (success: (formData: FormData, $f: fApi) => void, fail: ($f: fApi) => void)=> void
- 用法:
fApi.submit((formData, fapi) => {
//todo 提交表單
},()=>{
//todo 表單驗證未通過
})
如果對您有幫助,您可以在GitHub上點 'Star' 支持一下 謝謝!

浙公網(wǎng)安備 33010602011771號