element-ui的el-form表单和el-table校验嵌套使用校验el-input和el-select
element
A Vue.js 2.0 UI Toolkit for Web
项目地址:https://gitcode.com/gh_mirrors/eleme/element
免费下载资源
·
本文主要讲element-ui的el-form表单和el-table校验嵌套使用校验el-input和el-select
效果
点击保存可校验el-input和el-select是否有值,不符合校验规则就标红提醒
1.el-form嵌套el-table
- 1.el-form里面嵌套el-table
<el-form :model="formData" :rules="formData.rules" ref="formRef">
<el-table
:data="formData.tableData"
style="width: 100%">
<el-table-column
prop="id"
label="单号"
width="100">
</el-table-column>
</el-table>
</el-form>
- 2.在el-table-column自定义内容里面,嵌套el-form-item,里面再放el-input或者el-select结构
<el-table-column
prop="name"
label="商品名"
width="180">
<template slot-scope="scope">
<el-form-item>
<el-input v-model="scope.row.name"></el-input>
</el-form-item>
</template>
</el-table-column>
- 3.数据的结构
因为el-form是最外层的,所以formData是个对象,对象里放el-table用到的数组和rules
这样定义是为了校验时用到rules
formData: {
rules: {
name: {
required: true,
message: '请输入商品名',
tirgger: ['blur', 'change']
},
selected: {
required: true,
message: '请选择',
tirgger: 'change'
}
},
tableData: [
{id: '001'},
{id: '002'},
{id: '003'},
]
},
2.定义规则
1.el-form绑定: rules使用formData.rules整个对象,同时定义一个ref
<el-form :model="formData" :rules="formData.rules" ref="formRef">
2.保证prop必须唯一
自定义prop:使用 列表数据属性名+列的下标scope.$index+列的数据属性名
<el-table-column
prop="name"
label="商品名"
width="180">
<template slot-scope="scope">
<el-form-item :prop="'tableData.' + scope.$index + '.name'" :rules="formData.rules.name">
<el-input v-model="scope.row.name"></el-input>
</el-form-item>
</template>
</el-table-column>
注意:如果不是使用列表数据属性名tableData,会出现报错
Error: please transfer a valid prop path to form item
3.保证每一个el-form-item都要配置rules属性
rules使用formData.rules对应的属性的规则
3.完整代码
<template>
<div>
<el-form :model="formData" :rules="formData.rules" ref="formRef">
<el-table
:data="formData.tableData"
style="width: 100%">
<el-table-column
prop="id"
label="单号"
width="100">
</el-table-column>
<el-table-column
prop="name"
label="商品名"
width="180">
<template slot-scope="scope">
<el-form-item :prop="'tableData.' + scope.$index + '.name'" :rules="formData.rules.name">
<el-input v-model="scope.row.name"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column
prop="class"
label="分类">
<template slot-scope="scope">
<el-form-item :prop="'tableData.' +scope.$index + '.class'" :rules="formData.rules.selected">
<el-select v-model="scope.row.class">
<el-option
v-for="item in classOption"
:key="item.value"
:label="item.label"
:value="item.value"
></el-option>
</el-select>
</el-form-item>
</template>
</el-table-column>
</el-table>
</el-form>
<el-button @click="submitForm('formRef')">提交</el-button>
</div>
</template>
<script>
export default {
data() {
return {
formData: {
rules: {
name: {
required: true,
message: '请输入商品名',
tirgger: ['blur', 'change']
},
selected: {
required: true,
message: '请选择',
tirgger: 'change'
}
},
tableData: [
{id: '001'},
{id: '002'},
{id: '003'},
]
},
classOption: [
{
value: 0,
label: '分类1'
},
{
value: 0,
label: '分类1'
}
]
}
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
}
}
</script>
GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:3 个月前 )
c345bb45
7 个月前
a07f3a59
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update table.md
* Update transition.md
* Update popover.md 7 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)