Vue?elementUI表單嵌套表格并對(duì)每行進(jìn)行校驗(yàn)詳解
效果展示
先看看這是不是需要的效果^_^
如圖,ElementUI 表單里嵌套了表格,表格內(nèi)每行能進(jìn)行【保存】【新增】【編輯】【刪除】【重置】等操作,同時(shí)可以對(duì)每行的某些字段進(jìn)行校驗(yàn)(而不是整個(gè)表單校驗(yàn)!),這種需求很常見(jiàn),所以記錄下來(lái)。
代碼鏈接
關(guān)鍵代碼
表格數(shù)據(jù)
// 數(shù)據(jù)格式必須是【對(duì)象嵌套數(shù)組】,【form】綁定表單,【list】綁定表格 form: { // 表格數(shù)據(jù) list: [ { id: 1, name: '小葉', age: '12', phone: '123456', show: true }, { id: 2, name: '小李', age: '23', phone: '123457', show: true }, { id: 3, name: '小林', age: '12', phone: '123458', show: true } ] },
組件嵌套
- 添加字段校驗(yàn)的時(shí)候,格式必須寫成這樣的 :prop="'list.' + scope.$index + '.name'"
這是 elementui 規(guī)定的格式,渲染后的結(jié)果為 list.1.name - 每個(gè)字段要?jiǎng)討B(tài)綁定表單的 rules 屬性
- 如果不是以上的格式,會(huì)出錯(cuò)?。?!
// 表單必須嵌套在表格的外面,表單必須綁定【rules】【ref】屬性 <el-form :model="form" :rules="rules" ref="form"> <el-table :data="form.list"> <el-table-column prop="name" label="姓名"> <template scope="scope"> // 每個(gè)字段動(dòng)態(tài)的綁定表單的【prop】【rules】屬性 <el-form-item :prop="'list.' + scope.$index + '.name'" :rules="rules.name"> <el-input size="mini" v-model="scope.row.name" placeholder="請(qǐng)輸入" clearable></el-input> </el-form-item> </template> </el-table-column> </el-table> </el-form>
校驗(yàn)方法
- 表單的字段對(duì)象存在于 this.$refs['form'].fields 中,并且字段對(duì)象具有 prop【datas.1.name】 屬性和 validateField 方法【驗(yàn)證 datas.1.name 能否通過(guò)校驗(yàn)】
- 但是 validateField 方法需要手動(dòng)創(chuàng)建來(lái)驗(yàn)證能否通過(guò)校驗(yàn)
- 必須創(chuàng)建,否則會(huì)出錯(cuò)!?。?/li>
// 表單校驗(yàn)方法 // 【form】是需要校驗(yàn)的表單,就是表單中【ref】綁定的字段 // 【index】是需要傳入的行數(shù),字段【scope.$index】 validateField(form, index) { let result = true; for (let item of this.$refs[form].fields) { if(item.prop.split(".")[1] == index){ this.$refs[form].validateField(item.prop, err => { if(err !="") { result = false; } }); } if(!result) break; } return result; }
重置方法
// 對(duì)【需要校驗(yàn)】的表單字段進(jìn)行重置 // 參數(shù)同校驗(yàn)方法,如果是全部重置 reset(form, index) { this.$refs[form].fields.forEach(item => { if(item.prop.split(".")[1] == index){ item.resetField(); } }) } // 如果需要全部重置可以直接質(zhì)控表單中字段 // 【row】是每行傳入的數(shù)據(jù) resetRow(row) { row.name = ""; row.age = ""; row.phone = ""; }
完整代碼
因?yàn)橛玫氖窃诰€鏈接,網(wǎng)絡(luò)不穩(wěn)定時(shí)頁(yè)面不一定能加載出來(lái),使用時(shí)可以更換成本地的!
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>vue表單嵌套表格逐行驗(yàn)證</title> <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> <!-- 引入樣式 --> <link rel="stylesheet" rel="external nofollow" > <!-- 引入組件庫(kù) --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> </head> <body> <div id="app"> <!-- 頁(yè)面組件 --> <h2 style="text-align: center; line-height: 23px;color: #909399;">vue表單嵌套表格逐行驗(yàn)證</h2> <el-form :model="form" :rules="rules" ref="form" :inline="true" style="margin: 23px auto 0px; width: 96%; overflow: hidden;"> <el-table border :data="form.list"> <el-table-column align="center" prop="id" label="序號(hào)" width="55"> </el-table-column> <el-table-column align="center" prop="name" label="姓名"> <template scope="scope"> <el-form-item :prop="'list.' + scope.$index + '.name'" :rules="rules.name" v-if="scope.row.show"> <el-input size="mini" v-model="scope.row.name" placeholder="請(qǐng)輸入" clearable> </el-input> </el-form-item> <div v-if="!scope.row.show">{{scope.row.name}}</div> </template> </el-table-column> <el-table-column align="center" prop="age" label="年齡"> <template scope="scope"> <el-form-item :prop="'list.' + scope.$index + '.age'" :rules="rules.age" v-if="scope.row.show"> <el-input size="mini" v-model="scope.row.age" placeholder="請(qǐng)輸入" clearable> </el-input> </el-form-item> <div v-if="!scope.row.show">{{scope.row.age}}</div> </template> </el-table-column> <el-table-column align="center" prop="phone" label="聯(lián)系方式"> <template scope="scope"> <el-form-item :prop="'list.' + scope.$index + '.phone'" :rules="rules.phone" v-if="scope.row.show"> <!-- <el-form-item v-if="scope.row.show"> --> <el-input size="mini" v-model="scope.row.phone" placeholder="請(qǐng)輸入" clearable> </el-input> </el-form-item> <div v-if="!scope.row.show">{{scope.row.phone}}</div> </template> </el-table-column> <el-table-column label="操作" align="center" width="290" fixed="right"> <template slot-scope="scope"> <el-button type="text" style="color: #E6A23C;" @click="save(scope.$index, scope.row)" v-if="scope.row.show" icon="el-icon-check">保存 </el-button> <el-button type="text" style="color: #409EFF;" @click="edit(scope.row)" v-if="!scope.row.show" icon="el-icon-edit">編輯 </el-button> <el-button type="text" style="color: #67C23A;" v-if="scope.$index+1 == listLength" @click="addRow(scope.$index, scope.row)" icon="el-icon-plus">新增 </el-button> <el-button type="text" style="color: #F56C6C;" @click="delRow(scope.$index, scope.row)" icon="el-icon-delete">刪除 </el-button> <el-button type="text" style="color: #909399;" @click="reset('form', scope.$index)" v-if="scope.row.show" icon="el-icon-refresh">重置 </el-button> <!-- <el-button type="text" style="color: #909399;" @click="resetRow(scope.row)" v-if="scope.row.show" icon="el-icon-refresh">重置 </el-button> --> </template> </el-table-column> </el-table> </el-form> </div> </body> </html> <script> var app = new Vue({ el: '#app', data() { return { // 表單數(shù)據(jù) form: { // 表格數(shù)據(jù) list: [{ id: 1, name: '', age: '', phone: '', show: true }] }, // 表單驗(yàn)證規(guī)則 rules: { name: [{ required: true, message: '請(qǐng)輸入姓名!', trigger: 'blur' }], age: [{ required: true, message: '請(qǐng)輸入年齡!', trigger: 'blur' }], phone: [{ required: true, message: '請(qǐng)輸入聯(lián)系方式!', trigger: 'blur' }], }, // 表格長(zhǎng)度默認(rèn)為 1 listLength: 1, } }, methods: { // 校驗(yàn) validateField(form, index) { let result = true; for (let item of this.$refs[form].fields) { if (item.prop.split(".")[1] == index) { this.$refs[form].validateField(item.prop, err => { if (err != "") { result = false; } }); } if (!result) break; } return result; }, // 重置【只針對(duì)校驗(yàn)字段】 reset(form, index) { this.$refs[form].fields.forEach(item => { if (item.prop.split(".")[1] == index) { item.resetField(); } }) }, // 重置【全部】 resetRow(row) { row.name = ""; row.age = ""; row.phone = ""; }, // 保存 save(index, row) { if (!this.validateField('form', index)) return; row.show = false; }, // 新增 addRow(index, row) { if (!this.validateField('form', index)) return; this.form.list.push({ id: index + 2, name: '', age: '', phone: '', show: true }); this.listLength = this.form.list.length; }, // 編輯 edit(row) { row.show = true; }, // 刪除 delRow(index, row) { if (this.form.list.length > 1) { this.form.list.splice(index, 1); this.listLength = this.form.list.length; } else { this.form.list = [{ id: 1, name: '', age: '', phone: '', show: true }]; } }, } }) </script>
總結(jié)
到此這篇關(guān)于Vue elementUI表單嵌套表格并對(duì)每行進(jìn)行校驗(yàn)的文章就介紹到這了,更多相關(guān)elementUI表單嵌套表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- el-element中el-table表格嵌套el-select實(shí)現(xiàn)動(dòng)態(tài)選擇對(duì)應(yīng)值功能
- vue+element表格實(shí)現(xiàn)多層數(shù)據(jù)的嵌套方式
- Element實(shí)現(xiàn)表格嵌套、多個(gè)表格共用一個(gè)表頭的方法
- 關(guān)于ElementPlus中的表單驗(yàn)證規(guī)則詳解
- ElementUI表單驗(yàn)證validate和validateField的使用及區(qū)別
- element表單驗(yàn)證如何清除校驗(yàn)提示語(yǔ)
- 解決vue+ element ui 表單驗(yàn)證有值但驗(yàn)證失敗問(wèn)題
- element 表格嵌套表單驗(yàn)證指定行的操作方法
相關(guān)文章
vue2?自定義?el-radio-button?的樣式并設(shè)置默認(rèn)值的方法
這篇文章主要介紹了vue2?自定義?el-radio-button?的樣式并設(shè)置默認(rèn)值的操作方法,代碼分為html部分和css修改樣式代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10vue中的路由傳值與重調(diào)本路由改變參數(shù)
這篇文章主要介紹了vue中的路由傳值與重調(diào)本路由改變參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09使用vue開(kāi)發(fā)移動(dòng)端管理后臺(tái)的注意事項(xiàng)
這篇文章主要介紹了使用vue開(kāi)發(fā)移動(dòng)端管理后臺(tái)的注意事項(xiàng),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03基于Vue2實(shí)現(xiàn)移動(dòng)端圖片上傳、壓縮、拖拽排序、拖拽刪除功能
這篇文章主要介紹了基于Vue2實(shí)現(xiàn)移動(dòng)端圖片上傳、壓縮、拖拽排序、拖拽刪除功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能
這篇文章主要為大家詳細(xì)介紹了Vue調(diào)用PC攝像頭實(shí)現(xiàn)拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09使用vue-route 的 beforeEach 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由跳轉(zhuǎn)前驗(yàn)證登錄)功能
在網(wǎng)站中普遍會(huì)遇到這樣的需求,路由跳轉(zhuǎn)前做一些驗(yàn)證,比如登錄驗(yàn)證(未登錄去登錄頁(yè))。下面腳本之家小編給大家?guī)?lái)了使用vue-route 的 beforeEach 實(shí)現(xiàn)導(dǎo)航守衛(wèi)(路由跳轉(zhuǎn)前驗(yàn)證登錄)功能,感興趣的朋友一起看看吧2018-03-03vue項(xiàng)目build打包后部分樣式錯(cuò)亂的解決
這篇文章主要介紹了vue項(xiàng)目build打包后部分樣式錯(cuò)亂的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07