若依 ruoyi 分离版 vue 简单的行内编辑实现

需要实现的效果:双击文本 - 修改文本 - 保存修改。

原码:仅文本显示文字内容

html 复制代码
<el-table-column label="商品" align="center" prop="goodsName" width="200" v-if="columns[1].visible" />

实现双击文本、修改文本:

在上面源码基础上进行编辑,新增如下

修改后代码:

html 复制代码
      <el-table-column label="商品" align="center" prop="goodsName" width="200" v-if="columns[1].visible">
        <template slot-scope="scope">
          <span v-if="!scope.row.isEditing" @dblclick="startEditing(scope.$index, scope.row)">{{scope.row.goodsName}}</span>
          <span v-else><el-input v-model="scope.row.goodsName" @blur="stopEditing(scope.$index, scope.row)"/></span>
        </template>
      </el-table-column>

行内文本框的双击事件、失去焦点事件:

javascript 复制代码
    startEditing(index, row) {
      // 启用编辑模式:设置当前行的isEditing属性值为true,使用 this.$set 同步更新视图为文本框
      this.$set(row, 'isEditing', true);
    },
    stopEditing(index, row) {
      // 禁用编辑模式:设置当前行的isEditing属性值为false,使用 this.$set 同步更新视图为文本
      this.$set(row, 'isEditing', false);
      console.info(row);
      console.info(row.id);
      console.info(row.goodsId);
      console.info(row.goodsName);
      // 这里可以添加保存或其他逻辑
      // 调用接口,更新数据

    }

后端数据集合对象中,新增属性 isEditing

总体参考代码:

html 复制代码
<template>  
  <el-table :data="tableData">  
    <el-table-column label="商品" align="center" width="200">  
      <template slot-scope="scope">  
        <span  
          v-if="!scope.row.isEditing"  
          @dblclick="startEditing(scope.$index, scope.row)"  
        >  
          {{ scope.row.goodsName }}  
        </span>  
        <el-input  
          v-else  
          v-model="scope.row.goodsName"  
          @blur="stopEditing(scope.$index, scope.row)"  
        />  
      </template>  
    </el-table-column>  
    <!-- 其他列... -->  
  </el-table>  
</template>  
  
<script>  
export default {  
  data() {  
    return {  
      tableData: [  
        { goodsName: '商品1', isEditing: false },  
        { goodsName: '商品2', isEditing: false },  
        // ... 其他数据  
      ],  
    };  
  },  
  methods: {  
    startEditing(index, row) {  
      this.$set(row, 'isEditing', true); // 启用编辑模式  
    },  
    stopEditing(index, row) {  
      this.$set(row, 'isEditing', false); // 禁用编辑模式  
      // 这里可以添加保存或其他逻辑  
    },  
  },  
};  
</script>

其他

  1. 想要一体版的,看这里 https://blog.csdn.net/torpidcat/article/details/101369733

  2. vue-ele-editable 适用原生vue

https://github.com/dream2023/vue-ele-editable

相关推荐
赖龙2 小时前
pnpm vs npm
前端·npm·node.js
To_OC2 小时前
LC 239 滑动窗口最大值:从暴力超时到单调队列一遍过
javascript·算法·leetcode
学如逆水,不进则退3 小时前
在线免费音频剪辑:NBTools 可视化波形裁剪 MP3/WAV,本地处理免上传
前端·音视频
Mh3 小时前
虚拟滚动真的比普通滚动性能更好吗?
前端·javascript·性能优化
pe7er3 小时前
React + Ant Design 中的 IME (输入法合成)安全输入组件
前端
Super 含4 小时前
Android 启动优化(二):TTID、TTFD 与 Macrobenchmark 启动性能测量
前端
Python私教4 小时前
别急着加 llms.txt:企业官网面向 AI 搜索的工程清单
前端·人工智能·seo
东方小月4 小时前
从零开发一个 Coding Agent(十三):实现安全的 read 文件读取工具
前端·人工智能·全栈
东方小月4 小时前
从零开发一个 Coding Agent(十二):实现版本化 JSONL 与真实 CLI 入口
前端·设计模式·前端框架
FL16238631296 小时前
室内易燃物识别易燃评估室内易燃程度识别分割数据集labelme格式1015张85类别
java·服务器·前端