el-table 单元格,双击编辑

[复制链接]
发表于 2026-1-26 23:57:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
el-table 单元格,双击编辑

实现结果


代码如下
  1. <template>
  2.   <el-table :data="tableData" style="width: 100%">
  3.     <el-table-column prop="name" label="姓名" width="180">
  4.       <template slot-scope="scope">
  5.         <div @dblclick="editCell(scope, 'name')" style="cursor: pointer;">
  6.           <span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'name'">
  7.             {{ scope.row.name }}
  8.           </span>
  9.           <el-input
  10.             v-else
  11.             v-model="editingCell.value"
  12.             @blur="saveEdit(scope.row, 'name')"
  13.             @keydown.enter="saveEdit(scope.row, 'name')"
  14.             size="small"
  15.             style="width: 100px;"
  16.           />
  17.         </div>
  18.       </template>
  19.     </el-table-column>
  20.     <el-table-column prop="age" label="年龄" width="180">
  21.       <template slot-scope="scope">
  22.         <div @dblclick="editCell(scope, 'age')" style="cursor: pointer;">
  23.           <span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'age'">
  24.             {{ scope.row.age }}
  25.           </span>
  26.           <el-input
  27.             v-else
  28.             v-model="editingCell.value"
  29.             @blur="saveEdit(scope.row, 'age')"
  30.             @keydown.enter="saveEdit(scope.row, 'age')"
  31.             size="small"
  32.             style="width: 100px;"
  33.           />
  34.         </div>
  35.       </template>
  36.     </el-table-column>
  37.   </el-table>
  38. </template>
  39. <script>
  40. export default {
  41.   data() {
  42.     return {
  43.       tableData: [
  44.         { name: 'John', age: 25 },
  45.         { name: 'Jane', age: 30 },
  46.         { name: 'Doe', age: 28 },
  47.       ],
  48.       isEditingCell: false,
  49.       editingCell: {
  50.         row: null,
  51.         field: null,
  52.         value: null,
  53.       },
  54.     };
  55.   },
  56.   methods: {
  57.     editCell(scope, field) {
  58.       this.isEditingCell = true;
  59.       this.editingCell.row = scope.row;
  60.       this.editingCell.field = field;
  61.       this.editingCell.value = scope.row[field]; // 将原有的值赋给编辑单元格
  62.     },
  63.     saveEdit(row, field) {
  64.       row[field] = this.editingCell.value;  // 将输入的值保存回原数据
  65.       this.isEditingCell = false;
  66.       this.editingCell.row = null;
  67.       this.editingCell.field = null;
  68.       this.editingCell.value = null; // 清空编辑值
  69.     },
  70.   },
  71. };
  72. </script>
  73. <style scoped>
  74. /* 自定义样式 */
  75. </style>
复制代码
假如值为空无法点击无效的话,可以设置<span>的样式 style="display: inline-block ;
  1. <span v-if="!isEditingCell || editingCell.row !== scope.row || editingCell.field !== 'age'" style="display: inline-block ;>
  2.             {{ scope.row.age }}
  3.           </span>
复制代码
如许就不会碰到值为空,但是无法表现的环境了
详细的内容各人可以根据现实必要调解!
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表