element el-table嵌套input输入框动态添加行 form动态表单验证实现

1. 总结

  1. el-form 包裹 el-table,el-table的data为form.data
  2. el-input prop为data.${scope.$index}.phoneNum, :rules=“rules.phoneNum”, v-model=“scope.row.phoneNum”
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, minimal-ui">
    
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
head>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js">script>
<script src="https://unpkg.com/element-ui/lib/index.js">script>

<body>
    <div id="app">
        <el-form label-width="140px" size="mini" ref="form" :model="form" :rules="rules">
            <el-table stripe size="mini" :data="form.tableData" style="width: 60%;height:100%;">
                <el-table-column label="日期" prop="date" width="180px" align="center">el-table-column>
                <el-table-column label="姓名" prop="name" width="100px" align="center">el-table-column>
                <el-table-column label="电话号码" align="center" style="width: 100%">
                    <template slot-scope="scope">
                        <el-form-item :prop="`tableData.${scope.$index}.phoneNum`" :rules="rules.phoneNum">
                            <el-input v-model="scope.row.phoneNum">el-input>
                        el-form-item>
                    template>
                el-table-column>
                <el-table-column prop="address" label="地址" width="180px">
                el-table-column>
                <el-table-column fixed="right" label="操作" align='center' prop='operation' width='150px'>
                    <template slot-scope="scope">
                        <el-button-group>
                            <el-button type="danger" size="mini" @click="operate(scope.$index, 'delete')">删 除
                            el-button>
                        el-button-group>
                    template>
                el-table-column>
            el-table>
        el-form>
    div>
    <script type="text/javascript">
        let vm = new Vue({
            el: '#app',
            data() {
                return {
                    form: {
                        tableData: [{
                            date: '2016-05-02',
                            name: '王小虎',
                            address: '上海市普陀区金沙江路 1518 弄'
                        }, {
                            date: '2016-05-04',
                            name: '王小虎',
                            address: '上海市普陀区金沙江路 1517 弄'
                        }, {
                            date: '2016-05-01',
                            name: '王小虎',
                            address: '上海市普陀区金沙江路 1519 弄'
                        }, {
                            date: '2016-05-03',
                            name: '王小虎',
                            address: '上海市普陀区金沙江路 1516 弄'
                        }]
                    },
                    rules: {
                        phoneNum: [
                            { required: true, message: '请输入电话号码', trigger: 'blur' }
                        ]
                    },
                }
            },
            methods: {
                // 操作
                operate(index, type) {
                    if (type === 'delete') {
                        this.form.tableData.splice(index, 1)
                    }
                }
            }
        });
    script>
body>

html>

你可能感兴趣的:(element,javascript,前端,vue.js)