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

相关推荐
karshey3 分钟前
【vue】NoticeBar:滚动通知栏组件手动实现(内容、速度、循环间隔可配置)
前端·javascript·vue.js
醉方休22 分钟前
React 官方推荐使用 Vite
前端·react.js·前端框架
花菜会噎住24 分钟前
Vue3 路由配置和使用与讲解(超级详细)
开发语言·javascript·ecmascript·路由·router
细节控菜鸡27 分钟前
【2025最新】ArcGIS for JavaScript 快速实现热力图渲染
开发语言·javascript·arcgis
Dontla27 分钟前
React惰性初始化函数(Lazy Initializer)(首次渲染时执行一次,只执行一次,应对昂贵初始化逻辑)(传入一个函数、传入函数)
前端·javascript·react.js
lypzcgf36 分钟前
FastbuildAI新建套餐-前端代码分析
前端·智能体平台·ai应用平台·agent平台·fastbuildai
南囝coding1 小时前
Claude Code 插件系统来了
前端·后端·程序员
摇滚侠1 小时前
Spring Boot 3零基础教程,WEB 开发 默认的自动配置,笔记25
前端·spring boot·笔记
Cherry Zack1 小时前
Vue Router 路由管理完全指南:从入门到精通前言
前端·javascript·vue.js
亮子AI2 小时前
【npm】npm install 产生软件包冲突怎么办?(详细步骤)
前端·npm·node.js