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

news/2025/2/8 20:02:38 标签: vue.js, elementui, javascript, 前端

 

  1. 使用 el-table 和 el-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>


http://www.niftyadmin.cn/n/5845249.html

相关文章

qt6.8安装mysql8.0驱动

qt6.8安装mysql8.0驱动 qt6.8本身是不带mysql驱动。想要在qt里面使用mysql,还是比较麻烦的。需要自己编译驱动 首先下载qt源码&#xff0c;链接Index of /archive/qt/6.8/6.8.1/single 下载mysql对于驱动文件&#xff0c;链接是MySQL :: Download MySQL Connector/C (Archiv…

数据结构-基础

1、概念&#xff1a; 程序 数据结构 算法 2、程序的好坏 可读性&#xff0c;稳定性&#xff0c;扩展性&#xff0c;时间复杂度&#xff0c;空间复杂度。 3、数据结构 是指存储、组织数据的方式&#xff0c;以便高效地进行访问和修改。通过选择适当的数据结构&#xff0c; 能…

基于 llama-Factory 动手实践 Llama 全参数 SFT 和 LoRA SFT

一、llama-Factory&#xff1a;你的 Llama 模型 SFT 工厂 llama-Factory 是一个开源的、用户友好的工具&#xff0c;专门用于对 Llama 系列模型进行微调。它提供了简洁的界面和强大的功能&#xff0c;让你无需复杂的代码编写&#xff0c;就能轻松完成 Llama 模型的 SFT 任务&a…

【DeepSeek论文精读】3. DeepSeekMoE:迈向混合专家语言模型的终极专业化

欢迎关注[【AIGC论文精读】](https://blog.csdn.net/youcans/category_12321605.html&#xff09;原创作品 【DeepSeek论文精读】1. 从 DeepSeek LLM 到 DeepSeek R1 【DeepSeek论文精读】2. DeepSeek LLM&#xff1a;以长期主义扩展开源语言模型 【DeepSeek论文精读】3. DeepS…

使用DeepSeek的技巧笔记

来源&#xff1a;新年逼自己一把&#xff0c;学会使用DeepSeek R1_哔哩哔哩_bilibili 前言 对于DeepSeek而言&#xff0c;我们不再需要那么多的提示词技巧&#xff0c;但还是要有两个注意点&#xff1a;你需要理解大语言模型的工作原理与局限,这能帮助你更好的知道AI可完成任务…

Office/WPS接入DS等多个AI工具,开启办公新模式!

在现代职场中&#xff0c;Office办公套件已成为工作和学习的必备工具&#xff0c;其功能强大但复杂&#xff0c;熟练掌握需要系统的学习。为了简化操作&#xff0c;使每个人都能轻松使用各种功能&#xff0c;市场上涌现出各类办公插件。这些插件不仅提升了用户体验&#xff0c;…

全志A133 android10 thermal温控策略配置调试

一&#xff0c;功能介绍 Thermal简称热控制系统&#xff0c;其功能是通过temperature sensor&#xff08;温度传感器&#xff09;测量当前CPU、GPU等设备的温度值&#xff0c;然后根据此温度值&#xff0c;影响CPU、GPU等设备的调频策略&#xff0c;对CPU、GPU等设备的最大频率…

Python----Python高级(并发编程:协程Coroutines,事件循环,Task对象,协程间通信,协程同步,将协程分布到线程池/进程池中)

一、协程 1.1、协程 协程&#xff0c;Coroutines&#xff0c;也叫作纤程(Fiber) 协程&#xff0c;全称是“协同程序”&#xff0c;用来实现任务协作。是一种在线程中&#xff0c;比线程更加轻量级的存在&#xff0c;由程序员自己写程序来管理。 当出现IO阻塞时&#xff0c;…