el-table 单元格,双击编辑

el-table 单元格,双击编辑

实现效果

代码如下

html 复制代码
<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="name" label="姓名" width="180">
      <template slot-scope="scope">
        <div @dblclick="editCell(scope, 'name')" style="cursor: pointer;">
          <span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'name'">
            {{ scope.row.name }}
          </span>
          <el-input
            v-else
            v-model="editingCell.value"
            @blur="saveEdit(scope.row, 'name')"
            @keydown.enter="saveEdit(scope.row, 'name')"
            size="small"
            style="width: 100px;"
          />
        </div>
      </template>
    </el-table-column>

    <el-table-column prop="age" label="年龄" width="180">
      <template slot-scope="scope">
        <div @dblclick="editCell(scope, 'age')" style="cursor: pointer;">
          <span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'age'">
            {{ scope.row.age }}
          </span>
          <el-input
            v-else
            v-model="editingCell.value"
            @blur="saveEdit(scope.row, 'age')"
            @keydown.enter="saveEdit(scope.row, 'age')"
            size="small"
            style="width: 100px;"
          />
        </div>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: 'John', age: 25 },
        { name: 'Jane', age: 30 },
        { name: 'Doe', age: 28 },
      ],
      isEditingCell: false,
      editingCell: {
        row: null,
        field: null,
        value: null,
      },
    };
  },
  methods: {
    editCell(scope, field) {
      this.isEditingCell = true;
      this.editingCell.row = scope.row;
      this.editingCell.field = field;
      this.editingCell.value = scope.row[field]; // 将原有的值赋给编辑单元格
    },
    saveEdit(row, field) {
      row[field] = this.editingCell.value;  // 将输入的值保存回原数据
      this.isEditingCell = false;
      this.editingCell.row = null;
      this.editingCell.field = null;
      this.editingCell.value = null; // 清空编辑值
    },
  },
};
</script>

<style scoped>
/* 自定义样式 */
</style>

如果值为空无法点击无效的话,可以设置<span>的样式 style="display: inline-block ;

html 复制代码
<span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'age'" style="display: inline-block ;>
            {{ scope.row.age }}
          </span>

这样就不会遇到值为空,但是无法显示的情况了

具体的内容大家可以根据实际需要调整!

相关推荐
南山安17 分钟前
Tailwind CSS:顺风CSS
javascript·css·react.js
期待のcode1 小时前
@RequestBody的伪表单提交场景
java·前端·vue.js·后端
栀秋6661 小时前
防抖 vs 节流:从百度搜索到京东电商,看前端性能优化的“节奏哲学”
前端·javascript
一颗烂土豆1 小时前
vfit.js v2.0.0 发布:精简、语义化与核心重构 🎉
前端·vue.js·响应式设计
有意义1 小时前
深入防抖与节流:从闭包原理到性能优化实战
前端·javascript·面试
2503_928411562 小时前
12.26 小程序问题和解决
前端·javascript·微信小程序·小程序
总之就是非常可爱2 小时前
vue3 KeepAlive 核心原理和渲染更新流程
前端·vue.js·面试
over6972 小时前
防抖与节流:前端性能优化的“双子星”,让你的网页丝滑如德芙!
前端·javascript·面试
red润2 小时前
手把手封装Iframe父子单向双向通讯功能
前端·javascript·vue.js
gustt2 小时前
JavaScript 闭包实战:手写防抖与节流函数,优化高频事件性能
前端·javascript·面试