在ant design vue开发中有时候会利用后台数据循环生成表单,需要我们绑定prop以及自定义校验事件
以下是我用官网提供的方法结合自身项目写出来的总结
// 循环生成的人员表单数据
addManForm:{
manObjList:[
{
person_info_company_guid:undefined,//所属公司
person_info_team_guid:undefined,//班组
person_info_plan_sum:'',//计划投入数量
person_info_plan_time:undefined,//计划投入时间
person_info_sum:'',//实际投入数量
person_info_time:undefined,//实际投入时间
person_info_remark:'',
currentParentGuid:'',//当前选中的行,用于添加的的父级id
}
]
},
<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="计划投入数量:" :prop="'manObjList.'+index+'.person_info_plan_sum'"
:rules="[{type: 'number', message: '仅支持输入数字', trigger: 'blur' ,transform(value){
if(value){
// 将输入的值转为数字
var val = Number(value)
if(/^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/.test(val)) return val
// 返回false即为校验失败
return false
}
}},
{required:true,message:'计划投入数量不能为空',trigger: 'blur'}]">
<a-input v-model="item.person_info_plan_sum" :disabled="modalDisabled" placeholder="请输入计划投入数量" />
</a-form-model-item>
</a-col>
<a-col :span="12">
<a-form-model-item label="计划投入时间:" :prop="'manObjList.'+index+'.person_info_plan_time'"
:rules="[{ required: true, message: '计划投入时间不能为空', 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="实际投入数量:" :prop="'manObjList.'+index+'.person_info_sum'"
:rules="[{type: 'number', message: '仅支持输入数字', trigger: 'blur' ,transform(value){
if(value){
// 将输入的值转为数字
var val = Number(value)
if(/^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/.test(val)) return val
// 返回false即为校验失败
return false
}
}},
{required:true,message:'实际投入数量不能为空',trigger: 'blur'}
]">
<a-input v-model="item.person_info_sum" :disabled="modalDisabled" placeholder="请输入实际投入数量"/>
</a-form-model-item>
</a-col>
<a-col :span="12">
<a-form-model-item label="实际投入时间:" :prop="'manObjList.'+index+'.person_info_time'"
:rules="[{ required: true, message: '实际投入时间不能为空', trigger: 'change'},
{validator:manActualTime}]" >
<a-date-picker v-model="item.person_info_time" placeholder="请选择实际投入时间" 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>
// 添加一个人员信息表单
addManObjList(){
this.addManForm.manObjList.push( {
person_info_company_guid:undefined,//所属公司
person_info_team_guid:undefined,//班组
person_info_plan_sum:'',//计划投入数量
person_info_plan_time:undefined,//计划投入时间
person_info_sum:'',//实际投入数量
person_info_time:undefined,//实际投入时间
man_deviation_number:'',//投入数量偏差
man_deviation_time:'',//投入时间偏差
person_info_remark:'',
currentParentGuid:'',//当前选中的行,用于添加的的父级id
})
},
在a-form-model-item中绑定动态rules
:rules="[
{required: true, message: '计划投入时间不能为空', trigger: 'change'},
{validator:manPlanTime}
]"
// 人员的校验规则,计划时间
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('计划投入时间不得大于实际投入时间'))
return false
}
callback()
}
callback()
},