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>

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

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

相关推荐
前端Hardy13 分钟前
HTML&CSS: 谁懂啊!用代码 “擦去”图片雾气
前端·javascript·css
前端Hardy16 分钟前
HTML&CSS:好精致的导航栏
前端·javascript·css
天下无贼27 分钟前
【手写组件】 Vue3 + Uniapp 手写一个高颜值日历组件(含跨月补全+今日高亮+选中状态)
前端·vue.js
一个不爱写代码的瘦子1 小时前
迭代器和生成器
前端·javascript
洋葱头_2 小时前
vue3项目不支持低版本的android,如何做兼容
前端·vue.js
奔跑的蜗牛ing2 小时前
Vue3 + Element Plus 输入框省略号插件:零侵入式全局解决方案
vue.js·typescript·前端工程化
源猿人4 小时前
企业级文件浏览系统的Vue实现:架构设计与最佳实践
前端·javascript·数据可视化
最后一个农民工4 小时前
vue3实现仿豆包模版式智能输入框
前端·vue.js
RoyLin4 小时前
TypeScript设计模式:迭代器模式
javascript·后端·node.js
小桥风满袖6 小时前
极简三分钟ES6 - ES9中for await of
前端·javascript