当前位置: 代码网 > it编程>前端脚本>Vue.js > Vue ElementUI table实现双击修改编辑某个内容的方法

Vue ElementUI table实现双击修改编辑某个内容的方法

2024年10月28日 Vue.js 我要评论
1、使用@cell-dblclick事件,当双击时触发事件<el-table @cell-dblclick="handlecelldblclick"2、单元格设置主要重点为判断双击时切换inpu

1、使用@cell-dblclick事件,当双击时触发事件

<el-table @cell-dblclick="handlecelldblclick"

2、单元格设置

主要重点为判断双击时切换input框,然后绑定ref,设置失去焦点时触发点方法,与按enter键触发点方法

<el-table-column prop="name" label="姓名" width="180">
      <template slot-scope="scope">
        <span v-if="editabledata !== scope.row">{{ scope.row.name }}</span>
        <el-input
          v-else
          :ref="'input-' + scope.$index"
          v-model="scope.row.name"
          @blur="handleinputblur(scope.row)"
          @keyup.enter.native="handleinputenter(scope.row)"
        ></el-input>
      </template>
    </el-table-column>

3、添加当前编辑的数据

editabledata: null, // 当前编辑的数据项

4、为所有的方法赋予逻辑

// 双击时触发
handlecelldblclick(row, column, cell, event) {
  if (column.property === 'customerboxnum') {
    this.editabledata = row; // 设置当前编辑的数据项
    this.$nexttick(() => {
      const inputref = 'input-' + this.boxlist.indexof(row);
      const inputelement = this.$refs[inputref];
      if (inputelement) {
        inputelement.focus(); // 聚焦输入框
      } else {
        console.error('input element not found:', inputref);
      }
    });
  }
},
handleinputblur(row) {
  // 输入框失去焦点时保存更改
  this.editabledata = null; // 返回到静态显示状态
},
handleinputenter(row) {
  // 按下回车键时保存更改
  this.editabledata = null; // 返回到静态显示状态
},

5、打完收工

到此这篇关于vueelementui table实现双击修改编辑某个内容的方法的文章就介绍到这了,更多相关vue elementui table双击修改内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com