<template>
<el-form ref="form" :model="form" label-width="80px">
<el-form-item label="用户信息">
<el-button size="small" @click="addUser">添加用户</el-button>
<el-table :data="form.users" style="width: 100%;">
<el-table-column prop="name" label="姓名" width="180">
<template slot-scope="scope">
<el-form-item :prop="'users[' + scope.$index + '].name'" :rules="rules.name">
<el-input v-model="scope.row.name"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column prop="age" label="年龄" width="180">
<template slot-scope="scope">
<el-form-item :prop="'users[' + scope.$index + '].age'" :rules="rules.age">
<el-input v-model.number="scope.row.age"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button size="mini" type="danger" @click="removeUser(scope.$index)">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('form')">提交</el-button>
<el-button @click="resetForm('form')">重置</el-button>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
form: {
users: [
// 初始为空数组
]
},
rules: {
name: [
{ required: true, message: '请输入姓名', trigger: 'blur' }
],
age: [
{ required: true, message: '请输入年龄', trigger: 'blur' },
{ type: 'number', message: '年龄必须是数字值', trigger: 'blur' }
]
}
};
},
methods: {
addUser() {
this.form.users.push({
name: '',
age: null
});
},
评论已关闭