element中el-table與el-form同用并校驗(yàn)
基本結(jié)構(gòu)
<template> <div> <el-form ref="form" :model="form"> <el-table ref="table" :data="form.tableData" empty-text='暫無數(shù)據(jù)'> <el-table-column label="姓名"> <template slot-scope="scope"> <el-form-item > <el-input v-model="scope.row.name"></el-input> </el-form-item> </template> </el-table-column> <el-table-column label="年齡"> <template slot-scope="scope"> <el-form-item > <el-input v-model.number="scope.row.age" type="number"></el-input> </el-form-item> </template> </el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button type="danger" size="mini" @click="del(scope.$index)" icon='el-icon-delete'></el-button> <el-button type="primary" size="mini" @click="add" icon='el-icon-plus'></el-button> </template> </el-table-column> </el-table> </el-form> </div> </template> <script> export default { data() { return { form: { tableData: [ { name: "aaa", age: 11 }, { name: "", age: '' } ] } }; }, methods:{ add(){ this.form.tableData.push({ name: "", age: '' }) }, del(index){ this.form.tableData.splice(index,1); } } }; </script>
el-table 數(shù)據(jù)是數(shù)組 , el-form 數(shù)據(jù)是 對(duì)象
添加校驗(yàn)
Form 組件提供了表單驗(yàn)證的功能,只需要通過 rules
屬性傳入約定的驗(yàn)證規(guī)則,并將 Form-Item 的 prop
屬性設(shè)置為需校驗(yàn)的字段名即可。
方法1:
<el-form :rules='rules' ...> <el-form-item :prop=" 'tableData.' + scope.$index + '.name' " :rules='rules.name'>
data(){ return { rules: { name: [ { required: true, message: '名字不能為空', trigger: 'blur' } ] } } }
方法2:
<el-form-item :prop=" 'tableData.' + scope.$index + '.name' " :rules="{ required: true, message: '名字不能為空', trigger: 'blur' }">
<el-table-column label="姓名"> <template slot-scope="scope"> <el-form-item :prop=" 'tableData.' + scope.$index + '.name' " :rules="{ required: true, message: '名字不能為空', trigger: 'blur' }"> <el-input v-model="scope.row.name"></el-input> </el-form-item> </template> </el-table-column>
自己寫校驗(yàn)方法
方法1 : 對(duì)應(yīng)上面的方法1,校驗(yàn)犯法寫在data
中.
data() { var checkAge = (rule, value, callback) => { if (!value) { return callback(new Error('年齡不能為空')); }else if(!Number.isInteger(value) ){ callback(new Error('請(qǐng)輸入數(shù)字值')); }else if(value < 18){ callback(new Error('必須年滿18歲')); }else if(value >50){ callback(new Error('小于50歲')); }else{ callback(); } }; var checkName = (rule, value, callback) => { let regName =/^[\u4e00-\u9fa5]{2,4}$/; if (!value) { return callback(new Error('姓名不能為空')); }else if(!regName.test(value)){ callback(new Error('請(qǐng)輸入正確的姓名')); }else{ callback(); } }; return { form: { tableData: [ { name: "", age: '' } ] }, rules: { name: [ { required: true, trigger: 'blur' ,validator: checkName} ], age: [ { required: true, trigger: 'blur', validator: checkAge} ], } }; },
方法2: 對(duì)應(yīng)上面的方法2,校驗(yàn)犯法寫在methods
中.
<el-form-item :prop=" 'tableData.' + scope.$index + '.name' " :rules=" { required: true, trigger: 'blur' ,validator: checkName}"> <el-form-item :prop=" 'tableData.' + scope.$index + '.age' " :rules=" { required: true, trigger: 'blur' ,validator: checkAge}">
methods:{ checkAge (rule, value, callback) { if (!value) { return callback(new Error('年齡不能為空')); }else if(!Number.isInteger(value) ){ callback(new Error('請(qǐng)輸入數(shù)字值')); }else if(value < 18){ callback(new Error('必須年滿18歲')); }else if(value >50){ callback(new Error('小于50歲')); }else{ callback(); } }, checkName (rule, value, callback) { let regName =/^[\u4e00-\u9fa5]{2,4}$/; if (!value) { return callback(new Error('姓名不能為空')); }else if(!regName.test(value)){ callback(new Error('請(qǐng)輸入正確的姓名')); }else{ callback(); } } }
同table中,某字段不能重復(fù)
8.27 更新
之前寫了一個(gè)同table中,不能寫相同的電話號(hào)碼
的校驗(yàn),但是并不好。修改后如下
<el-table-column label="電話號(hào)碼"> <template slot-scope="scope"> <el-form-item :prop=" 'tableData.' + scope.$index + '.tel' " :rules=" { required: true, trigger: 'blur' ,validator: checkTel}" > <el-input v-model.number="scope.row.tel" type="number" @blur="telequalCheckblur(scope.$index,scope.row.tel)" ></el-input> </el-form-item> </template> </el-table-column>
methods:{ checkTel(rule, value, callback) { let regTel = /^(((1[3456789][0-9]{1})|(15[0-9]{1}))+\d{8})$/; let index = rule.field.split(".")[1]; if (!value) { return callback(new Error("電話號(hào)碼不能為空")); } else if (!regTel.test(value)) { callback(new Error("請(qǐng)輸入正確的電話號(hào)碼")); } else if (this.telequalCheck(index, value)) { callback(new Error("電話號(hào)碼重復(fù),請(qǐng)重新填寫")); } else { callback(); } }, //相等判斷 telequalCheck(index, value) { return this.form.tableData.some(({ tel }, i) => { console.log(i, index, i == index); return i == index ? false : tel === value; }); }, }
9.14更新
必填*顯示
1. 輸入框前加*
正常來說,在el-form中,使用rules的required: true
,就能顯示*
了.
因?yàn)槭窃诒砀裰?所以表單就不使用label
了,所以這時(shí)必填選項(xiàng)的*
沒有了.
注意:這里面label=' '
必須有空格,否則還是沒有*
9.17更新
表頭加*
1.el-table>
添加 header-cell-class-name='required'
參數(shù) | 說明 | 類型 | 可選值 | 默認(rèn)值 |
---|---|---|---|---|
header-cell-class-name | 表頭單元格的 className 的回調(diào)方法,也可以使用字符串為所有表頭單元格設(shè)置一個(gè)固定的 className。 | Function({row, column, rowIndex, columnIndex})/String | — | — |
2. elment 有默認(rèn)的樣式,如果不想要,就換個(gè)class名字header-cell-class-name='requiredclass'
<style> .requiredclass>div.cell::before{ content: "*"; color: #f56c6c; margin-right: 4px; } </style>
3. 只加某幾個(gè):header-cell-class-name='addHeaderCellClassName'
addHeaderCellClassName({row, column, rowIndex, columnIndex}){ if(columnIndex===0 || columnIndex===1){ return 'requiredclass' } }
自定義表頭
在需要添加*
的位置添加自定義表頭
.requiredclass::before{ content: "*"; color: #f56c6c; margin-right: 4px; }
刪除某行,同時(shí)刪除這行的校驗(yàn)結(jié)果
問題: 刪除某行時(shí),這行的校驗(yàn)結(jié)果不隨著刪除
觀察圖片,刪除第三行,第三行的校驗(yàn)結(jié)果,還在.
解決方法
刪除時(shí),對(duì)表單進(jìn)行校驗(yàn)
del(index) { this.form.tableData.splice(index, 1); this.$refs.form.validate() },
row-key
綁定row-key,綁定的值要保證存在且唯一
<el-form ref="form" :model="form" label-width="20px"> <el-table ref="table" :data="form.tableData" empty-text="暫無數(shù)據(jù)" header-cell-class-name="requiredclass" row-key='id' > ... form: { tableData: [ { name: "張一", age: 19, id:'1', tel: 13333333332 }, ] }
到此這篇關(guān)于element中el-table與el-form同用并校驗(yàn)的文章就介紹到這了,更多相關(guān)element el-table與el-form內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用xlsx模塊實(shí)現(xiàn)讀寫Excel文檔內(nèi)容
在前端項(xiàng)目開發(fā)中,讀寫Excel文件的需求變得越來越常見,本文將詳細(xì)介紹如何在Vue項(xiàng)目中利用FileReader對(duì)象和xlsx模塊來讀取Excel文件的內(nèi)容,需要的可以參考下2024-03-03詳解Unity webgl 嵌入Vue實(shí)現(xiàn)過程
Unity webgl嵌入到前端網(wǎng)頁中,前端通過調(diào)用Unity webgl內(nèi)方法實(shí)現(xiàn)需要展示的功能,前端點(diǎn)擊Unity webgl內(nèi)的交互點(diǎn),Unity webgl返回給前端一些需要的數(shù)據(jù),這篇文章主要介紹了Unity webgl 嵌入Vue實(shí)現(xiàn)過程,需要的朋友可以參考下2024-01-01vue項(xiàng)目實(shí)現(xiàn)添加圖片裁剪組件
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目實(shí)現(xiàn)添加圖片裁剪組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03基于vue+canvas的excel-like組件實(shí)例詳解
a vue component,基于vue的表格組件,主要解決大數(shù)據(jù)量的表格渲染性能問題,使用canvas繪制表格,同時(shí)支持類似excel的批量選中,復(fù)制黏貼刪除,實(shí)時(shí)編輯等功能.這篇文章主要介紹了基于vue+canvas的excel-like組件,需要的朋友可以參考下2017-11-11vue中利用three.js實(shí)現(xiàn)全景圖的完整示例
這篇文章主要給大家介紹了關(guān)于vue中利用three.js實(shí)現(xiàn)全景圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12vue uniapp 防止按鈕多次點(diǎn)擊的三種實(shí)現(xiàn)方式
最近的項(xiàng)目完成后,在性能優(yōu)化階段需要做按鈕的防止重復(fù)點(diǎn)擊功能,本文主要介紹了vue uniapp 防止按鈕多次點(diǎn)擊的三種實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08