antd+vue實現(xiàn)動態(tài)驗證循環(huán)屬性表單的思路
希望實現(xiàn)查詢表單的某些屬性可以循環(huán)驗證必填項:
需求:
1.名稱,對比項,備注必填,默認(rèn)為一行,可增加多行
2.根據(jù)名稱,動態(tài)請求對比項列表,名稱變化時,清空該行當(dāng)前選擇的對比項
思路:將整個搜索分成了兩個表單,分別去做驗證。一個是可動態(tài)添加的循環(huán)表單form,另一個為普通表單dateForm

html
<a-form :form="form" @keyup.enter.native='searchQuery'>
<div class="dynamic-wrap">
<div v-for="(item,index) in formList" :key="index">
<a-row :gutter="24">
<a-col :span="6">
<a-form-item label="名稱" :labelCol="{span: 7}" :wrapperCol="{span: 17}">
<a-select placeholder='請選擇名稱'
v-decorator="[`equipment[${index}]`,{ initialValue: formList[index] ? formList[index].equipment : '', rules: [{ required: true, message: '請選擇設(shè)備!' }]}]"
@change="(e)=>equipChange(e,index)">
<a-select-option v-for='options in formList[index].eqpList' :key='options.name' :value='options.name'>
{{ options.name }}
</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :span="6">
<a-form-item label="對比項" :labelCol="{span: 7}" :wrapperCol="{span: 17}">
<a-select
placeholder="請選擇對比項"
v-decorator="[`dataCode[${index}]`,{initialValue: formList[index] ? formList[index].dataCode : '',rules: [{ required: true, message: '請選擇對比項!' }]}]">
<a-select-option v-for='option in formList[index].dataTypeList' :key='option.name' :value='option.name'>
{{ option.name }}
</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :span="6">
<a-form-item label="備注" :labelCol="{span: 6}" :wrapperCol="{span: 18}">
<a-input v-decorator="[`remark[${index}]`]" placeholder="請輸入備注"></a-input>
</a-form-item>
</a-col>
<a-col :span="2" style="padding-left: 0px">
<a-form-item :labelCol="{span: 0}" :wrapperCol="{span: 24}">
<template v-if="formList.length > 1">
<a-icon type="delete" @click="removeRow(index)"/>
</template>
</a-form-item>
</a-col>
</a-row>
</div>
</div>
</a-form>
<a-form :form="dateForm" inline @keyup.enter.native='searchQuery'>
<a-form-item label='查詢?nèi)掌? :labelCol="{span: 8}" :wrapperCol="{span: 16}"
style="display: inline-block;width: 300px;">
<a-date-picker
style="width: 200px;"
class='query-group-cust'
v-decorator="['startTime', { rules: [{ required: true, message: '請選擇開始時間!' }] }]"
:disabled-date='disabledStartDate'
format='YYYY-MM-DD'
placeholder='請選擇開始時間'
@change='handleStart($event)'
@openChange='handleStartOpenChange'></a-date-picker>
</a-form-item>
<span :style="{ display: 'inline-block', width: '24px', textAlign: 'center' }">-</span>
<a-form-item style="display: inline-block;width: 200px;">
<a-date-picker
style="width: 200px;"
class='query-group-cust'
v-decorator="['endTime', { rules: [{ required: true, message: '請選擇結(jié)束時間!' }] }]"
:disabled-date='disabledEndDate'
format='YYYY-MM-DD'
placeholder='請選擇結(jié)束時間'
@change='handleEnd($event)'
:open='endOpen'
@openChange='handleEndOpenChange'></a-date-picker>
</a-form-item>
<span style="margin-left: 10px">
<a-button type='primary' :disabled='loading' @click='searchQuery' icon='search'>查詢</a-button>
<a-button type='primary' @click='searchReset' icon='search' style='margin-left:10px'>重置</a-button>
<a-button type="primary" icon="plus" @click="addRow" style='margin-left:10px'>新增查詢條件</a-button>
</span>
</a-form>
<p>查詢條件為:{{searchData}}</p>
js
initForm() {
// 首先請求設(shè)備列表,存放在eqpList中
// 初始化form表單
this.formList.push({
equipment: '',
dataCode: '',
remark: '',
eqpList: this.eqpList,
dataTypeList: []
})
},
// 刪除一行
handleRemove(index) {
if (this.formList.length === 1) {
return
}
this.formList.splice(index, 1)
},
// 新增一行
handleAdd() {
this.formList.push({
equipment: '',
dataCode: '',
remark: '',
eqpList: this.eqpList, // 可以根據(jù)接口動態(tài)獲取,這里便于演示,直接賦值了
dataTypeList: [],// 可以根據(jù)接口動態(tài)獲取并根據(jù)設(shè)備去關(guān)聯(lián)
})
},
equipChange(value, index) {
// change賦值
this.formList[index].equipment = value;
//同步更新 當(dāng)前選擇的設(shè)備對應(yīng)的對比項列表
this.handleEqpIdentity(value, index)
},
// 根據(jù)設(shè)備查詢對應(yīng)的對比項列表
handleEqpIdentity(value, index) {
this.dataTypeList = []; //清空dataTypeList
this.formList[index].dataTypeList = []; // 清空當(dāng)前行的 dataTypeList
//根據(jù)新的設(shè)備名稱 獲取對應(yīng)的對比項列表
getAction(this.url.getDataTypeList, {equipment: value})
.then((res) => {
if (res.success) {
this.dataTypeList = res.result;
this.formList[index].dataTypeList = this.dataTypeList;
// this.formList[index].dataCode = ''; 直接賦值為空 是無效的
//需使用 getFieldValue, setFieldsValue
let dataCode1Arr = this.form.getFieldValue('dataCode');
if (dataCode1Arr.length !== 0) {
dataCode1Arr[index] = ''
}
this.form.setFieldsValue({dataCode: dataCode1Arr})
} else {
this.$message.warning(res.message)
}
})
.catch(() => {
this.$message.error('獲取失敗,請重試!')
})
},
// 點擊查詢
searchQuery() {
// 先驗證循環(huán)表單
const {form: {validateFields}} = this
validateFields((error, values) => {
if (!error) {
this.dateForm.validateFields((dateErr, dateValues) => {
//再驗證日期搜索表單
dateValues.startTime = moment(dateValues.startTime).format('YYYY-MM-DD')
dateValues.endTime = moment(dateValues.endTime).format('YYYY-MM-DD')
if (!dateErr) {
this.loading = true;
let formData = Object.assign({}, dateValues);
//整理成后臺所需的數(shù)據(jù)結(jié)構(gòu)
// 循環(huán)表單
let searchArr = [];
(values[`equipment`]).forEach((item, index) => {
const obj = {
equipment: item,
remark: values[`remark`][index],
dataCode: values[`dataCode`][index]
}
searchArr.push(obj);
})
// 日期表單
if (!dateValues.startTime) {
formData.startTime = moment(new Date()).format('YYYY-MM-DD')
}
formData.eqpInfoParamVoList = searchArr;
this.searchData = JSON.parse(formData)
// 請求接口
}
})
}
})
},
到此這篇關(guān)于antd vue實現(xiàn)動態(tài)驗證循環(huán)屬性表單的文章就介紹到這了,更多相關(guān)antd vue動態(tài)驗證循環(huán)屬性表單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中如何實現(xiàn)后臺管理系統(tǒng)的權(quán)限控制的方法示例
這篇文章主要介紹了vue中如何實現(xiàn)后臺管理系統(tǒng)的權(quán)限控制的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
vue利用openlayers實現(xiàn)動態(tài)軌跡
這篇文章主要為大家介紹了vue利用openlayers實現(xiàn)動態(tài)軌跡,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用
這篇文章主要介紹了關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用,我們將某段代碼封裝成一個組件,而這個組件又在另一個組件中引入,而引入該封裝的組件的文件叫做父組件,被引入的組件叫做子組件,需要的朋友可以參考下2023-05-05
Vue2過渡標(biāo)簽transition使用動畫方式
這篇文章主要介紹了Vue2過渡標(biāo)簽transition使用動畫方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Vue.js實現(xiàn)文件上傳壓縮優(yōu)化處理技巧
這篇文章主要介紹了Vue.js實現(xiàn)文件上傳壓縮優(yōu)化處理,本文給大家介紹兩種方法一種是借助canvas的封裝的文件壓縮上傳,二是使用compressorjs第三方插件實現(xiàn),本文給大家介紹的非常詳細需要的朋友可以參考下2022-11-11
Vue3表單組件el-form校驗規(guī)則rules屬性示例詳解
在el-form中正確使用rules校驗是非常重要的,rules使用不當(dāng)容易出現(xiàn)規(guī)則不生效、規(guī)則一直被觸發(fā)等各種現(xiàn)象,這篇文章主要給大家介紹了關(guān)于Vue3表單組件el-form校驗規(guī)則rules屬性的相關(guān)資料,需要的朋友可以參考下2024-08-08

