ant design vue動(dòng)態(tài)循環(huán)生成表單以及自定義校驗(yàn)規(guī)則詳解
在ant design vue開發(fā)中有時(shí)候會(huì)利用后臺(tái)數(shù)據(jù)循環(huán)生成表單,需要我們綁定prop以及自定義校驗(yàn)事件
以下是我用官網(wǎng)提供的方法結(jié)合自身項(xiàng)目寫出來的總結(jié)
一、首先在data里定義表單數(shù)據(jù)
// 循環(huán)生成的人員表單數(shù)據(jù)
addManForm:{
manObjList:[
{
person_info_company_guid:undefined,//所屬公司
person_info_team_guid:undefined,//班組
person_info_plan_sum:'',//計(jì)劃投入數(shù)量
person_info_plan_time:undefined,//計(jì)劃投入時(shí)間
person_info_sum:'',//實(shí)際投入數(shù)量
person_info_time:undefined,//實(shí)際投入時(shí)間
person_info_remark:'',
currentParentGuid:'',//當(dāng)前選中的行,用于添加的的父級(jí)id
}
]
},
二、在對(duì)話框內(nèi)循環(huán)生成表單
<a-modal
:title="addManTitle"
:visible="manModalVisible"
@ok="manModalOk"
@cancel="manModalCancel"
okText="確定"
cancelText="取消"
:mask=false
:zIndex=1000
:width=850>
<a-form-model ref="addManFormRef" :model="addManForm" :label-col="{span:7}" :wrapper-col="{ span: 16 }">
<a-row>
</a-row>
<div class="manContent">
<div class="manSingle" v-for="(item,index) in addManForm.manObjList" :key="index">
<div class="title">
<div class="iconOperate">
<a-icon type="up" v-if="manArrowState && index==0" @click="manFold"/>
<a-icon type="down" v-if="!manArrowState && index==0" @click="manUnfold"/>
<i class="iconfont iconicon-test" v-if="!btnVisible && index==0" @click="addManObjList"></i>
<i class="iconfont iconshanchu" v-if="!btnVisible && index==0" @click="delManObjList"></i>
</div>
</div>
<div class="manContainer">
<a-row>
<a-col :span="12">
<a-form-model-item label="所屬公司:" :prop="'manObjList.'+index+'.person_info_company_guid'"
:rules="[{ required: true, message: '所屬公司不能為空', trigger: 'change'}]">
<a-tree-select
v-model="item.person_info_company_guid"
:tree-data="companySelectData"
style="width:100%"
placeholder="請(qǐng)選擇所屬公司"
allow-clear
:disabled="modalDisabled"
tree-default-expand-all/>
</a-form-model-item>
</a-col>
<a-col :span="12">
<a-form-model-item label="所屬班組:" :prop="'manObjList.'+index+'.person_info_team_guid'"
:rules="[{ required: true, message: '所屬班組不能為空', trigger: 'change'}]">
<a-tree-select
v-model="item.person_info_team_guid"
:tree-data="teamSelectData"
style="width:100%"
placeholder="請(qǐng)選擇所屬班組"
allow-clear
:disabled="modalDisabled"
tree-default-expand-all/>
</a-form-model-item>
</a-col>
</a-row>
<a-row>
<a-col :span="12">
<a-form-model-item label="計(jì)劃投入數(shù)量:" :prop="'manObjList.'+index+'.person_info_plan_sum'"
:rules="[{type: 'number', message: '僅支持輸入數(shù)字', trigger: 'blur' ,transform(value){
if(value){
// 將輸入的值轉(zhuǎn)為數(shù)字
var val = Number(value)
if(/^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/.test(val)) return val
// 返回false即為校驗(yàn)失敗
return false
}
}},
{required:true,message:'計(jì)劃投入數(shù)量不能為空',trigger: 'blur'}]">
<a-input v-model="item.person_info_plan_sum" :disabled="modalDisabled" placeholder="請(qǐng)輸入計(jì)劃投入數(shù)量" />
</a-form-model-item>
</a-col>
<a-col :span="12">
<a-form-model-item label="計(jì)劃投入時(shí)間:" :prop="'manObjList.'+index+'.person_info_plan_time'"
:rules="[{ required: true, message: '計(jì)劃投入時(shí)間不能為空', trigger: 'change'},
{validator:manPlanTime}]">
<a-date-picker v-model="item.person_info_plan_time" format="YYYY-MM-DD" :disabled="modalDisabled" style="width:100%"/>
</a-form-model-item>
</a-col>
</a-row>
<a-row>
<a-col :span="12">
<a-form-model-item label="實(shí)際投入數(shù)量:" :prop="'manObjList.'+index+'.person_info_sum'"
:rules="[{type: 'number', message: '僅支持輸入數(shù)字', trigger: 'blur' ,transform(value){
if(value){
// 將輸入的值轉(zhuǎn)為數(shù)字
var val = Number(value)
if(/^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/.test(val)) return val
// 返回false即為校驗(yàn)失敗
return false
}
}},
{required:true,message:'實(shí)際投入數(shù)量不能為空',trigger: 'blur'}
]">
<a-input v-model="item.person_info_sum" :disabled="modalDisabled" placeholder="請(qǐng)輸入實(shí)際投入數(shù)量"/>
</a-form-model-item>
</a-col>
<a-col :span="12">
<a-form-model-item label="實(shí)際投入時(shí)間:" :prop="'manObjList.'+index+'.person_info_time'"
:rules="[{ required: true, message: '實(shí)際投入時(shí)間不能為空', trigger: 'change'},
{validator:manActualTime}]" >
<a-date-picker v-model="item.person_info_time" placeholder="請(qǐng)選擇實(shí)際投入時(shí)間" style="width:100%" :disabled="modalDisabled"/>
</a-form-model-item>
</a-col>
</a-row>
<a-row>
<a-col :span="12">
<a-form-model-item label="備注:" :prop="'manObjList.'+index+'.person_info_remark'"
:rules="[{max: 200, message: '備注長(zhǎng)度不能大于200', trigger: 'blur' }]">
<a-input type="textarea" v-model="item.person_info_remark" placeholder="請(qǐng)輸入備注" :disabled="modalDisabled"/>
</a-form-model-item>
</a-col>
</a-row>
</div>
</div>
</div>
</a-form-model>
</a-modal>
fold和unfold方法控制展開折疊,addManObjList添加一組數(shù)據(jù),delManObjList刪除一組數(shù)據(jù)
注:prop要?jiǎng)討B(tài)綁定,數(shù)組+索引+字段名=》manObjList.’+index+’.person_info_remark’
三、添加數(shù)據(jù)方法
// 添加一個(gè)人員信息表單
addManObjList(){
this.addManForm.manObjList.push( {
person_info_company_guid:undefined,//所屬公司
person_info_team_guid:undefined,//班組
person_info_plan_sum:'',//計(jì)劃投入數(shù)量
person_info_plan_time:undefined,//計(jì)劃投入時(shí)間
person_info_sum:'',//實(shí)際投入數(shù)量
person_info_time:undefined,//實(shí)際投入時(shí)間
man_deviation_number:'',//投入數(shù)量偏差
man_deviation_time:'',//投入時(shí)間偏差
person_info_remark:'',
currentParentGuid:'',//當(dāng)前選中的行,用于添加的的父級(jí)id
})
},
循環(huán)表單中的自定義校驗(yàn)
在a-form-model-item中綁定動(dòng)態(tài)rules
:rules="[
{required: true, message: '計(jì)劃投入時(shí)間不能為空', trigger: 'change'},
{validator:manPlanTime}
]"
// 人員的校驗(yàn)規(guī)則,計(jì)劃時(shí)間
manPlanTime(rule,value,callback){
let planTimeArr = this.addManForm.manObjList.filter(item=>{
return item.person_info_plan_time == value
})
if(value && planTimeArr[0].person_info_time){
if(this.$moment(value).format('YYYY-MM-DD') > this.$moment(planTimeArr[0].person_info_time).format('YYYY-MM-DD')){
callback(new Error('計(jì)劃投入時(shí)間不得大于實(shí)際投入時(shí)間'))
return false
}
callback()
}
callback()
},
寫在最后
這樣就能完成一個(gè)循環(huán)生成表單且完成自定義表單校驗(yàn),但還有一個(gè)小瑕疵,動(dòng)態(tài)生成的日期選擇框綁定的是UTC格式的日期,而不是組件本身綁定的moment對(duì)象的日期格式。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
uniapp+Vue實(shí)現(xiàn)App端用戶每日彈出簽到彈窗功能
用戶簽到這個(gè)功能是很多APP、小程序等都具備的,也是其成長(zhǎng)體系中的重要環(huán)節(jié),這篇文章主要介紹了uniapp+Vue實(shí)現(xiàn)App端用戶每日彈出簽到彈窗功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-05-05
Vue3中我的Watch為什么總監(jiān)聽不到數(shù)據(jù)詳解
這篇文章主要介紹了Vue3中Watch為什么總監(jiān)聽不到數(shù)據(jù)的相關(guān)資料,文中還分析了Vue3中的watch監(jiān)聽函數(shù)和直接監(jiān)聽對(duì)象的區(qū)別,通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-03-03
使用Vue進(jìn)行圖片壓縮的實(shí)現(xiàn)方案和對(duì)比
在 Vue 項(xiàng)目中處理用戶上傳的大圖片時(shí),通過前端壓縮減小圖片體積是優(yōu)化上傳體驗(yàn)的有效方式,本文將和大家介紹幾個(gè)具體實(shí)現(xiàn)方案及相關(guān)說明,希望對(duì)大家有所幫助2025-10-10
vue+elementUI多表單同時(shí)提交及表單校驗(yàn)最新解決方案
假設(shè)有一個(gè)頁面,需要分三步填寫三個(gè)表單,且每個(gè)表單位于獨(dú)立的組件中,然后最后保存同時(shí)提交,如何進(jìn)行表單必填項(xiàng)校驗(yàn),下面小編給大家介紹vue+elementUI多表單同時(shí)提交及表單校驗(yàn)最新解決方案,感興趣的朋友一起看看吧2024-03-03
解決vue前端文件上傳報(bào)錯(cuò):上傳失敗,原因:413 Request Entity Too&
這篇文章主要介紹了解決vue前端文件上傳報(bào)錯(cuò):上傳失敗,原因:413 Request Entity Too Large,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Electron實(shí)現(xiàn)靜默打印小票的流程詳解
很多情況下程序中使用的打印都是用戶無感知的,并且想要靈活的控制打印內(nèi)容,往往需要借助打印機(jī)給我們提供的api再進(jìn)行開發(fā),這種開發(fā)方式非常繁瑣,并且開發(fā)難度較大,本文給大家介紹了Electron實(shí)現(xiàn)靜默打印小票的流程,感興趣的朋友可以參考下2024-06-06
vue裁切預(yù)覽組件功能的實(shí)現(xiàn)步驟
這篇文章主要介紹了vue裁切預(yù)覽組件功能的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05

