ElementUI table表格组件实现双击编辑单元格失去焦点还原,支持多单元格

在使用ElementUI table表格组件时有时需要双击单元格显示编辑状态,失去焦点时还原表格显示。

实现思路:

  • 在数据中增加isFocus:false.控制是否显示
  • 在table中用@cell-dblclick双击方法

先看效果:

上源码:在表格模板中用scope.row.isFocus && focusLabelName=='姓名1控制多个单元格显示

javascript 复制代码
<el-table :data="tableData" border stripe style="width: 100%"  @cell-dblclick="tabClick">
    <el-table-column prop="date" label="Product Name" width="180"></el-table-column>
    
    <el-table-column prop="address" label="地址"></el-table-column>

    <el-table-column prop="price" label="姓名1"  width="180">
        <template slot-scope="scope">
            <el-input v-if="scope.row.isFocus && focusLabelName=='姓名1'" v-focus size="small" v-model="scope.row.price" @change="changeTrafOrigTaxAmount(scope.row)" @blur="blurInput(scope.row)"></el-input>
            <span v-else>{{scope.row.price}}</span>
        </template>
    </el-table-column>

    <el-table-column prop="price" label="姓名2"  width="180">
        <template slot-scope="scope">
            <el-input v-if="scope.row.isFocus && focusLabelName=='姓名2'" v-focus size="small" v-model="scope.row.price2" @change="changeTrafOrigTaxAmount(scope.row)" @blur="blurInput(scope.row)"></el-input>
            <span v-else>{{scope.row.price2}}</span>
        </template>
    </el-table-column>
</el-table>

方法:

javascript 复制代码
data: function () {
        return {
            tableData: [{
                date: '2016-05-02',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1518 弄',
                price: 1000,
                price2: 1000,
                price3: 1000,
                isTransfer: true,
                rate: 0.3,
                amount: 1000,
                isFocus: false,
              }, {
                date: '2016-05-04',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1517 弄',
                price: 1000,
                isTransfer: false,
                rate: 0.3,
                amount: 1000,
                isFocus: false,
              }, {
                date: '2016-05-01',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1519 弄',
                price: 1000,
                price2: 1000,
                price3: 1000,
                isTransfer: true,
                rate: 0.3,
                amount: 1000,
                isFocus: false,
              }, {
                date: '2016-05-03',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1516 弄',
                price: 1000,
                price2: 1000,
                price3: 1000,
                isTransfer: false,
                rate: 0.3,
                amount: 1000,
                isFocus: false,
            }],
            focusLabelName:''
        }
    },
    methods: {
        tabClick(row, column, cell, event)
        {
            console.log(row, column, cell);
            row.isFocus = true;
            this.focusLabelName = column.label;
            
        },
        blurInput(row)
        {
            row.isFocus = false
        }
    }
相关推荐
TrisighT16 小时前
uni-app鸿蒙原生应用开发实战(下):核心功能实现与技术细节
vue.js·harmonyos
无心使然17 小时前
Openlayers图层按需分层渲染到不同Canvas画布
前端·vue.js·gis
daols8817 小时前
vxe-table 实现 Excel 风格向下复制填充(Ctrl + D 键)
javascript·vue.js·excel·vxe-table·vxe-ui
fxshy17 小时前
Vue 组件中 padding 生效了,但竖线还是贴到底边的问题
javascript·vue.js·ecmascript
Aotman_17 小时前
JavaScript数组对象中指定字段转换
java·开发语言·前端·javascript·vue.js·前端框架·es6
姓蔡小朋友17 小时前
React基础
前端·react.js·前端框架
源码宝18 小时前
基于SpringBoot+Vue+小程序+Android的智慧校园电子班牌系统源码示例
vue.js·spring boot·架构·智慧校园·电子班牌·源码·代码
_xaboy18 小时前
开源AI表单设计器 FcDesigner v3.5 版本发布!
前端·vue.js·低代码·开源·表单
段ヤシ.18 小时前
【Java框架】知识点汇总Day7:Spring Boot +Vue(持续更新)
vue.js·spring boot·后端·框架
风之舞_yjf18 小时前
Vue基础(33)_web Storage(web存储)
前端·javascript·vue.js