当前位置:   article > 正文

antd vue实现动态验证循环属性表单_vue 循环出来的数据怎么做必填

vue 循环出来的数据怎么做必填

希望实现查询表单的某些属性可以循环验证必填项:

需求:

1.名称,对比项,备注必填,默认为一行,可增加多行

2.根据名称,动态请求对比项列表,名称变化时,清空该行当前选择的对比项

思路:将整个搜索分成了两个表单,分别去做验证。一个是可动态添加的循环表单form,另一个为普通表单dateForm

html

  1. <a-form :form="form" @keyup.enter.native='searchQuery'>
  2. <div class="dynamic-wrap">
  3. <div v-for="(item,index) in formList" :key="index">
  4. <a-row :gutter="24">
  5. <a-col :span="6">
  6. <a-form-item label="名称" :labelCol="{span: 7}" :wrapperCol="{span: 17}">
  7. <a-select placeholder='请选择名称'
  8. v-decorator="[`equipment[${index}]`,{ initialValue: formList[index] ? formList[index].equipment : '', rules: [{ required: true, message: '请选择设备!' }]}]"
  9. @change="(e)=>equipChange(e,index)">
  10. <a-select-option v-for='options in formList[index].eqpList' :key='options.name' :value='options.name'>
  11. {{ options.name }}
  12. </a-select-option>
  13. </a-select>
  14. </a-form-item>
  15. </a-col>
  16. <a-col :span="6">
  17. <a-form-item label="对比项" :labelCol="{span: 7}" :wrapperCol="{span: 17}">
  18. <a-select
  19. placeholder="请选择对比项"
  20. v-decorator="[`dataCode[${index}]`,{initialValue: formList[index] ? formList[index].dataCode : '',rules: [{ required: true, message: '请选择对比项!' }]}]">
  21. <a-select-option v-for='option in formList[index].dataTypeList' :key='option.name' :value='option.name'>
  22. {{ option.name }}
  23. </a-select-option>
  24. </a-select>
  25. </a-form-item>
  26. </a-col>
  27. <a-col :span="6">
  28. <a-form-item label="备注" :labelCol="{span: 6}" :wrapperCol="{span: 18}">
  29. <a-input v-decorator="[`remark[${index}]`]" placeholder="请输入备注"></a-input>
  30. </a-form-item>
  31. </a-col>
  32. <a-col :span="2" style="padding-left: 0px">
  33. <a-form-item :labelCol="{span: 0}" :wrapperCol="{span: 24}">
  34. <template v-if="formList.length > 1">
  35. <a-icon type="delete" @click="removeRow(index)"/>
  36. </template>
  37. </a-form-item>
  38. </a-col>
  39. </a-row>
  40. </div>
  41. </div>
  42. </a-form>
  43. <a-form :form="dateForm" inline @keyup.enter.native='searchQuery'>
  44. <a-form-item label='查询日期' :labelCol="{span: 8}" :wrapperCol="{span: 16}"
  45. style="display: inline-block;width: 300px;">
  46. <a-date-picker
  47. style="width: 200px;"
  48. class='query-group-cust'
  49. v-decorator="['startTime', { rules: [{ required: true, message: '请选择开始时间!' }] }]"
  50. :disabled-date='disabledStartDate'
  51. format='YYYY-MM-DD'
  52. placeholder='请选择开始时间'
  53. @change='handleStart($event)'
  54. @openChange='handleStartOpenChange'></a-date-picker>
  55. </a-form-item>
  56. <span :style="{ display: 'inline-block', width: '24px', textAlign: 'center' }">-</span>
  57. <a-form-item style="display: inline-block;width: 200px;">
  58. <a-date-picker
  59. style="width: 200px;"
  60. class='query-group-cust'
  61. v-decorator="['endTime', { rules: [{ required: true, message: '请选择结束时间!' }] }]"
  62. :disabled-date='disabledEndDate'
  63. format='YYYY-MM-DD'
  64. placeholder='请选择结束时间'
  65. @change='handleEnd($event)'
  66. :open='endOpen'
  67. @openChange='handleEndOpenChange'></a-date-picker>
  68. </a-form-item>
  69. <span style="margin-left: 10px">
  70. <a-button type='primary' :disabled='loading' @click='searchQuery' icon='search'>查询</a-button>
  71. <a-button type='primary' @click='searchReset' icon='search' style='margin-left:10px'>重置</a-button>
  72. <a-button type="primary" icon="plus" @click="addRow" style='margin-left:10px'>新增查询条件</a-button>
  73. </span>
  74. </a-form>
  75. <p>查询条件为:{{searchData}}</p>

js

  1. initForm() {
  2. // 首先请求设备列表,存放在eqpList中
  3. // 初始化form表单
  4. this.formList.push({
  5. equipment: '',
  6. dataCode: '',
  7. remark: '',
  8. eqpList: this.eqpList,
  9. dataTypeList: []
  10. })
  11. },
  12. // 删除一行
  13. handleRemove(index) {
  14. if (this.formList.length === 1) {
  15. return
  16. }
  17. this.formList.splice(index, 1)
  18. },
  19. // 新增一行
  20. handleAdd() {
  21. this.formList.push({
  22. equipment: '',
  23. dataCode: '',
  24. remark: '',
  25. eqpList: this.eqpList, // 可以根据接口动态获取,这里便于演示,直接赋值了
  26. dataTypeList: [],// 可以根据接口动态获取并根据设备去关联
  27. })
  28. },
  29. equipChange(value, index) {
  30. // change赋值
  31. this.formList[index].equipment = value;
  32. //同步更新 当前选择的设备对应的对比项列表
  33. this.handleEqpIdentity(value, index)
  34. },
  35. // 根据设备查询对应的对比项列表
  36. handleEqpIdentity(value, index) {
  37. this.dataTypeList = []; //清空dataTypeList
  38. this.formList[index].dataTypeList = []; // 清空当前行的 dataTypeList
  39. //根据新的设备名称 获取对应的对比项列表
  40. getAction(this.url.getDataTypeList, {equipment: value})
  41. .then((res) => {
  42. if (res.success) {
  43. this.dataTypeList = res.result;
  44. this.formList[index].dataTypeList = this.dataTypeList;
  45. // this.formList[index].dataCode = ''; 直接赋值为空 是无效的
  46. //需使用 getFieldValue, setFieldsValue
  47. let dataCode1Arr = this.form.getFieldValue('dataCode');
  48. if (dataCode1Arr.length !== 0) {
  49. dataCode1Arr[index] = ''
  50. }
  51. this.form.setFieldsValue({dataCode: dataCode1Arr})
  52. } else {
  53. this.$message.warning(res.message)
  54. }
  55. })
  56. .catch(() => {
  57. this.$message.error('获取失败,请重试!')
  58. })
  59. },
  60. // 点击查询
  61. searchQuery() {
  62. // 先验证循环表单
  63. const {form: {validateFields}} = this
  64. validateFields((error, values) => {
  65. if (!error) {
  66. this.dateForm.validateFields((dateErr, dateValues) => {
  67. //再验证日期搜索表单
  68. dateValues.startTime = moment(dateValues.startTime).format('YYYY-MM-DD')
  69. dateValues.endTime = moment(dateValues.endTime).format('YYYY-MM-DD')
  70. if (!dateErr) {
  71. this.loading = true;
  72. let formData = Object.assign({}, dateValues);
  73. //整理成后台所需的数据结构
  74. // 循环表单
  75. let searchArr = [];
  76. (values[`equipment`]).forEach((item, index) => {
  77. const obj = {
  78. equipment: item,
  79. remark: values[`remark`][index],
  80. dataCode: values[`dataCode`][index]
  81. }
  82. searchArr.push(obj);
  83. })
  84. // 日期表单
  85. if (!dateValues.startTime) {
  86. formData.startTime = moment(new Date()).format('YYYY-MM-DD')
  87. }
  88. formData.eqpInfoParamVoList = searchArr;
  89. this.searchData = JSON.parse(formData)
  90. // 请求接口
  91. }
  92. })
  93. }
  94. })
  95. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/115587
推荐阅读
相关标签