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
        }
    }
相关推荐
洋葱头_1 小时前
vue3项目不支持低版本的android,如何做兼容
前端·vue.js
奔跑的蜗牛ing2 小时前
Vue3 + Element Plus 输入框省略号插件:零侵入式全局解决方案
vue.js·typescript·前端工程化
ygria2 小时前
样式工程化:如何实现Design System
前端·前端框架·前端工程化
最后一个农民工4 小时前
vue3实现仿豆包模版式智能输入框
前端·vue.js
艾小码7 小时前
Vue组件到底怎么定义?全局注册和局部注册,我踩过的坑你别再踩了!
前端·javascript·vue.js
wusp19947 小时前
项目实战——“微商城”前后台【005】之前台项目首页编写
vue.js·vant·底部导航栏
森林的尽头是阳光8 小时前
vue防抖节流,全局定义,使用
前端·javascript·vue.js
计算机毕业设计木哥8 小时前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
java·vue.js·spring boot·mysql·spark·毕业设计·课程设计
zero13_小葵司8 小时前
Vue 3 前端工程化规范
前端·javascript·vue.js
Yolanda_20228 小时前
vue-sync修饰符解析以及切换iframe页面进行保存提示功能的思路
前端·javascript·vue.js