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)目寫出來(lái)的總結(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)文章
vue和thymeleaf相結(jié)合的注意事項(xiàng)詳解
這篇文章主要介紹了vue和thymeleaf相結(jié)合的注意事項(xiàng)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
vue優(yōu)化之優(yōu)雅的拋出錯(cuò)誤(Error)問(wèn)題
這篇文章主要介紹了vue優(yōu)化之優(yōu)雅的拋出錯(cuò)誤(Error)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
npm ERR! code 128的錯(cuò)誤問(wèn)題解決方法
這篇文章主要介紹了解決npm ERR! code 128的錯(cuò)誤問(wèn)題,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02
mpvue實(shí)現(xiàn)左側(cè)導(dǎo)航與右側(cè)內(nèi)容的聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了mpvue實(shí)現(xiàn)左側(cè)導(dǎo)航與右側(cè)內(nèi)容的聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
詳解vue項(xiàng)目中調(diào)用百度地圖API使用方法
這篇文章主要介紹了vue項(xiàng)目中調(diào)用百度地圖API使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Vue router重定向redirect如何傳值問(wèn)題
這篇文章主要介紹了Vue router重定向redirect如何傳值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
使用element組件table表格實(shí)現(xiàn)某條件下復(fù)選框無(wú)法勾選
這篇文章主要介紹了使用element組件table表格實(shí)現(xiàn)某條件下復(fù)選框無(wú)法勾選問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
解決vue組件銷毀之后計(jì)時(shí)器繼續(xù)執(zhí)行的問(wèn)題
這篇文章主要介紹了解決vue組件銷毀之后計(jì)時(shí)器繼續(xù)執(zhí)行的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07

