【Vue】vue動態添加表單項


一、需求背景

有一個接口需要批量修改入參,但是不固定多少個入參,不固定是哪一個。就得做一個動態添加表單項,如下面的格式params里面,一個key,一個value

參數形式如下面params,忽略pvaId。

{"pvaId":9,"params":[{"paramName":"1","paramValue":"1"},{"paramName":"2","paramValue":"2"},{"paramName":"3","paramValue":"3"},{"paramName":"4","paramValue":"4"}]}

二、實現效果

一組值是一個參數名,一個參數值,

可動態添加一組,可刪除一組。

 

 

三、實現過程

3.1、html

固定打開彈窗,會顯示一組參數名1,參數值1;

點擊添加參數后,會在下面動態生成參數名2,參數值2.......;

點擊一組參數后面的刪除按鈕,會動態刪除該組參數,下面的下標自動向上替代刪除的一個

 

 1 <!-- 批量修改參數的對話框 -->
 2     <el-dialog :visible.sync="showModifyParametersVisible" title="批量修改參數" center customClass="customWidth" :before-close="closeDialog">
 3       <el-form ref="modifyParametersForm" :inline="true" :model="modifyParametersForm" >
 4         <el-form-item label='參數名1' prop="params[0].paramName" :rules="{
 5                     required: true, message: '參數名不能為空', trigger: 'blur'
 6                 }">
 7           <el-input v-model="modifyParametersForm.params[0].paramName"></el-input>
 8         </el-form-item>
 9         <el-form-item label='參數值1' prop="params[0].paramValue" :rules="{
10                     required: true, message: '參數值不能為空', trigger: 'blur'
11                 }">
12           <el-input v-model="modifyParametersForm.params[0].paramValue"></el-input>
13         </el-form-item>
14         <el-form-item>
15           <i class="el-icon-delete" style="visibility:hidden;"></i>
16         </el-form-item>
17         <!-- 動態增加項目 -->
18         
19         <div v-for="(item, index) in modifyParametersForm.params" v-if="index>=1" :key="index">
20           <el-form-item :label="'參數名' + (index+1)" :prop="'params.' + index + '.paramName'" :rules="{
21                     required: true, message: '參數名不能為空', trigger: 'blur'
22                 }">
23 
24             <el-input v-model="item.paramName"></el-input>
25           </el-form-item>
26           <el-form-item :label="'參數值' + (index+1)" :prop="'params.' + index + '.paramValue'" :rules="{
27                     required: true, message: '參數值不能為空', trigger: 'blur'
28                 }">
29             <el-input v-model="item.paramValue"></el-input>
30           </el-form-item>
31 
32           <el-form-item>
33             <i class="el-icon-delete" @click="deleteItem(item, index)"></i>
34           </el-form-item>
35         </div>
36       </el-form>
37       <div slot="footer" class="dialog-footer">
38         <el-button type="primary" @click="submitParameForm()">提交</el-button>
39         <el-button @click="addItem">添加參數</el-button>
40       </div>
41     </el-dialog>

 

3.2、data

 1 // 批量修改入參表單
 2       modifyParametersForm: {
 3         pvaId: '',
 4         params: [
 5           {
 6             "paramName": '',
 7             "paramValue": ''
 8           }
 9         ]
10       },

 

3.3、JS

 1 // 批量修改參數相關接口
 2     // 添加一組key/value輸入框
 3     addItem() {
 4       this.modifyParametersForm.params.push({
 5         paramName: '',
 6         paramValue: ''
 7       })
 8     },
 9     // 刪除一組輸入框
10     deleteItem(item, index) {
11       this.modifyParametersForm.params.splice(index, 1)
12     },
13     // 關閉批量修改參數表單
14     closeDialog(){
15       this.showModifyParametersVisible = false
16       this.modifyParametersForm = {
17         pvaId: '',
18         params: [
19           {
20             "paramName": '',
21             "paramValue": ''
22           }
23         ]
24       }
25     },
26     // 提交批量修改入參表單
27     submitParameForm() {
28       this.$refs.modifyParametersForm.validate((valid) => {
29         if (valid) {
30 31           updateParams(this.modifyParametersForm).then(response => {
32             console.log(response.status)
33             if (response.status === 1) {
34               console.log('success!!')
35               this.$message.success('修改入參成功!')
36               // 提交后關閉彈窗,清空數據
37               this.$nextTick(() => {
38                 this.closeDialog()
39               })
40 
41             } else {
42               console.log('failed')
43               this.$message.error(response.message)
44             }
45           })
46         } else {
47           console.log('error submit!!')
48           return false
49         }
50       })
51     },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM