_el-row v-for">
当前位置:   article > 正文

element v-for循环列表的表单校验(动态绑定的数据)_el-row v-for

el-row v-for

1、一层循环时嵌套:

html:

  1. <el-form :model="addform" ref="addform" :rules="rules">
  2. <el-form-item label="资源" required label-width="120px">
  3. <el-row v-for="(item,i) in addform.resourceList" :key="i">
  4. <el-col :span="10">
  5. <el-form-item :prop="'resourceList.'+ i +'.castType'" :rules="rules.castType">
  6. <el-select v-model="item.castType" placeholder="请选择">
  7. <el-option label="免费" name="type" :value="0"></el-option>
  8. <el-option label="付费" name="type" :value="1"></el-option>
  9. </el-select>
  10. </el-form-item>
  11. </el-col>
  12. </el-form>

js:

  1. data () {
  2. return {
  3. addform: {
  4. resourceList: [{
  5. id:new Date().getTime(), // id不需要 这里的id前端做删除使用了
  6. castType: '',
  7. name: '',
  8. }],
  9. },
  10. rules: {
  11. castType: [{required: true, message: '请选择', trigger: 'change' }],
  12. name: [{required: true, message: '请输入', trigger: 'blur' }],
  13. }
  14. }
  15. },

总结: 

  1. 总结:
  2. //一层嵌套时:
  3. // 1、prop 属性动态绑定,为model对象下的数据,如addform.resourceList为循环数组:
  4. // 则::model="addform",:prop="'resourceList.'+ i +'.castType'"
  5. // 2、在el-form-item 标签上添加 rules 属性,属性值为rules绑定的对象下的值。
  6. // 即::rules="rules.castType"

2.双层或多层循环表单

html代码如下:

  1. <el-form :model="formData" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  2. <template v-for="(item, index) in formData.oneCycle">
  3. <el-form-item label="姓名" :prop="'oneCycle.' + index + 'name'" :rules="{required: true, message: '请输入',trigger: 'blur'}">
  4. <el-input v-model="item.name"></el-input>
  5. </el-form-item>
  6. <template v-for="(itemSon, indexSon) in item.towCycle">
  7. <el-form-item label="性别" :prop="'oneCycle.' + index + '.towCycle.' + indexSon + 'sex'" :rules="{required: true, message: '请输入',trigger: 'blur'}">
  8. <el-input v-model="item.sex"></el-input>
  9. </el-form-item>
  10. </template>
  11. </template>
  12. </el-form>

js:

  1. formData: {
  2. oneCycle: [{
  3. name: '',
  4. towCycle: [
  5. {
  6. sex: ''
  7. },
  8. {
  9. sex: ''
  10. }]
  11. },
  12. {
  13. name: '',
  14. towCycle: [
  15. {
  16. sex: ''
  17. {
  18. sex: ''
  19. }
  20. ]
  21. }
  22. ]
  23. },
  24. rules: {}

总结:

多重循环的原理也是一样,只要值对应上就行

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