ElementUI表格行编辑单元格编辑支持(输入框,选择框)_elementui 行内编辑-程序员宅基地

技术标签: 前端界面实现  

1、普通版的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>
3、表格整体与编辑视图的切换,双击表格,或进行视图切换。可以编辑也可以展示

在这里插入图片描述

<!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>
4、行编辑视图手动切换,通过双击表格实现编辑的表格的切换。

在这里插入图片描述

<!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>
5、单机视图自动切换,即只留一条要编辑的操作。这个和之前的原理不一样,通过样式控制

在这里插入图片描述

<!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>
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xiaoge_586/article/details/117090446

智能推荐

python中文显示不出来_解决Python词云库wordcloud不显示中文的问题-程序员宅基地

文章浏览阅读2.6k次。解决Python词云库wordcloud不显示中文的问题2018-11-25背景:wordcloud是基于Python开发的词云生成库,功能强大使用简单。github地址:https://github.com/amueller/word_cloudwordcloud默认是不支持显示中文的,中文会被显示成方框。安装:安装命令:pip install wordcloud解决:经过测试发现不支持显示中文..._词云python代码无法输出文字

台式计算机cpu允许温度,玩游戏cpu温度多少正常(台式电脑夏季CPU一般温度多少)...-程序员宅基地

文章浏览阅读1.1w次。随着炎热夏季的到来,当玩游戏正爽的时候,电脑突然死机了,自动关机了,是不是有想给主机一脚的冲动呢?这个很大的原因是因为CPU温度过高导致的。很多新手玩家可能都有一个疑虑,cpu温度多少以下正常?有些说是60,有些说是70,到底多高CPU温度不会死机呢?首先我们先看看如何查看CPU的温度。下载鲁大师并安装,运行鲁大师软件,即可进入软件界面,并点击温度管理,即可看到电脑各个硬件的温度。鲁大师一般情况下..._台式机玩游戏温度多少正常

小白自学Python日记 Day2-打印打印打印!_puthon打印任务收获-程序员宅基地

文章浏览阅读243次。Day2-打印打印打印!我终于更新了!(哭腔)一、 最简单的打印最最简单的打印语句: print(“打印内容”)注意:python是全英的,符号记得是半角下面是我写的例子:然后进入power shell ,注意:你需要使用cd来进入你保存的例子的文件夹,保存时名字应该取为xxx.py我终于知道为什么文件夹取名都建议取英文了,因为进入的时候是真的很麻烦!如果你没有进入正确的文件夹..._puthon打印任务收获

Docker安装:Errors during downloading metadata for repository ‘appstream‘:_"cenerrors during download metadata for repository-程序员宅基地

文章浏览阅读1k次。centos8问题参考CentOS 8 EOL如何切换源? - 云服务器 ECS - 阿里云_"cenerrors during download metadata for repository \"appstream"

尚硅谷_谷粒学苑-微服务+全栈在线教育实战项目之旅_基于微服务的在线教育平台尚硅谷-程序员宅基地

文章浏览阅读2.7k次,点赞3次,收藏11次。SpringBoot+Maven+MabatisPlusmaven在新建springboot项目引入RELEASE版本出错maven在新建springboot项目引入RELEASE版本出错maven详解maven就是通过pom.xml中的配置,就能够从仓库获取到想要的jar包。仓库分为:本地仓库、第三方仓库(私服)、中央仓库springframework.boot:spring-boot-starter-parent:2.2.1.RELEASE’ not found若出现jar包下载不了只有两_基于微服务的在线教育平台尚硅谷

随便推点

网络学习第六天(路由器、VLAN)_路由和vlan-程序员宅基地

文章浏览阅读316次。路由的概念路由器它称之为网关设备。路由器就是用于连接不同网络的设备路由器是位于OSI模型的第三层。路由器通过路由决定数据的转发。网关的背景:当时每家计算机厂商,用于交换数据的通信程序(协议)和数据描述格式各不相同。因此,就把用于相互转换这些协议和格式的计算机称为网关。路由器与三层交换器的对比路由协议对比路由器的作用:1.路由寻址2.实现不同网络之间相连的功能3.通过路由决定数据的转发,转发策略称为 路由选择。VLAN相关技术什么是VLAN?中文名称叫:虚拟局域网。虚_路由和vlan

设置div背景颜色透明度,内部元素不透明_div设置透明度,里面的内容不透明-程序员宅基地

文章浏览阅读2.8w次,点赞6次,收藏22次。设置div背景颜色透明度,内部元素不透明:.demo{  background-color:rgba(255,255,255,0.15) } 错误方式:.demo{ background-color:#5CACEE;opacity:0.75;} 这样会导致div里面的元素内容和背景颜色一起变透明只针对谷歌浏览器的测试_div设置透明度,里面的内容不透明

Discuz!代码大全-程序员宅基地

文章浏览阅读563次。1.[ u]文字:在文字的位置可以任意加入您需要的字符,显示为下划线效果。2.[ align=center]文字:在文字的位置可以任意加入您需要的字符,center位置center表示居中,left表示居左,right表示居右。5.[ color=red]文字:输入您的颜色代码,在标签的中间插入文字可以实现文字颜色改变。6.[ SIZE=数字]文字:输入您的字体大小,在标签的中间插入文..._discuzcode 大全

iOS NSTimer定时器-程序员宅基地

文章浏览阅读2.6k次。iOS中定时器有三种,分别是NSTimer、CADisplayLink、dispatch_source,下面就分别对这三种计时器进行说明。一、NSTimerNSTimer这种定时器用的比较多,但是特别需要注意释放问题,如果处理不好很容易引起循环引用问题,造成内存泄漏。1.1 NSTimer的创建NSTimer有两种创建方法。方法一:这种方法虽然创建了NSTimer,但是定时器却没有起作用。这种方式创建的NSTimer,需要加入到NSRunLoop中,有NSRunLoop的驱动才会让定时器跑起来。_ios nstimer

Linux常用命令_ls-lmore-程序员宅基地

文章浏览阅读4.8k次,点赞17次,收藏51次。Linux的命令有几百个,对程序员来说,常用的并不多,考虑各位是初学者,先学习本章节前15个命令就可以了,其它的命令以后用到的时候再学习。1、开机 物理机服务器,按下电源开关,就像windows开机一样。 在VMware中点击“开启此虚拟机”。2、登录 启动完成后,输入用户名和密码,一般情况下,不要用root用户..._ls-lmore

MySQL基础命令_mysql -u user-程序员宅基地

文章浏览阅读4.1k次。1.登录MYSQL系统命令打开DOS命令框shengfen,以管理员的身份运行命令1:mysql -u usernae -p password命令2:mysql -u username -p password -h 需要连接的mysql主机名(localhost本地主机名)或是mysql的ip地址(默认为:127.0.0.1)-P 端口号(默认:3306端口)使用其中任意一个就OK,输入命令后DOS命令框得到mysql>就说明已经进入了mysql系统2. 查看mysql当中的._mysql -u user

推荐文章

热门文章

相关标签