element的el-form和el-table嵌套使用實現(xiàn)
更新時間:2022年08月12日 10:02:16 作者:ddx2019
本文主要介紹了element的el-form和el-table嵌套使用實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
一、element的el-form和el-table嵌套使用
要點:
:model="addJsonForm"
給表單綁定數(shù)據(jù),addJsonForm
是傳入表單的數(shù)據(jù)對象- 注意表單數(shù)據(jù)對象
addJsonForm
的定義:屬性params
(可按需求命名)為表單內嵌套的表格的顯示數(shù)據(jù),數(shù)組類型; 屬性addJsonRules
,為表單綁定的驗證規(guī)則。 el-table
: 采用自定義列模板,可自定義表頭,el-form
: 表單項綁定對應的字段名和校驗規(guī)則:prop="'params.' + scope.$index + '.name'"
綁定傳入Form 組件的 model 中對應的字段name
:rules="addJsonForm.addJsonRules.name"
綁定表單驗證規(guī)則
<template> <div> <el-form :model="addJsonForm" ref="addJsonForm" :rules="addJsonForm.addJsonRules" :inline="true" label-width="108px" > <el-table :data="addJsonForm.params" style="width: 100%" border> <el-table-column type="selection" width="55" align="center"> </el-table-column> <el-table-column align="center"> <template slot="header" slot-scope="scope"> <span style="color:#2d65dc;">成員名稱</span> <i style="color:#F56C6C;">*</i> </template> <template slot-scope="scope"> <el-form-item :prop="'params.' + scope.$index + '.name'" :rules="addJsonForm.addJsonRules.name" > <el-input type="text" v-model="scope.row.name" autocomplete="off" ></el-input> </el-form-item> </template> </el-table-column> <el-table-column align="center"> <template slot="header" slot-scope="scope"> <span style="color:#2d65dc;">成員值</span> <i style="color:#F56C6C;">*</i> </template> <template slot-scope="scope"> <el-form-item :prop="'params.' + scope.$index + '.value'" :rules="addJsonForm.addJsonRules.value" > <el-input type="text" v-model="scope.row.value" autocomplete="off" ></el-input> </el-form-item> </template> </el-table-column> </el-table> </el-form> </div> </template> <script> export default { data() { return { addJsonForm: { params: [ { name: "", value: "" } ], addJsonRules: { name: [ { required: true, message: "請輸入成員名稱", trigger: "blur" } ], value: [ { required: true, message: "成員值不能為空", trigger: "blur" } ] } } }; } }; </script>
二、應用實例
點擊添加的時候,動態(tài)增加表格的行數(shù),點擊刪除的時候,刪除表格的行數(shù)據(jù)。
近期更新
: 因評論區(qū)問到后續(xù)如何用 Form 表單的 resetFields 方法,這里就新加一個重置功能。
<template> <div> <el-button @click="showPopup">點擊顯示彈框</el-button> <h3> dataSourceJson: <span>{{ FormInAddPopup.dataSourceJson }}</span> </h3> <el-dialog class="comn_dialog" title="添加數(shù)據(jù)" :visible.sync="addJsonVisible" width="415px" top="23vh" > <el-button @click="addTableItem">添加</el-button> <el-button @click="delTableItem">刪除</el-button> /* 近期更新 */ <el-button @click="resetForm('myForm')">重置</el-button> <el-form :model="addJsonForm" ref="addJsonForm" :rules="addJsonForm.addJsonRules" :inline="true" label-width="108px" > <el-table :data="addJsonForm.params" style="width: 100%" border @selection-change="addJsonSelectionChange" > <el-table-column type="selection" width="55" align="center"> </el-table-column> <el-table-column align="center"> <template slot="header" slot-scope="scope"> <span style="color:#2d65dc;">成員名稱</span> <i style="color:#F56C6C;">*</i> </template> <template slot-scope="scope"> <el-form-item :prop="'params.' + scope.$index + '.name'" :rules="addJsonForm.addJsonRules.name" > <el-input type="text" v-model="scope.row.name" autocomplete="off" ></el-input> </el-form-item> </template> </el-table-column> <el-table-column align="center"> <template slot="header" slot-scope="scope"> <span style="color:#2d65dc;">成員值</span> <i style="color:#F56C6C;">*</i> </template> <template slot-scope="scope"> <el-form-item :prop="'params.' + scope.$index + '.value'" :rules="addJsonForm.addJsonRules.value" > <el-input type="text" v-model="scope.row.value" autocomplete="off" ></el-input> </el-form-item> </template> </el-table-column> </el-table> </el-form> <span slot="footer" class="dialog-footer"> <el-button @click="resetAddJsonPopup">取 消</el-button> <el-button type="primary" @click="submitAddJsonPopup">確定</el-button> </span> </el-dialog> </div> </template> <script> export default { data() { return { addJsonVisible: false, addJsonMultiple: [], FormInAddPopup: { dataSourceJson: "" // 獲取到的dataJson,顯示為 [{name:"",value:""},{name:"",value:""}] 的格式 }, tabItemId: 1, // 表格數(shù)據(jù)的 id addJsonForm: { params: [ { name: "", value: "", tabItemId: 1 // 彈框中,刪除空行的唯一標志,默認從1開始 } ], addJsonRules: { name: [ { required: true, message: "請輸入成員名稱", trigger: "blur" } ], value: [ { required: true, message: "成員值不能為空", trigger: "blur" } ] } } }; }, methods: { // 近期更新 resetForm(formName) { this.$refs[formName].resetFields(); }, RndNum(n) { // 生成隨機數(shù) let rdmNum = ""; for (let i = 0; i < n; i++) { rdmNum += Math.floor(Math.random() * 10); // [0,10)的整數(shù) } return rdmNum; }, showPopup() { this.addJsonVisible = true; }, addJsonSelectionChange(val) { this.addJsonMultiple = val; }, resetAddJsonPopup() { //關閉 固定值彈窗 this.$set(this.addJsonForm, "params", []); this.addJsonVisible = false; }, submitAddJsonPopup() { //保存 固定值 if (this.addJsonMultiple.length > 0) { this.$refs["addJsonForm"].validate(valid => { if (valid) { let result = []; this.addJsonMultiple.map(val => { this.$delete(val, "tabItemId"); // 刪除tabItemId屬性 result.push(val); }); result.length ? (result = JSON.stringify(result)) : (result = ""); this.FormInAddPopup.dataSourceJson = result; this.addJsonVisible = false; } else { return false; } }); } else { this.$message.warning("請選擇要保存的數(shù)據(jù)"); } }, addTableItem() { this.tabItemId = "T" + this.RndNum(6); //生成以T開頭的七位隨機數(shù) this.addJsonForm.params.push({ name: "", value: "", tabItemId: this.tabItemId }); }, delTableItem() { // 確認刪除 if (this.addJsonMultiple.length > 0) { let arrs = []; let ids = this.addJsonMultiple.map(val => val.tabItemId); //拿到選中的數(shù)據(jù)id, this.addJsonForm.params.forEach(item => { if (!ids.includes(item.tabItemId)) { // 當id在params中,表示數(shù)據(jù)被選中,該將其刪除,即將不被選中的保留 arrs.push(item); } }); this.addJsonForm.params = arrs; } else { this.$message.warning("請選擇要刪除的數(shù)據(jù)"); } } } }; </script>
到此這篇關于element的el-form和el-table嵌套使用實現(xiàn)的文章就介紹到這了,更多相關element el-form和el-table嵌套內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解axios 全攻略之基本介紹與使用(GET 與 POST)
本篇文章主要介紹了axios 全攻略之基本介紹與使用(GET 與 POST),詳細的介紹了axios的安裝和使用,還有 GET 與 POST方法,有興趣的可以了解一下2017-09-09