element-UI+VUE 实现el-table双击单元格编辑

vue文件代码:

<template>
  <div class="content-wrapper">
    <div class="card-body">

      <el-table height="600px" :data="tableData" border stripe style="width: 100%">

        <el-table-column :show-overflow-tooltip="true" align="center" prop="enddate" label="第一列" width="250">
          <template slot-scope="scope">
            <div @dblclick="changeEnddate(scope.$index,'tag1', scope.row)" style="height: 40px;line-height: 40px;">
              <span v-show="!scope.row.is_show_tag1">{{scope.row.tag1}}</span>
              <el-input :ref='"enddateinput"+"tag1"+"&"+scope.$index' @blur="switchShow(scope.$index,'tag1')"
                        clearable @keyup.enter.native="$event.target.blur" v-show="scope.row.is_show_tag1" size="mini"
                        v-model="scope.row.tag1"></el-input>
            </div>
          </template>
        </el-table-column>

        <el-table-column :show-overflow-tooltip="true" align="center" prop="enddate" label="第二列" width="250">
          <template slot-scope="scope">
            <div @dblclick="changeEnddate(scope.$index,'tag2', scope.row)" style="height: 40px;line-height: 40px;">
              <span v-show="!scope.row.is_show_tag2">{{scope.row.tag2}}</span>
              <el-input :ref='"enddateinput"+"tag2"+"&"+scope.$index' @blur="switchShow(scope.$index,'tag2')" clearable
                        @keyup.enter.native="$event.target.blur" v-show="scope.row.is_show_tag2" size="mini"
                        v-model="scope.row.tag2"></el-input>
            </div>
          </template>
        </el-table-column>

      </el-table>
    </div>
  </div>
</template>
<script>
  export default {
    name: "test2",
    data() {
      return {
        tableData: [{'tag1': '可修改的标签1', 'tag2': '可修改的标签2'}, {'tag1': '可修改的标签3', 'tag2': '可修改的标签4'}],
      };
    },

    methods: {
      // 切换input框的显示状态
      switchShow(index, tag, tmp_this=this){
        // 因为涉及到调用不同名称的变量, 不清楚怎么写, 只能先用switch case解决
        switch (tag) {
          case "tag1":
            tmp_this.tableData[index].is_show_tag1 = !tmp_this.tableData[index].is_show_tag1;
            break;
          case "tag2":
            tmp_this.tableData[index].is_show_tag2 = !tmp_this.tableData[index].is_show_tag2;
            break;
          // ...
        }
        tmp_this.tableData = [...tmp_this.tableData];//因为我table绑定的表格数据是后接过来赋值的,所以需要这步操作,如果没有1、2步骤这个可以不加。后面也一样
      },

      // 显示input框, 使光标焦点当前input框
      changeEnddate(index, tag, rowdata){
        this.switchShow(index, tag, this);
        // console.log('enddateinput' + index, typeof 'enddateinput' + index);
        // console.log(rowdata, typeof rowdata);
        setTimeout( ()=> {  //定时器是为了避免没有获取到dom的情况报错,所以象征性的给1毫秒让他缓冲
          this.$refs['enddateinput' + tag + '&' + index].focus()
          //el-input的autofocus失效,所以用这个方法。对应在template里的refs绑定值
        }, 1)
      },

    }
  }
</script>

最终效果: 能实现双击某个单元格显示input框进而编辑单元格内容, 光标挪出来输入框隐藏, 显示已修改的内容, 如图 :

在这里插入图片描述

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值