若依 芋道 ruoyi-vue-pro 分离版 vue3 简单的行内编辑实现

最终实现:列表内容双击、行内编辑、控件失焦提交接口修改

一、简单实现: 有bug,双击后文本框获取不到焦点

  1. VUE相关代码:
html 复制代码
<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-table :data="list" :show-overflow-tooltip="true" :stripe="true">



      <el-table-column label="流程备注" align="center" width="200">
        <template #default="scope">
          <span v-if="!scope.row.isEditing" @dblclick="startEditing(scope)" > {{ scope.row.name }} </span>
          <el-input v-else v-model="scope.row.name" @keyup.enter="stopEditing(scope)" @blur="stopEditing(scope)" />
        </template>
      </el-table-column>



    </el-table>
  </ContentWrap>
</template>
javascript 复制代码
<script lang="ts" setup>
// 获取列表数据示例:
const list = ref([]) // 列表数据(接口获取)
/** 接口查询列表数据 */
const getList = async () => {
  try {
      //查询列表数据 示例
      const data = await ContractApi.getContractPage(queryParams)
      list.value = data.list
  } finally {
    
  }
}



// 以下是行内编辑相关


let lastEditValue = "" //双击行内编辑文本内容(记录并用于判断提交时是否执行接口提交)
/** 双击文本,进入编辑状态 **/
const startEditing = (scope) => {
  lastEditValue = scope.row.name //双击行内编辑文本内容
  scope.row.isEditing = true
}
/** 回车键 或 编辑框失去焦点(如:点击编辑框外任意点),提交修改内容到接口 **/
const stopEditing = (scope) => {
  scope.row.isEditing = false
  // 行内编辑前后内容不同,提交修改后数据
  if(lastEditValue.trim() !== scope.row.name?.trim()){
    alert(scope.row.name?.trim())
    // 这里可以添加保存或其他逻辑
  }
}


</script>
  1. 后端代码:分页查询的集合对象中,新增属性 isEditing 用于标记是否是双击编辑状态
java 复制代码
/** 是否编辑状态(配合行内编辑使用):默认 否 **/
    private boolean isEditing = false;

二、双击后自动设置焦点全版本:

  1. VUE相关代码:
html 复制代码
<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-table :data="list" :show-overflow-tooltip="true" :stripe="true">



      <el-table-column label="流程备注" align="center" width="200">
        <template #default="{ row, $index }">
          <span v-if="!row.isEditing" @dblclick="startEditing(row, $index)" > {{ row.name }} </span>
          <el-input v-else :ref="(el) => setInputRef(el, $index)" v-model="row.name" @keyup.enter="stopEditing(row, $index)" @blur="stopEditing(row, $index)" />
        </template>
      </el-table-column>



    </el-table>
  </ContentWrap>
</template>





<script lang="ts" setup>
import { ref, nextTick } from 'vue' //双击行内编辑聚集相关


// 获取列表数据示例:
const list = ref([]) // 列表数据(接口获取)
/** 接口查询列表数据 */
const getList = async () => {
  try {
      //查询列表数据 示例
      const data = await ContractApi.getContractPage(queryParams)
      list.value = data.list
  } finally {
    
  }
}



// 以下是行内编辑相关



// 保存每个输入框的引用(按行索引)
const inputRefs = ref([])
const setInputRef = (el, index) => {
  if (el) {
    inputRefs.value[index] = el
  }
}

/** 双击文本,进入编辑状态 **/
const startEditing = async (row, index) => {
  row._oldName = row.name // 存储旧值到行对象:双击行内编辑文本内容(记录并用于判断提交时是否执行接口提交)
  row.isEditing = true //编辑中
  // 等待 DOM 更新后聚焦
  await nextTick()
  // 获取对应行的 el-input 组件实例
  const inputInstance = inputRefs.value[index]
  if (inputInstance) {
    // 方法1:直接调用组件暴露的 focus
    inputInstance.focus?.()
    // 如果无效,降级到原生 input(但通常 focus 方法已足够)
    // inputInstance.$el.querySelector('input')?.focus()
  }
}
/** 回车键 或 编辑框失去焦点(如:点击编辑框外任意点),提交修改内容到接口 **/
const stopEditing = async (row, index) => {
  row.isEditing = false
  const oldName = row._oldName
  // 行内编辑前后内容不同,提交修改后数据
  if(oldName.trim() !== row.name?.trim()){
    try {
      await ContractApi.updateInvoice(row.id, row.name) // 发起修改
      delete row._oldName // 清除临时数据
      // await getList() // 刷新列表
    } catch {
      // await getList()
      row.name = oldName// 恢复原值
    }
  }
}


</script>
  1. 后端代码:分页查询的集合对象中,新增属性 isEditing 用于标记是否是双击编辑状态
java 复制代码
/** 是否编辑状态(配合行内编辑使用):默认 否 **/
    private boolean isEditing = false;
相关推荐
星栈4 小时前
我在浏览器里试了试 PhotoCraft|Rust + WASM 修图,是玩具还是生产力?
前端
雨中飘荡的记忆5 小时前
淘宝搜不到的东西,能不能去全世界找?一个程序员的创业尝试
前端·设计
kyriewen5 小时前
我让 4 位 AI 面试官打分,同一个我差了 40 分
前端·面试·程序员
htzyl2066 小时前
Vue3中watch的deep默认值:reactive与ref为什么相反
前端·vue.js
用户112936112197 小时前
没写样式为什么还找我要 wxss?uni-app 这个 ENOENT 坑多数人误判了
前端·微信小程序·uni-app
用户553665935267 小时前
从零做一个全国山峰与线路库:山野抽签的初衷、架构与踩坑
前端
夕水7 小时前
用 SolidJS 复刻 DeepSeek Chat —— 顺便把 ew-solid-ui 拷打了一遍
前端·javascript
qq_274475157 小时前
第 17 篇:ES6 语法速览:解构赋值、展开运算符、模板字符串、对象简写
前端·ecmascript·es6
凯程序猿1号9 小时前
Web墨卡托EPSG3857瓦片行列号XYZ与经纬度双向换算及地面分辨率推导
前端·电脑
qq_274475159 小时前
第 21 篇:Proxy 代理与 Reflect:数据劫持基础,Vue 响应式底层原理前置
前端·javascript·vue.js