若依 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

相关推荐
阿豪只会阿巴13 分钟前
【没事学点啥】TurboBlog轻量级个人博客项目——项目介绍
javascript·python·django·html
Lee川19 分钟前
面试手写 KeepAlive:React 组件缓存的实现原理
前端·react.js·面试
墨染天姬40 分钟前
【AI】cursor提示词小技巧
前端·数据库·人工智能
烛阴44 分钟前
TEngine 入门系列(一):TEngine 是什么 & 为什么选它
前端·unity3d
转转技术团队1 小时前
WebNN:让 AI 推理在浏览器中“零距离”运行
前端
刀法如飞1 小时前
TypeScript 数组去重的 20 种实现方式,哪一种你还不知道?
前端·javascript·算法
IT_陈寒1 小时前
Vite热更新失效?你可能漏了这个小细节
前端·人工智能·后端
海石2 小时前
面试官:说一下你现在使用的 AI IDE,什么,JoyCode 是什么?
前端·ai编程
彩票管理中心秘书长2 小时前
一次搞懂:在Vue里用Showdown渲染Markdown+KaTeX数学公式
前端
m0_738120722 小时前
应急响应(重点)——记一次某公司流量应急溯源分析(附带下载链接)
服务器·前端·数据库·安全·web安全·网络安全