ElementUI表格行编辑单元格编辑支持(输入框,选择框)Demo

嗯,需要做成这个样子,所以网上查了些资料。整理了下。提供几个一个思路。不足之处请小伙伴指出来。

  •  普通版的table可编辑内嵌select选择框,输出框,编辑删除添加等
    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <!-- import CSS -->
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <!-- import Vue before Element -->
        <script src="https://unpkg.com/vue/dist/vue.js"></script>
        <!-- import JavaScript -->
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    </head>
    
    <body style="display: flex;justify-content: center;width: 100%;">
        <div id="app">
            <el-card>
                <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
                    <el-table-column prop="date" label="日期" width="180">
                        <template slot-scope="scope">
                            <el-select popper-class="role-option" v-model="scope.row.date" placeholder="请选择" size="small">
                                <el-option v-for="item in optionData" :key="item.value" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="姓名" width="180">
                        <template slot-scope="scope">
                            <el-select popper-class="role-option" v-model="scope.row.name" placeholder="请选择" size="small">
                                <el-option v-for="item in optionName" :key="item.value" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column prop="addr" width="180" label="地址">
                        <template slot-scope="scope">
                            <el-select popper-class="role-option" v-model="scope.row.addr" placeholder="请选择" size="small">
                                <el-option v-for="item in optionAddr" :key="item.value" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="100" header-align="right">
                        <template slot-scope="scope">
                            <div style="display: flex;justify-content: space-between;">
                                <el-link icon="el-icon-plus" size="mini" type="primary"
                                    @click="handleAdd(scope.$index, scope.row)" :underline="false"
                                    style="font-size: 14px;margin-left: 40px"></el-link>
                                <el-link icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)"
                                    type="danger" :underline="false"></el-link>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
        </div>
    </body>
    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    optionData: [{ value: "1", name: "2016-05-03" }, { value: "2", name: "2016-05-04" }, { value: "3", name: "2016-05-05" }],
                    optionName: [{ value: "1", name: " 王小虎" }, { value: "2", name: "王小龙" }, { value: "3", name: "王小狗" }],
                    optionAddr: [{ value: "1", name: "北凉" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }],
                    tableData: [{
                        date: '2016-05-02',
                        name: '王小虎',
                        addr: '北凉',
                    }, {
                        date: '2016-05-04',
                        name: '王小狗',
                        addr: '西北道'
                    },]
                }
            },
            mounted() {
    
            },
            methods: {
                handleAdd(index, row) {
                    this.tableData.push({
                        date: '',
                        name: '',
                        addr: ''
                    });
                },
                handleDelete(index, row) {
                    if (index > 0) {
                        this.tableData.splice(index, 1);
                    }
                },
                tableRowClassName({ row, rowIndex }) {
                    if ((rowIndex+1)%2 === 1) {
                        return 'warning-row';
                    } else{
                        return 'success-row';
                    }
                    return '';
                }
            },
            props: {},
            computed: {},
    
            created() {
    
            },
            watch: {},
        })
    </script>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }
    
        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
    
    </html>
  • 表格隐藏展示,数据联动,所谓数据联动,意思是通过第一列的选择控制之后两列的数据源展示不同的数据项。这里需要注意的如果选择框的数据源是动态通过接口获取的,那么需要把数据源由变量设置为函数,同时,接口的请求必须是同步的,不可以是异步,axios默认是异步的,如果没有合适的解决方案,可以使用jquery 的ajax方法。
    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <!-- import CSS -->
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <!-- import Vue before Element -->
        <script src="https://unpkg.com/vue/dist/vue.js"></script>
        <!-- import JavaScript -->
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    </head>
    
    <body style="display: flex;justify-content: center;width: 100%;">
        <div id="app">
            <el-card>
                <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
                    <el-table-column prop="date" label="日期" width="180">
                        <template slot-scope="scope">
                            <el-select popper-class="role-option" v-model="scope.row.date" placeholder="请选择" size="small"
                                @change="dateHandleChange">
                                <el-option v-for="item in optionData" :key="item.value" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="姓名" width="180">
                        <template slot-scope="scope">
                            <el-select popper-class="role-option" v-model="scope.row.name" placeholder="请选择" size="small"  v-show="scope.row.date != '2'" >
                                <el-option v-for="item in optionName" :key="item.value" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column prop="addr" width="180" label="地址">
                        <template slot-scope="scope">
                            <el-select popper-class="role-option" v-model="scope.row.addr" placeholder="请选择" size="small">
                                <el-option v-for="item in optionsAddr(scope.row)" :key="item.value" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="100" header-align="right">
                        <template slot-scope="scope">
                            <div style="display: flex;justify-content: space-between;">
                                <el-link icon="el-icon-plus" size="mini" type="primary"
                                    @click="handleAdd(scope.$index, scope.row)" :underline="false"
                                    style="font-size: 14px;margin-left: 40px"></el-link>
                                <el-link icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)"
                                    type="danger" :underline="false"></el-link>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
        </div>
    </body>
    <script>
     /**
        * @description: 表格隐藏展示,数据联动
        * @author: Liruilong
        */
        new Vue({
            el: '#app',
            data() {
                return {
                    optionData: [{ value: "1", name: "2016-05-03" }, { value: "2", name: "2016-05-04" }, { value: "3", name: "2016-05-05" }],
                    optionName: [{ value: "1", name: " 王小虎" }, { value: "2", name: "王小龙" }, { value: "3", name: "王小狗" }],
                    tableData: [{
                        date: '1',
                        name: '1',
                        addr: '1',
                    }],
                    optionAddrMapper: new Map(),
                }
            },
            mounted() {
                // 模拟ajax初始化数据集
                this.optionAddrMapper.set("1", [{ value: "1", name: "北凉" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
            },
            methods: {
                optionsAddr(row) {
                    console.log(row.date)
                    console.log(this.optionAddrMapper.get(row.date));
                    return this.optionAddrMapper.get(row.date);
                },
                //联动处理//模拟ajax调用接口获取数据.日期选择会联动地址
                dateHandleChange(val) {
                    // 模拟根据日期得到地址的数据集 这里写ajax ,必须是同步的请求。
                    this.optionAddrMapper.set("1", [{ value: "1", name: "北凉" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
                    this.optionAddrMapper.set("2", [{ value: "9", name: "西域" }, { value: "8", name: "苗疆" }, { value: "7", name: "剑气长城" }]);
                    this.optionAddrMapper.set("3", [{ value: "10", name: "圣贤林" }, { value: "11", name: "青冥天下" }, { value: "12", name: "藕花福地" }]);
                },
                handleAdd(index, row) {
                    this.tableData.push({
                        date: '',
                        name: '',
                        addr: ''
                    });
                },
                handleDelete(index, row) {
                    if (index > 0) {
                        this.tableData.splice(index, 1);
                    }
                },
                tableRowClassName({ row, rowIndex }) {
                    if ((rowIndex + 1) % 2 === 1) {
                        return 'warning-row';
                    } else {
                        return 'success-row';
                    }
                    return '';
                }
            },
            props: {},
            computed: {},
    
            created() {
    
            },
            watch: {},
        })
    </script>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }
    
        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
    
    </html>
  • 表格整体与编辑视图的切换,双击表格,或进行视图切换。可以编辑也可以展示
    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <!-- import CSS -->
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <!-- import Vue before Element -->
        <script src="https://unpkg.com/vue/dist/vue.js"></script>
        <!-- import JavaScript -->
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
        <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    </head>
    
    <body style="display: flex;justify-content: center;width: 100%;">
        <div id="app">
            <el-card>
                <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName"
                    @cell-dblclick="editdbClick">
                    <el-table-column prop="date" label="日期" width="180">
                        <template slot-scope="scope">
                            <el-select v-show="!isVlable" popper-class="role-option" v-model="scope.row.date"
                                placeholder="请选择" size="small" @change="dateHandleChange">
                                <el-option v-for="item in optionData" :key="item.name" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                            <span style="line-height: 32px;" v-show="isVlable">{{ optionDataf(scope.row.date)}} </span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="姓名" width="180">
                        <template slot-scope="scope">
                            <el-select v-show="!isVlable && scope.row.date != '2' " popper-class="role-option"
                                v-model="scope.row.name" placeholder="请选择" size="small">
                                <el-option v-for="item in optionName" :key="item.name" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                            <span style="line-height: 32px;" v-show="isVlable">{{optionNamef(scope.row.name) }} </span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="addr" width="180" label="地址">
                        <template slot-scope="scope">
                            <el-select v-show="!isVlable" popper-class="role-option" v-model="scope.row.addr"
                                placeholder="请选择" size="small">
                                <el-option v-for="item in optionsAddr(scope.row)" :key="item.name" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                            <span style="line-height: 32px;" v-show="isVlable">{{optionsAddrf(scope.row.addr) }}
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="100" header-align="right">
                        <template slot-scope="scope">
                            <div style="display: flex;justify-content: space-between;">
                                <el-link icon="el-icon-plus" size="mini" type="primary"
                                    @click="handleAdd(scope.$index, scope.row)" :underline="false"
                                    style="font-size: 14px;margin-left: 40px"></el-link>
                                <el-link icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)"
                                    type="danger" :underline="false"></el-link>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
        </div>
    </body>
    <script>
     /**
        * @description: 表格与编辑视图的切换,双击表格,或进行视图切换。可以编辑也可以展示。
        * @author: Liruilong
        */
        new Vue({
            el: '#app',
            data() {
                return {
                    isVlable: false,
                    optionData: [{ value: "1", name: "2016-05-03" }, { value: "2", name: "2016-05-04" }, { value: "3", name: "2016-05-05" }],
                    optionName: [{ value: "1", name: " 王小虎" }, { value: "2", name: "王小龙" }, { value: "3", name: "王小狗" }],
                    optionAddr:[{ value: "1", name: "北凉" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }],
                    tableData: [{
                        date: '1',
                        name: '1',
                        addr: '1',
                    }],
                    optionAddrMapper: new Map(),
                    date: '',
                    name: '',
                    address: '',
                }
            },
            created() {
    
            },
            mounted() {
                 // 模拟ajax初始化数据集
                 this.optionAddrMapper.set("1", [{ value: "1", name: "北凉" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
            },
            methods: {
                optionDataf(id) {
                    let name = id;
                    this.optionData.forEach((element) => {
    
                        if (element.value == id) {
                            name = element.name;
                        }
                    });
                    return name;
                },
                optionNamef(id) {
                    let name = id;
                    this.optionName.forEach((element) => {
                        if (element.value == id) {
                            name = element.name;
                        }
                    });
                    return name;
                },
                optionsAddrf(id) {
                    let name = id;
                  if(this.optionAddrMapper){
                    this.optionAddrMapper.forEach((value, key) => {
                        value.forEach((e) => {
                            if (e.value == id) {
                                name = e.name;
                            }
                        })
                    });
                }else{
                    this.optionAddr.forEach( e =>{
                        if (e.value == id) {
                                name = e.name;
                            }
                    });
                }
                    return name;
                },
                editdbClick(row, column, cell, event) {
                    this.isVlable = !this.isVlable;
                },
                saveClick(row, column, cell, event) {
                },
                optionsAddr(row) {
                    return this.optionAddrMapper.get(row.date);
                },
                //联动处理//模拟ajax调用接口获取数据.日期选择会联动地址
                dateHandleChange(val) {
                    // 模拟根据日期得到地址的数据集 这里写ajax ,必须是同步的请求。
                    this.optionAddrMapper.set("1", [{ value: "1", name: "北凉" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
                    this.optionAddrMapper.set("2", [{ value: "9", name: "西域" }, { value: "8", name: "苗疆" }, { value: "7", name: "剑气长城" }]);
                    this.optionAddrMapper.set("3", [{ value: "10", name: "圣贤林" }, { value: "11", name: "青冥天下" }, { value: "12", name: "藕花福地" }]);
                },
                // 添加操作。默认会保存当前行的数据,并添加一条新数据
                handleAdd(index, row) {
    
                    if ((row.date && row.name && row.addr) || (row.date && row.addr)) {
                        this.tableData.push({
                            date: '',
                            name: '',
                            addr: ''
                        });
                    }
                },
                handleDelete(index, row) {
                    debugger
                    if (index > 0) {
                        this.tableData.splice(index, 1);
                    }
                },
                tableRowClassName({ row, rowIndex }) {
                    row.index = rowIndex;
                    if ((rowIndex + 1) % 2 === 1) {
                        return 'warning-row';
                    } else {
                        return 'success-row';
                    }
                    return '';
                }
            },
            props: {},
            computed: {},
    
            created() {
    
            },
            watch: {},
            filters: {
    
            },b
    
        })
    </script>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }
    
        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
    
    </html>
  • 行编辑视图手动切换,通过双击表格实现编辑的表格的切换。

     

    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <!-- import CSS -->
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <!-- import Vue before Element -->
        <script src="https://unpkg.com/vue/dist/vue.js"></script>
        <!-- import JavaScript -->
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
        <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    </head>
    
    <body style="display: flex;justify-content: center;width: 100%;">
        <div id="app">
            <el-card>
                <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName"
                    @cell-dblclick="editdbClick">
                    <el-table-column prop="date" label="日期" width="180">
                        <template slot-scope="scope">
                            <el-select v-show="!showEdit[scope.$index]" popper-class="role-option" v-model="scope.row.date"
                                placeholder="请选择" size="small" @change="dateHandleChange">
                                <el-option v-for="item in optionData" :key="item.name" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                            <span style="line-height: 32px;"
                                v-show="showEdit[scope.$index]">{{ optionDataf(scope.row.date)}} </span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="姓名" width="180">
                        <template slot-scope="scope">
                            <el-select v-show="!showEdit[scope.$index] && scope.row.date != '2' " popper-class="role-option"
                                v-model="scope.row.name" placeholder="请选择" size="small">
                                <el-option v-for="item in optionName" :key="item.name" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                            <span style="line-height: 32px;"
                                v-show="showEdit[scope.$index]">{{optionNamef(scope.row.name) }} </span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="addr" width="180" label="地址">
                        <template slot-scope="scope">
                            <el-select v-show="!showEdit[scope.$index]" popper-class="role-option" v-model="scope.row.addr"
                                placeholder="请选择" size="small">
                                <el-option v-for="item in optionsAddr(scope.row)" :key="item.name" :label="item.name"
                                    :value="item.value"></el-option>
                            </el-select>
                            <span style="line-height: 32px;"
                                v-show="showEdit[scope.$index]">{{optionsAddrf(scope.row.addr) }}
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="100" header-align="right">
                        <template slot-scope="scope">
                            <div style="display: flex;justify-content: space-between;">
                                <el-link icon="el-icon-plus" size="mini" type="primary"
                                    @click="handleAdd(scope.$index, scope.row)" :underline="false"
                                    style="font-size: 14px;margin-left: 40px"></el-link>
                                <el-link icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)"
                                    type="danger" :underline="false"></el-link>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
        </div>
    </body>
    <script>
     /**
        * @description: 双击行视图手动切换,通过双击表格实现编辑的表格的切换
        * @author: Liruilong
        */
        new Vue({
            el: '#app',
            data() {
                return {
                    optionData: [{ value: "1", name: "2016-05-03" }, { value: "2", name: "2016-05-04" }, { value: "3", name: "2016-05-05" }],
                    optionName: [{ value: "1", name: " 王小虎" }, { value: "2", name: "王小龙" }, { value: "3", name: "王小狗" }],
                    optionAddr: [{ value: "1", name: "北凉" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }],
                    tableData: [{
                        date: '1',
                        name: '1',
                        addr: '1',
                    }],
                    optionAddrMapper: new Map(),
                    date: '',
                    name: '',
                    address: '',
                    showEdit: [false],
                }
            },
            created() {
    
            },
            mounted() {
                // 模拟ajax初始化数据集
                this.optionAddrMapper.set("1", [{ value: "1", name: "北凉" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
            },
            methods: {
                optionDataf(id) {
                    let name = id;
                    this.optionData.forEach((element) => {
    
                        if (element.value == id) {
                            name = element.name;
                        }
                    });
                    return name;
                },
                optionNamef(id) {
                    let name = id;
                    this.optionName.forEach((element) => {
                        if (element.value == id) {
                            name = element.name;
                        }
                    });
                    return name;
                },
                optionsAddrf(id) {
                    let name = id;
                    if (this.optionAddrMapper) {
                        this.optionAddrMapper.forEach((value, key) => {
                            value.forEach((e) => {
                                if (e.value == id) {
                                    name = e.name;
                                }
                            })
                        });
                    } else {
                        this.optionAddr.forEach(e => {
                            if (e.value == id) {
                                name = e.name;
                            }
                        });
                    }
                    return name;
                },
                editdbClick(row, column, cell, event) {
    
                    if ((row.date && row.name && row.addr) || (row.date && row.addr)) {
                        this.$set(this.showEdit, row.index, !this.showEdit[row.index])
                    }
    
                },
                // openShow(index){
                //     this.showEdit.forEach( (e) =>{
                //         if(e != index){
                //             this.$set(this.showEdit, e, false)
                //         }
    
                //     })
                // },
                saveClick(row, column, cell, event) {
                },
                optionsAddr(row) {
                    return this.optionAddrMapper.get(row.date);
                },
                //联动处理//模拟ajax调用接口获取数据.日期选择会联动地址
                dateHandleChange(val) {
                    // 模拟根据日期得到地址的数据集 这里写ajax ,必须是同步的请求。
                    this.optionAddrMapper.set("1", [{ value: "1", name: "北凉" }, { value: "2", name: "西北道" }, { value: "3", name: "泰安城" }]);
                    this.optionAddrMapper.set("2", [{ value: "9", name: "西域" }, { value: "8", name: "苗疆" }, { value: "7", name: "剑气长城" }]);
                    this.optionAddrMapper.set("3", [{ value: "10", name: "圣贤林" }, { value: "11", name: "青冥天下" }, { value: "12", name: "藕花福地" }]);
                },
                // 添加操作。默认会保存当前行的数据,并添加一条新数据
                handleAdd(index, row) {
                    let data = this.tableData[this.tableData.length - 1];
                    if (((row.date && row.name && row.addr) || (row.date && row.addr)) && ((data.date && data.name && data.addr) || (data.date && data.addr))) {
                        this.$set(this.showEdit, row.index, true)
                        this.tableData.push({
                            date: '',
                            name: '',
                            addr: ''
                        });
                        this.$set(this.showEdit, row.index + 1, false)
                    }
                },
                handleDelete(index, row) {
                    debugger
                    if (index > 0) {
                        this.tableData.splice(index, 1);
                    }
                },
                tableRowClassName({ row, rowIndex }) {
                    row.index = rowIndex;
                    if ((rowIndex + 1) % 2 === 1) {
                        return 'warning-row';
                    } else {
                        return 'success-row';
                    }
                    return '';
                }
            },
            props: {},
            computed: {},
    
            created() {
    
            },
            watch: {},
            filters: {
    
            },
    
        })
    </script>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }
    
        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
    
    </html>
  • 单机视图自动切换,即只留一条要编辑的操作。这个和之前的原理不一样,通过样式控制。
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <!-- import CSS -->
        <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
        <!-- import Vue before Element -->
        <script src="https://unpkg.com/vue/dist/vue.js"></script>
        <!-- import JavaScript -->
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    </head>
    
    
    <body style="display: flex;justify-content: center;width: 100%;margin-top: 20px;">
        <div id="app">
            <el-card>
                <!-- 关键代码 highlight-current-row -->
                <el-table :data="tableData" style="width: 100%" class="tb-edit"  highlight-current-row @row-click="handleCurrentChange" :row-class-name="tableRowClassName" >
                    <el-table-column prop="date" label="日期" width="180">
                        <template slot-scope="scope">
                            <el-input size="small" v-model="scope.row.date" placeholder="请输入内容"
                                @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.date}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="姓名" width="180">
                        <template slot-scope="scope">
                            <el-input size="small" v-model="scope.row.name" placeholder="请输入内容"
                                @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.name}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="addr" width="180" label="地址">
                        <template slot-scope="scope">
                            <el-input size="small" v-model="scope.row.addr" placeholder="请输入内容"
                                @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.addr}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="100" header-align="right">
                        <template slot-scope="scope">
                            <div style="display: flex;justify-content: space-between;">
                                <el-link icon="el-icon-plus" size="mini" type="primary"
                                    @click="handleAdd(scope.$index, scope.row)" :underline="false"
                                    style="font-size: 14px;margin-left: 40px"></el-link>
                                <el-link icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)"
                                    type="danger" :underline="false"></el-link>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
        </div>
    </body>
    <script>
        /**
        * @description: 单机视图自动切换,即只留一条要编辑的操作。这个和之前的原理不一样,通过样式控制。
        * @author: Liruilong
        */
        new Vue({
            el: '#app',
            data() {
                return {
                    tableData: [{
                        date: '2016-05-02',
                        name: '王小虎',
                        addr: '北凉',
                    }, {
                        date: '2016-05-04',
                        name: '王小狗',
                        addr: '西北道'
                    }, {
                        date: '2016-05-04',
                        name: '王小狗',
                        addr: '西北道'
                    }, {
                        date: '2016-05-04',
                        name: '王小狗',
                        addr: '西北道'
                    }]
                }
            },
            mounted() {
    
            },
            methods: {
                handleCurrentChange(row, event, column) {
                    console.log(row, event, column, event.currentTarget)
                    console.log("根据 Element Table @row-click=handleCurrentChange 事件,当该行点击的时候会动态添加一个 .current-row 的class属性,然后在 css 中进行 display 控制就行了。")
                },
                handleAdd(index, row) {
                    this.tableData.push({
                        date: '',
                        name: '',
                        addr: ''
                    });
                },
                handleDelete(index, row) {
                    if (index > 0) {
                        this.tableData.splice(index, 1);
                    }
                },
                tableRowClassName({ row, rowIndex }) {
                    row.index = rowIndex;
                    if ((rowIndex + 1) % 2 === 1) {
                        return 'warning-row';
                    } else {
                        return 'success-row';
                    }
                    return '';
                }
            },
    
            props: {},
            computed: {},
    
            created() {
    
            },
            watch: {},
        })
    </script>
    <style> 
        .el-table .warning-row {
            background: oldlace;
        }
    
        .el-table .success-row {
            background: #f0f9eb;
        }
        .tb-edit .el-input {
            display: none
        }
    
        .tb-edit .current-row .el-input {
            display: block
        }
    
        .tb-edit .current-row .el-input+span {
            display: none
        }
    </style>
    
    </html>

     

  •  

posted @ 2020-11-13 10:51  山河已无恙  阅读(4921)  评论(0编辑  收藏  举报