el-table表格点击单元格实现编辑

  1. 使用 el-tableel-table-column 创建表格。
  2. 在单元格的默认插槽中,使用 div 显示文本内容,单击时触发编辑功能。
  3. 使用 el-input 组件在单元格中显示编辑框。
  4. data() 方法中定义了 tableData,tabClickIndex: null,tabClickLabel: '',用于判断是否处于编辑转态
  5. @cell-click="tabClick" 方法用于将单元格设置为编辑状态,并使用 this.$nextTick 来确保输入框能获得焦点。
  6. 通过blur监听失去焦点。
javascript 复制代码
<template>
  <div>
    <el-table :data="dataDetail" :row-class-name="tableRowClassName" border style="width: 100%" max-height="500px" @cell-click="tabClick">
      <el-table-column prop="test1" label="test1" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.index === tabClickIndex && tabClickLabel === 'test1'">
            <el-input :ref="'test1'+scope.row.id" v-model="scope.row.test1" maxlength="300" placeholder="请输入test1" size="mini" @blur="inputBlur" />
          </span>
          <span v-else>{
  
  { scope.row.test1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="test2" label="test2" width="120px" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.index === tabClickIndex && tabClickLabel === 'test2'">
            <el-input :ref="'test2'+scope.row.id" v-model="scope.row.test2" :blur="inputBlur" placeholder="请输入test2" size="mini" @blur="inputBlur" />
          </span>
          <span v-else>{
  
  { scope.row.test2 | fmoney }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="test3" label="test3" align="center" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.index === tabClickIndex && tabClickLabel === 'test3'">
            <el-input :ref="'test3'+scope.row.id" v-model="scope.row.test3" maxlength="300" placeholder="请输入test3" size="mini" @blur="inputBlur" />
          </span>
          <span v-else>{
  
  { scope.row.test3 }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>

</template>

<script>
export default {
  data () {
    return {
      tabClickIndex: null, // 点击的单元格
      tabClickLabel: '', // 当前点击的列名
      dataDetail: [{ id: '123', test1: 'sss' }, { id: '456', test1: 'sss' }]
    }
  },
  methods: {
    tableRowClassName ({ row, rowIndex }) {
  
      row.index = rowIndex
    },
   
    tabClick (row, column, cell, event) {
      switch (column.label) {
        case 'test1':
          this.tabClickIndex = row.index
          this.tabClickLabel = column.label
          break
        case 'test2':
          this.tabClickIndex = row.index
          this.tabClickLabel = column.label
          break
        case 'test3':
          this.tabClickIndex = row.index
          this.tabClickLabel = column.label
          break
        default: return
      }
      const key = this.tabClickLabel + row.id
      this.$nextTick(() => {
        this.$refs[key].focus()
      })
    },
  
    inputBlur (row, event, column) {
      this.tabClickIndex = null
      this.tabClickLabel = ''
    },
  }
}
</script>

<style>
</style>
相关推荐
Profile排查笔记2 小时前
指纹浏览器环境异常排查:Fingerprint、Profile、Proxy、Session 和 Task Log 怎么看
前端·人工智能·后端·自动化
京韵养生记2 小时前
【无标题】
java·服务器·前端
格子软件2 小时前
2026年分布式GEO代理流量调度:源码级状态机防重挂实战
java·vue.js·人工智能·spring boot·分布式·vue
大气的小蜜蜂3 小时前
领域层的服务
java·前端·数据库
星栈3 小时前
LiveView 的 LiveComponent:比 React 组件更轻,但我一开始真的把它用错了
前端·前端框架·elixir
竹林8183 小时前
用 Pinata + IPFS 存 NFT 元数据踩了三天坑,我总结了这份完整的前端实现方案
javascript
林希_Rachel_傻希希3 小时前
web性能优化之延迟加载图片和<inframe>
前端·javascript·面试
maxmaxma3 小时前
Konva 从入门到实践 - day1
前端
火星校尉3 小时前
一场数据基建与消费场景的跨界实验
java·前端·数据库·python·php
W是笔名3 小时前
python_let`s try it 6___BMI计算器
java·前端·python