当前位置:   article > 正文

element ui form循环嵌套表单 及嵌套表单的验证方法(校验)_两个elform 嵌套

两个elform 嵌套

首先看看成果

表单一级、二级嵌套的增加、删除

在这里插入图片描述

一级、二级嵌套 的 表单的验证

在这里插入图片描述

下图添加了样式区分一级二级嵌套,与上图是一样的

请添加图片描述

知识点

一级嵌套
循环:<div v-for="(item, index) in form.producte" :key="index">
prop绑定::prop="`producte[${index}].name`"
规则绑定::rules="{
                      required: true,
                      message: '基本名称不能为空',
                      trigger: 'blur',
                    }"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
二级嵌套
循环:<div v-for="(item1, index1) in item.rules" :key="index1"> //item为上面一级中的item,下面的producte同理
prop绑定::prop="`producte[${index}].rules[${index1}].ruleName`"                      
规则绑定::rules="{
                   required: true,
                   message: '域名不能为空',
                   trigger: 'blur',
                   }"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

代码展示

直接复制到你的 vue 中就可以展示和进行测试
有问题的话可以提问(数据都是随便写的)
<template>
  <div class="form">
    <el-row>
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>卡片名称</span>
            <!-- 表单 -->
            <el-form
              ref="form"
              :model="form"
              :rules="rules"
              label-width="80px"
              label-position="top"
              size="mini"
            >
              <el-row>
                <el-col :span="12">
                  <el-form-item label="活动名称" prop="baseInfo.username">
                    <el-input
                      v-model="form.baseInfo.username"
                      style="width: 200px"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="活动密码" prop="baseInfo.pasword">
                    <el-input
                      v-model="form.baseInfo.pasword"
                      style="width: 200px"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <div v-for="(item, index) in form.producte" :key="index">
                <el-divider content-position="left"></el-divider>
                <el-row>
                  <el-form-item
                    label="基本名称"
                    :prop="`producte[${index}].name`"
                    :rules="{
                      required: true,
                      message: '基本名称不能为空',
                      trigger: 'blur',
                    }"
                  >
                    <el-input
                      v-model="item.name"
                      style="width: 200px"
                    ></el-input>
                  </el-form-item>
                </el-row>
                <div v-for="(item1, index1) in item.rules" :key="index1">
                  <el-row>
                    <el-col :span="5">
                      <el-form-item
                        label="规格"
                        :prop="`producte[${index}].rules[${index1}].ruleName`"
                        :rules="{
                          required: true,
                          message: '域名不能为空',
                          trigger: 'blur',
                        }"
                      >
                        <el-input
                          v-model="item1.ruleName"
                          style="width: 100px"
                        ></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-col :span="10">
                        <el-form-item
                          label="价格"
                          :prop="`producte[${index}].rules[${index1}].isFixed`"
                          :rules="{
                            required: true,
                            message: '价格不能为空',
                            trigger: 'change',
                          }"
                        >
                          <el-select
                            v-model="item1.isFixed"
                            placeholder="请选择"
                          >
                            <el-option
                              v-for="item1 in options"
                              :key="item1.value"
                              :label="item1.label"
                              :value="item1.value"
                            >
                            </el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="10">
                        <el-form-item
                          :prop="`producte[${index}].rules[${index1}].price`"
                          :rules="{
                            required: true,
                            message: '价格不能为空',
                            trigger: 'blur',
                          }"
                        >
                          <div style="height: 38px"></div>
                          <el-input v-model="item1.price">
                            <template slot="append"></template>
                          </el-input>
                        </el-form-item></el-col
                      >
                    </el-col>
                    <el-col :span="4">
                      <el-form-item
                        :prop="`producte[${index}].rules[${index1}].other`"
                        :rules="{
                          required: true,
                          message: '其他不能为空',
                          trigger: 'blur',
                        }"
                      >
                        <div style="height: 38px"></div>
                        <el-input v-model="item1.other">
                          <template slot="append"></template>
                        </el-input>
                      </el-form-item></el-col
                    >
                    <el-col :span="5">
                      <div style="height: 38px"></div>
                      <el-button
                        type="warning"
                        size="mini"
                        @click="addRule(index, index1)"
                        v-if="index1 == 0"
                        >添加规则</el-button
                      >
                      <el-button
                        type="warning"
                        size="mini"
                        @click="deleteRule(index, index1)"
                        v-else
                        >删除规则</el-button
                      >
                    </el-col>
                  </el-row>
                </div>
                <el-row>
                  <el-button
                    size="mini"
                    @click="createTable(index)"
                    v-if="index == form.producte.length - 1"
                    >创建新表</el-button
                  >
                  <el-button size="mini" @click="deleteTable(index)" v-else
                    >删除表</el-button
                  >
                </el-row>
                <el-divider content-position="left"></el-divider>
              </div>
              <el-form-item>
                <el-button type="primary" @click="submitForm('form')"
                  >立即创建</el-button
                >
                <el-button @click="resetForm('form')">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        {
          value: 1,
          label: "固定",
        },
        {
          value: 0,
          label: "非固定",
        },
      ],
      value: "",
      form: {
        baseInfo: {
          username: "",
          pasword: "",
        },
        producte: [
          {
            name: "",
            rules: [
              {
                ruleName: null,
                isFixed: null,
                price: null,
                other: null,
              },
            ],
          },
        ],
      },
      rules: {
        "baseInfo.username": [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        "baseInfo.pasword": [
          { required: true, message: "请选择活动区域", trigger: "blur" },
        ],
        date1: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        date2: [
          {
            type: "date",
            required: true,
            message: "请选择时间",
            trigger: "change",
          },
        ],
        type: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个活动性质",
            trigger: "change",
          },
        ],
        resource: [
          { required: true, message: "请选择活动资源", trigger: "change" },
        ],
        desc: [{ required: true, message: "请填写活动形式", trigger: "blur" }],
      },
    };
  },
  methods: {
    //删除规则
    deleteRule(index, index1) {
      this.form.producte[index].rules.splice(index1, 1);
    },
    //添加规则
    addRule(index, index1) {
      console.log(index, index1);
      let producteItem = {
        name: "",
        ruleName: null,
        isFixed: null,
        price: null,
        other: null,
      };
      this.form.producte[index]["rules"].push(producteItem);
    },

    //创建新表
    createTable(index) {
      let newTable = {
        name: "",
        rules: [
          {
            ruleName: null,
            isFixed: null,
            price: null,
            other: null,
          },
        ],
      };
      this.form.producte.push(newTable);
    },
    //删除信表
    deleteTable(index) {
      this.form.producte.splice(index, 1);
    },
    //提交表单验证
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/114869
推荐阅读
相关标签