使用element-ui +Vue 解決 table 里包含表單驗(yàn)證的問(wèn)題
應(yīng)用場(chǎng)景:
在實(shí)際使用中經(jīng)常會(huì)遇到需要在Form表單中使用table表格進(jìn)行表單提交,同時(shí)又需要對(duì)table的字段進(jìn)行校驗(yàn),效果如圖所示:
這個(gè)校驗(yàn)中,最關(guān)鍵的問(wèn)題在于如何給el-form-item 動(dòng)態(tài)綁定prop。
:prop="'tableData.' + scope.$index + '.字段名'"
方法一:
<template>
<div class="app-container">
<el-form :model="fromData" ref="from">
<el-table :data="fromData.domains">
<el-table-column label="姓名">
<template slot-scope="scope">
<el-form-item :prop="'domains.'+scope.$index+'.name'" :rules="fromaDataRules.name">
<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 :prop="'domains.'+scope.$index+'.desc'" :rules="fromaDataRules.desc">
<el-input v-model="scope.row.desc"></el-input>
</el-form-item>
</template>
</el-table-column>
</el-table>
</el-form>
<el-button type="warning" @click="submit('from')">submit</el-button>
</div>
</template>
<script>
export default {
data() {
return {
fromData:{
domains:undefined
},
fromaDataRules:{
name:[{ required: true, message: '請(qǐng)輸入', trigger: 'blur' }],
desc:[ { required: true, message: '請(qǐng)?zhí)顚?xiě)', trigger: 'blur' }]
},
domains:[],
}
},
mounted(){
this.initDomains()
},
methods:{
initDomains(){
this.domains=[
{
name: "小紅",
desc: "11123"
},
{
name: "小紅",
desc: "11123"
}
]
},
init(){
this.$set(this.fromData,'domains',this.domains)
},
submit(formName){
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
}
}
}
</script>
上述代碼中比較關(guān)鍵的部分有一下兩點(diǎn):
1、:prop="‘domains.'+scope.$index+'.name'" ,用于動(dòng)態(tài)綁定prop到el-form-item;
2、this.$set(this.fromData,‘domains',this.domains) ,用于為fromData設(shè)置domains這個(gè)節(jié)點(diǎn)。
方法二:
<template>
<div class="app-container">
<el-form :model="fromData" ref="from">
<el-table :data="fromData.domains">
<el-table-column label="姓名">
<template slot-scope="scope">
<el-form-item :prop="'domains.'+scope.$index+'.name'" :rules="fromData.fromaDataRules.name">
<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 :prop="'domains.'+scope.$index+'.desc'" :rules="fromData.fromaDataRules.desc">
<el-input v-model="scope.row.desc"></el-input>
</el-form-item>
</template>
</el-table-column>
</el-table>
</el-form>
<el-button type="warning" @click="submit('from')">submit</el-button>
</div>
</template>
<script>
export default {
data() {
return {
fromData:{
fromaDataRules:{
name:[{ required: true, message: '請(qǐng)輸入', trigger: 'blur' }],
desc:[ { required: true, message: '請(qǐng)?zhí)顚?xiě)', trigger: 'blur' }]
},
domains:[],
},
}
},
mounted(){
this.initDomains()
},
methods:{
initDomains(){
this.fromData.domains=[
{
name: "小紅",
desc: "11123"
},
{
name: "小紅",
desc: "11123"
}
]
},
init(){
},
submit(formName){
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
}
}
}
</script>
補(bǔ)充知識(shí):Vue+ElementUI 完整增刪查改驗(yàn)證功能的表格
我就廢話(huà)不多說(shuō)了,大家還是直接看代碼吧~
<template>
<div class="block">
<el-col>
<el-row>
<el-form>
<el-form-item>
<el-input style="width: 250px;float: left" placeholder="請(qǐng)輸入名稱(chēng)" v-model="query"></el-input>
<el-button @click="handleSelect" style="float: left;margin-left: 10px">查詢(xún)</el-button>
<el-button @click="handleAdd" style="float: left;margin-left: 10px">新增</el-button>
</el-form-item>
</el-form>
</el-row>
<el-row>
<el-table
:data="tableData"
style="width: 100%"
border>
<el-table-column
prop="date"
label="日期"
width="250">
</el-table-column>
<el-table-column
prop="name"
label="姓名"
width="250">
</el-table-column>
<el-table-column
prop="address"
label="地址"
width="350">
</el-table-column>
<el-table-column>
<template slot-scope="scope">
<el-button size="mini" @click="handleEdit(scope.$index,scope.row)">編輯</el-button>
<el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">刪除</el-button>
</template>
</el-table-column>
</el-table>
</el-row>
<el-row>
<el-dialog class="dialog" :title="operation===true ?'新增':'編輯'" :visible.sync="dialogVisible" width="350px" >
<el-form label-width="80px" :model="lineData" :rules="addRule" ref="lineData" >
<el-form-item label="日期" prop="date">
<el-input v-model="lineData.date"></el-input>
</el-form-item>
<el-form-item label="姓名" prop="name">
<el-input v-model="lineData.name"></el-input>
</el-form-item>
<el-form-item label="地址">
<el-input v-model="lineData.address"></el-input>
</el-form-item>
<el-form-item>
<el-button @click="handleSave" type="primary">確定</el-button>
<el-button @click="handleCancel">取消</el-button>
</el-form-item>
</el-form>
</el-dialog>
</el-row>
</el-col>
</div>
</template>
<script>export default {
data () {
return {
operation: true,
dialogVisible: false,
lineData: {},
editData: {},
query: '',
addRule: {
date: [{required: true, message: '請(qǐng)輸入日期', trigger: 'blur'}],
name: [{required: true, message: '請(qǐng)輸入名稱(chēng)', trigger: 'blur'}]
},
tableData: [{
id: 1,
date: '2016-05-02',
name: '王一虎',
address: '上海市普陀區(qū)金沙江路 1518 弄'
}, {
id: 2,
date: '2016-05-04',
name: '王二虎',
address: '上海市普陀區(qū)金沙江路 1517 弄'
}, {
id: 3,
date: '2016-05-01',
name: '王一虎',
address: '上海市普陀區(qū)金沙江路 1519 弄'
}, {
id: 4,
date: '2016-05-03',
name: '王四虎',
address: '上海市普陀區(qū)金沙江路 1516 弄'
}]
}
},
methods: {
handleEdit (index, row) {
this.editData = JSON.stringify(row)
this.lineData = JSON.parse(this.editData)
this.dialogVisible = true
this.operation = false
},
handleDelete (index, row) {
this.tableData.splice(index, 1)
},
handleAdd () {
this.dialogVisible = true
this.operation = true
this.lineData = {}
this.lineData.id = Math.random()
},
handleSelect () {
if (this.query !== '') {
const tmpData = []
for (let item of this.tableData) {
if (item.name === this.query) {
tmpData.push(item)
}
}
this.tableData = tmpData
}
},
handleSave () {
this.$refs.lineData.validate((valid) => {
if (valid) {
this.addLine(this.lineData)
this.dialogVisible = false
} else {
alert('保存失敗')
return false
}
})
},
handleCancel () {
this.dialogVisible = false
},
addLine (item) {
let existed = false
for (let i = 0; i < this.tableData.length; i++) {
if (this.tableData[i].id === item.id) {
this.tableData[i].id = item.id
this.tableData[i].name = item.name
this.tableData[i].address = item.address
this.tableData[i].date = item.date
existed = true
break
}
}
if (!existed) {
this.tableData.push(this.lineData)
}
}
}
}
</script>
<style scoped>
.block{
width: 75%;
margin-left: 400px;
margin-top: 200px;
}
</style>
以上這篇使用element-ui +Vue 解決 table 里包含表單驗(yàn)證的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue.js使用Element-ui實(shí)現(xiàn)導(dǎo)航菜單
- vue.js+element-ui動(dòng)態(tài)配置菜單的實(shí)例
- vue.js與element-ui實(shí)現(xiàn)菜單樹(shù)形結(jié)構(gòu)的解決方法
- vue.js element-ui tree樹(shù)形控件改iview的方法
- 使用Vue.js和Element-UI做一個(gè)簡(jiǎn)單登錄頁(yè)面的實(shí)例
- vue.js element-ui validate中代碼不執(zhí)行問(wèn)題解決方法
- Vue element-ui父組件控制子組件的表單校驗(yàn)操作
- 詳解element-ui 表單校驗(yàn) Rules 配置 常用黑科技
- vue.js+element-ui的基礎(chǔ)表單實(shí)例代碼
相關(guān)文章
組件庫(kù)中使用 vue-i18n 國(guó)際化的案例詳解
這篇文章主要介紹了組件庫(kù)中使用 vue-i18n 國(guó)際化,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
簡(jiǎn)述Vue中容易被忽視的知識(shí)點(diǎn)
這篇文章主要介紹了簡(jiǎn)述Vue中容易被忽視的知識(shí)點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Vue實(shí)現(xiàn)具備掃描和查看數(shù)據(jù)的二維碼
在我們生活中,二維碼的應(yīng)用越來(lái)越廣泛,特別是在移動(dòng)互聯(lián)網(wǎng)的時(shí)代,二維碼成為了快速傳達(dá)信息的一種利器,本文我們就來(lái)看看如何在Vue框架下,實(shí)現(xiàn)一個(gè)具備掃描和查看數(shù)據(jù)的二維碼吧2023-05-05
vue3中實(shí)現(xiàn)使用element-plus調(diào)用message
這篇文章主要介紹了vue3中實(shí)現(xiàn)使用element-plus調(diào)用message,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue+Springboot實(shí)現(xiàn)接口簽名的示例代碼
這篇文章主要介紹了Vue+Springboot實(shí)現(xiàn)接口簽名的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
vue單行文本溢出會(huì)出現(xiàn)title提示自定義指令
這篇文章主要為大家介紹了vue單行文本溢出會(huì)出現(xiàn)title提示自定義指令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

