form表单动态新增文本框

213 阅读1分钟
               <el-dialog

                      :title="title"

                      :visible.sync="open"

                      width="1000px"

                      append-to-body

                    >

                      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

                        <el-row

                          :gutter="20"

                          v-for="(item, index) in rulesList"

                          :key="index"

                        >

                          <el-col :span="12">

                            <el-form-item label="字段":prop="'rulesList.' + index +                                         '.field'"

              :rules="[

                { required: true, message: '字段不能为空', trigger: 'blur' },

              ]">

                              <el-input v-model="item.field"></el-input>

                            </el-form-item>

                          </el-col>

                          <el-col :span="12">

                            <el-form-item label="规则" :prop="item.rule">

                              <el-select v-model="item.rule">

                                <el-option

                                  v-for="(items, r) in queryTypeOptions"

                                  :key="r"

                                  :label="items.dictLabel"

                                  :value="items.dictValue"

                                >

                                </el-option>

                              </el-select> </el-form-item

                          ></el-col>

                          <el-col :span="12">

                            <el-form-item>

                              <el-button

                                v-if="index + 1 == rulesList.length"

                                @click="addItem"

                                type="primary"

                                >增加</el-button

                              >

                              <el-button

                                v-if="index !== 0"

                                @click="deleteItem(item, index)"

                                type="danger"

                                >删除</el-button

                              >

                            </el-form-item>

                          </el-col>

                        </el-row>

                       
                      </el-form>

                      <div slot="footer" class="dialog-footer">

                        <el-button type="primary" @click="submitForm">确 定</el-button>

                        <el-button @click="cancel">取 消</el-button>

                      </div>

                    </el-dialog>
                    
                    
                    
                    rulesList: [

                    {

                      field: "",

                      rule: "",

                    },

                  ],
                  
                  
                  /* 新增字段名和规则框 */

                    addItem() {

                      this.rulesList.push({

                        field: "",

                        rule: "",

                      });

                    },

                    /*  删除字段名和规则框  */

                    deleteItem(item, index) {

                      this.rulesList.splice(index, 1);

                      console.log(this.rulesList, "删除");

                    },
                    
                    
                    submitForm() {

                  this.$refs["form"].validate((valid) => {

                    if (valid) {
提交的时候把传给后端需要的字段
                      this.form.rules = this.rulesList;

                      if (this.form.id != null) {

                        updateRule(this.form).then((response) => {

                          this.$modal.msgSuccess("修改成功");

                          this.open = false;

                          this.getList();

                        });

                      } else {

                        addRule(this.form).then((response) => {

                          this.$modal.msgSuccess("新增成功");

                          this.open = false;

                          this.getList();

                        });

                      }

                    }

                  });

                },