欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ant design vue動態(tài)循環(huán)生成表單以及自定義校驗規(guī)則詳解

 更新時間:2024年01月03日 10:03:53   作者:輕嘆年華逝,  
這篇文章主要介紹了ant design vue動態(tài)循環(huán)生成表單以及自定義校驗規(guī)則詳解,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

在ant design vue開發(fā)中有時候會利用后臺數(shù)據(jù)循環(huán)生成表單,需要我們綁定prop以及自定義校驗事件

以下是我用官網(wǎng)提供的方法結(jié)合自身項目寫出來的總結(jié)

一、首先在data里定義表單數(shù)據(jù)

// 循環(huán)生成的人員表單數(shù)據(jù)
addManForm:{
    manObjList:[
        {
            person_info_company_guid:undefined,//所屬公司
            person_info_team_guid:undefined,//班組
            person_info_plan_sum:'',//計劃投入數(shù)量
            person_info_plan_time:undefined,//計劃投入時間
            person_info_sum:'',//實際投入數(shù)量
            person_info_time:undefined,//實際投入時間
            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="計劃投入數(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即為校驗失敗
	                                        return false
	                                    }
	                                }},
	                                {required:true,message:'計劃投入數(shù)量不能為空',trigger: 'blur'}]">
	                                <a-input v-model="item.person_info_plan_sum" :disabled="modalDisabled" placeholder="請輸入計劃投入數(shù)量" />
	                            </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="實際投入數(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即為校驗失敗
	                                        return false
	                                    }
	                                }},
	                                {required:true,message:'實際投入數(shù)量不能為空',trigger: 'blur'}
	                            ]">
	                                <a-input v-model="item.person_info_sum" :disabled="modalDisabled" placeholder="請輸入實際投入數(shù)量"/>
	                            </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>

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:'',//計劃投入數(shù)量
        person_info_plan_time:undefined,//計劃投入時間
        person_info_sum:'',//實際投入數(shù)量
        person_info_time:undefined,//實際投入時間
        man_deviation_number:'',//投入數(shù)量偏差
        man_deviation_time:'',//投入時間偏差
        person_info_remark:'',
        currentParentGuid:'',//當(dāng)前選中的行,用于添加的的父級id
    })
},

循環(huán)表單中的自定義校驗

在a-form-model-item中綁定動態(tài)rules

:rules="[
	{required: true, message: '計劃投入時間不能為空', trigger: 'change'},
	{validator:manPlanTime}
]"
// 人員的校驗規(guī)則,計劃時間
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()
},

寫在最后

這樣就能完成一個循環(huán)生成表單且完成自定義表單校驗,但還有一個小瑕疵,動態(tài)生成的日期選擇框綁定的是UTC格式的日期,而不是組件本身綁定的moment對象的日期格式。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue和thymeleaf相結(jié)合的注意事項詳解

    vue和thymeleaf相結(jié)合的注意事項詳解

    這篇文章主要介紹了vue和thymeleaf相結(jié)合的注意事項詳解,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • vue優(yōu)化之優(yōu)雅的拋出錯誤(Error)問題

    vue優(yōu)化之優(yōu)雅的拋出錯誤(Error)問題

    這篇文章主要介紹了vue優(yōu)化之優(yōu)雅的拋出錯誤(Error)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • npm ERR! code 128的錯誤問題解決方法

    npm ERR! code 128的錯誤問題解決方法

    這篇文章主要介紹了解決npm ERR! code 128的錯誤問題,本文給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02
  • mpvue實現(xiàn)左側(cè)導(dǎo)航與右側(cè)內(nèi)容的聯(lián)動

    mpvue實現(xiàn)左側(cè)導(dǎo)航與右側(cè)內(nèi)容的聯(lián)動

    這篇文章主要為大家詳細(xì)介紹了mpvue實現(xiàn)左側(cè)導(dǎo)航與右側(cè)內(nèi)容的聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 詳解vue項目中調(diào)用百度地圖API使用方法

    詳解vue項目中調(diào)用百度地圖API使用方法

    這篇文章主要介紹了vue項目中調(diào)用百度地圖API使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • vue過濾器用法實例分析

    vue過濾器用法實例分析

    這篇文章主要介紹了vue過濾器用法,結(jié)合實例形式總結(jié)分析了vue.js常見過濾器相關(guān)使用技巧與操作注意事項,需要的朋友可以參考下
    2019-03-03
  • Vue router重定向redirect如何傳值問題

    Vue router重定向redirect如何傳值問題

    這篇文章主要介紹了Vue router重定向redirect如何傳值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 使用element組件table表格實現(xiàn)某條件下復(fù)選框無法勾選

    使用element組件table表格實現(xiàn)某條件下復(fù)選框無法勾選

    這篇文章主要介紹了使用element組件table表格實現(xiàn)某條件下復(fù)選框無法勾選問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 解決vue組件銷毀之后計時器繼續(xù)執(zhí)行的問題

    解決vue組件銷毀之后計時器繼續(xù)執(zhí)行的問題

    這篇文章主要介紹了解決vue組件銷毀之后計時器繼續(xù)執(zhí)行的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Vue.js實戰(zhàn)之Vuex的入門教程

    Vue.js實戰(zhàn)之Vuex的入門教程

    這篇文章主要給大家介紹了Vue.js實戰(zhàn)之Vuex的入門教程,文中介紹的非常詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-04-04

最新評論