elementUI的表单里套表格加验证的demo

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>elementUI</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        #app {
            width: 600px;
            margin: 30px;
        }
    </style>
</head>

<body>
    <div id="app">
        <el-form :model="myModel" :rules="rules" ref="myForm" size="small" label-width="100px">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="myModel.name" clearable></el-input>
            </el-form-item>
            <el-form-item label="家人" prop="family" required>
                <el-button type="success" size="small" style="margin-bottom: 10px;" plain icon="el-icon-plus"
                    @click="addFamily">添加</el-button>
                <el-table :data="myModel.family" size="medium" border>
                    <el-table-column prop="relation" label="关系">
                        <template slot-scope="scope">
                            <el-form-item :prop="'family.' + scope.$index + '.relation'" :rules="rules.relation"
                                style="margin-bottom: 0;">
                                <el-select v-model="scope.row.relation" clearable>
                                    <el-option label="父母" value="1"></el-option>
                                    <el-option label="夫妻" value="2"></el-option>
                                    <el-option label="子女" value="3"></el-option>
                                </el-select>
                            </el-form-item>
                        </template>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="姓名">
                        <template slot-scope="scope">
                            <el-form-item :prop="'family.' + scope.$index + '.name'" :rules="rules.name"
                                style="margin-bottom: 0;">
                                <el-input v-model="scope.row.name" clearable></el-input>
                            </el-form-item>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="medium" @click="submitForm('myForm')">提交</el-button>
            </el-form-item>
        </el-form>


    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                myModel: {
                    name: '',
                    family: []
                },
                rules: {
                    name: {
                        required: true,
                        message: '请输入姓名',
                        trigger: ['change', 'blur']
                    },
                    relation: {
                        required: true,
                        message: '请选择关系',
                        trigger: ['change', 'blur']
                    }
                }
            },
            methods: {
                addFamily() {
                    this.myModel.family.push({
                        relation: "",
                        name: "",
                        email: ""
                    });
                },
                submitForm(formName) {
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            alert('submit!');
                        } else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                }
            }
        })
    </script>
</body>

</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值