• vue element 动态增加表单并进行表单验证


    表单验证:
    需要注意的一点是:

    普通表单验证单项依靠的是prop…而动态生成的表单要用:prop

    书写的语法是:prop="‘moreNotifyObject.’ + index +’.notifyobject’",moreNotifyObject是v-for绑定的数组,index是索引,notifyobject是表单绑定的v-model的名称,然后用.把他们链接起来。

    所以总结起来的语法就是:prop="‘v-for绑定的数组.’ + index + ‘.v-model绑定的变量’"

    还有一个需要注意就是v-for的写法,要将表单的model名写进去

    <div class="moreRulesIn" v-for="(item, index) in addForm.moreNotifyObject" :key="item.key">

    <template>
      <div>
        <el-form ref="form" :model="addForm" label-width="80px">
          <div class="moreRules">
            <div
              class="moreRulesIn"
              v-for="(item, index) in addForm.moreNotifyObject"
              :key="item.key"
            >
              <el-row>
                <el-col :span="6">
                  <el-form-item
                    label="field"
                    :prop="'moreNotifyObject.' + index + '.field'"
                    :rules="[{ required: true, message: '请完善信息' }]"
                  >
                    <el-input
                      v-model="item.field"
                      placeholder="请输入field"
                      class="el-select_box"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    label="name"
                    :prop="'moreNotifyObject.' + index + '.name'"
                    :rules="[{ required: true, message: '请完善信息' }]"
                  >
                    <el-input
                      v-model="item.name"
                      placeholder="请输入name"
                      class="el-select_box"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    label="max"
                    :prop="'moreNotifyObject.' + index + '.max'"
                    :rules="[{ required: true, message: '请完善信息' }]"
                  >
                    <el-input
                      v-model="item.max"
                      placeholder="请输入max"
                      class="el-select_box"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6" align="center">
                  <el-button @click="deleteRules(item, index)">删除</el-button>
                </el-col>
              </el-row>
            </div>
             <el-button @click="addUser">增加</el-button>
             <el-button @click="initData">清空</el-button>
          </div>
        </el-form>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          addForm: {
            name: "",
            content: "",
            moreNotifyObject: [
              {
                field: "",
                name: "",
                max: "",
              },
              {
                field: "",
                name: "",
                max: "",
              },
            ],
          },
        };
      },
      methods: {
        initData() {
          this.addForm.moreNotifyObject = [];
        },
        addUser() {
          this.addForm.moreNotifyObject.push({
            field: "",
            name: "",
            max: "",
          });
        },
        deleteRules(item, index) {
          this.index = this.addForm.moreNotifyObject.indexOf(item);
          if (index !== -1) {
            this.addForm.moreNotifyObject.splice(index, 1);
          }
        },
      },
    };
    </script>
    
    <style lang="scss" scoped>
    </style>
  • 相关阅读:
    IIS6.0服务器架站无法访问解决方案总结
    DNN中做支持多语言的模块
    在dotnetnuke中创建 parent portal
    DNN,Rainbow资源
    2005年岁末,各种主流CMS系统的比较和汇总
    在DNN中获取所有模块信息
    学习dnn的新资源,sooooooooo great!!
    DNN的电子商务模块
    DNN学习笔记
    也学ASP.NET 2.0 AJAX 之二:使用Timer控件
  • 原文地址:https://www.cnblogs.com/7c89/p/14286122.html
Copyright © 2020-2023  润新知