ant design vue動態(tài)循環(huán)生成表單以及自定義校驗(yàn)規(guī)則詳解
在ant design vue開發(fā)中有時候會利用后臺數(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ì)劃投入時間 person_info_sum:'',//實(shí)際投入數(shù)量 person_info_time:undefined,//實(shí)際投入時間 person_info_remark:'', currentParentGuid:'',//當(dāng)前選中的行,用于添加的的父級id } ] },
二、在對話框內(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="請選擇所屬公司" 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="請選擇所屬班組" 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="請輸入計(jì)劃投入數(shù)量" /> </a-form-model-item> </a-col> <a-col :span="12"> <a-form-model-item label="計(jì)劃投入時間:" :prop="'manObjList.'+index+'.person_info_plan_time'" :rules="[{ required: true, message: '計(jì)劃投入時間不能為空', 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="請輸入實(shí)際投入數(shù)量"/> </a-form-model-item> </a-col> <a-col :span="12"> <a-form-model-item label="實(shí)際投入時間:" :prop="'manObjList.'+index+'.person_info_time'" :rules="[{ required: true, message: '實(shí)際投入時間不能為空', trigger: 'change'}, {validator:manActualTime}]" > <a-date-picker v-model="item.person_info_time" placeholder="請選擇實(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: '備注長度不能大于200', trigger: 'blur' }]"> <a-input type="textarea" v-model="item.person_info_remark" placeholder="請輸入備注" :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要動態(tài)綁定,數(shù)組+索引+字段名=》manObjList.’+index+’.person_info_remark’
三、添加數(shù)據(jù)方法
// 添加一個人員信息表單 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ì)劃投入時間 person_info_sum:'',//實(shí)際投入數(shù)量 person_info_time:undefined,//實(shí)際投入時間 man_deviation_number:'',//投入數(shù)量偏差 man_deviation_time:'',//投入時間偏差 person_info_remark:'', currentParentGuid:'',//當(dāng)前選中的行,用于添加的的父級id }) },
循環(huán)表單中的自定義校驗(yàn)
在a-form-model-item中綁定動態(tài)rules
:rules="[ {required: true, message: '計(jì)劃投入時間不能為空', trigger: 'change'}, {validator:manPlanTime} ]"
// 人員的校驗(yàn)規(guī)則,計(jì)劃時間 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í)際投入時間')) return false } callback() } callback() },
寫在最后
這樣就能完成一個循環(huán)生成表單且完成自定義表單校驗(yàn),但還有一個小瑕疵,動態(tài)生成的日期選擇框綁定的是UTC格式的日期,而不是組件本身綁定的moment對象的日期格式。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue和thymeleaf相結(jié)合的注意事項(xiàng)詳解
這篇文章主要介紹了vue和thymeleaf相結(jié)合的注意事項(xiàng)詳解,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11vue優(yōu)化之優(yōu)雅的拋出錯誤(Error)問題
這篇文章主要介紹了vue優(yōu)化之優(yōu)雅的拋出錯誤(Error)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03mpvue實(shí)現(xiàn)左側(cè)導(dǎo)航與右側(cè)內(nèi)容的聯(lián)動
這篇文章主要為大家詳細(xì)介紹了mpvue實(shí)現(xiàn)左側(cè)導(dǎo)航與右側(cè)內(nèi)容的聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10詳解vue項(xiàng)目中調(diào)用百度地圖API使用方法
這篇文章主要介紹了vue項(xiàng)目中調(diào)用百度地圖API使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04使用element組件table表格實(shí)現(xiàn)某條件下復(fù)選框無法勾選
這篇文章主要介紹了使用element組件table表格實(shí)現(xiàn)某條件下復(fù)選框無法勾選問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03解決vue組件銷毀之后計(jì)時器繼續(xù)執(zhí)行的問題
這篇文章主要介紹了解決vue組件銷毀之后計(jì)時器繼續(xù)執(zhí)行的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07