Vue实现点击表格单元格出现输入框,失去焦点隐藏输入框功能

18 篇文章 1 订阅
13 篇文章 0 订阅

Vue实现点击表格单元格出现输入框,失去焦点隐藏输入框功能

效果如下:
在这里插入图片描述

代码如下:

<template>
  <div>
     <el-table
      :data="tableData"
      row-key="id"
      @cell-click="editCell" 
      style="width: 700px;">
      <el-table-column align="center" label="日期" show-overflow-tooltip type="date">
        <template slot-scope="scope">
          <el-input
            :ref="`date-${scope.row.id}`"
            v-model="scope.row.date" 
            v-show="scope.row.id === tabClickId && tabClickLabel === '日期'" placeholder="日期" 
            @blur="inputBlur(scope.row)">
          </el-input>
          <div class="cell-text" v-show="scope.row.id !== tabClickId || tabClickLabel !== '日期'">{{ scope.row.date }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="姓名" show-overflow-tooltip type="name">
        <template slot-scope="scope">
          <el-input
            :ref="`name-${scope.row.id}`"
            v-model="scope.row.name" 
            v-show="scope.row.id === tabClickId && tabClickLabel === '姓名'" placeholder="姓名" 
            @blur="inputBlur(scope.row)">
          </el-input>
          <div class="cell-text" v-show="scope.row.id !== tabClickId || tabClickLabel !== '姓名'">{{ scope.row.name }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="地址" show-overflow-tooltip type="address">
        <template slot-scope="scope">
          <el-input
            :ref="`address-${scope.row.id}`"
            v-model="scope.row.address" 
            v-show="scope.row.id === tabClickId && tabClickLabel === '地址'" placeholder="地址" 
            @blur="inputBlur(scope.row)">
          </el-input>
          <div class="cell-text" v-show="scope.row.id !== tabClickId || tabClickLabel !== '地址'">{{ scope.row.address }}</div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script type="text/javascript">
export default {
    mounted() {
    },
    data() {
        return {
          tableData: [{
            id: 1,
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            id: 2,
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1517 弄'
          }, {
            id: 3,
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1519 弄'
          }, {
            id: 4,
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1516 弄'
          }],
          tabClickId: '',
          tabClickLabel: ''
        }
    },
    methods: {
      editCell(item, column, cell, event){
        //根据点击的单元格判断是否可变成输入框
        switch (column.label) {
          case '日期': //当为日期时不变成输入框
            return
          default: 
            this.tabClickId = item.id
            this.tabClickLabel = column.label
            break
        }
        //输入框默认获取焦点
        this.$nextTick(() => {
          this.$refs[column.type + '-' + item.id].focus();
        })
      },
      // 失去焦点初始化
      inputBlur(item) {
        this.tabClickId = "";
        this.tabClickLabel = "";
        //这里还可以进行其他数据提交等操作
      }
    }
}
</script>
  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值