实现表格双击后变输入框

双击后效果,本次需求我的代码中始终只会保持一个输入框 每次都会执行stopEditing 函数
还有一种实现方式就是在tableData2中的每一个对象中加一个布尔值,然后失焦、聚焦的时候 直接修改这个值显示

javascript 复制代码
    <!-- 编辑测量数据弹框 -->
    <Dialog title="编辑测量数据" width="1200px" :visible.sync="editMeasureShow" append-to-body v-dialogDrag>
      <el-table
        :data="tableData2"
        class="tableData2_class"
        border
        stripe
        :header-cell-style="{
          background: '#f1f3f5',
          color: '#000000'
        }"
      >
       
        <el-table-column align="center" prop="weight" label="重量(KG)">
          <template v-slot="{ row }">
            <div
              style="width: 100%; cursor: pointer"
              @dblclick="startEditing(row, 'weight')"
              v-if="!row.isEditingWeight"
            >
              {{ row.weight || '--' }}
            </div>
            <el-input
              v-else
              ref="weightInput"
              v-model="row.weight"
              @blur="stopEditing(row, 'weight')"
              @keyup.enter="stopEditing(row, 'weight')"
              type="number"
              size="mini"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="length" label="长(CM)">
          <template v-slot="{ row }">
            <div
              style="width: 100%; cursor: pointer"
              @dblclick="startEditing(row, 'length')"
              v-if="!row.isEditingLength"
            >
              {{ row.length || '--' }}
            </div>
            <el-input
              v-else
              ref="lengthInput"
              v-model="row.length"
              @blur="stopEditing(row, 'length')"
              @keyup.enter="stopEditing(row, 'length')"
              type="number"
              size="mini"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="width" label="宽(CM)">
          <template v-slot="{ row }">
            <div style="width: 100%; cursor: pointer" @dblclick="startEditing(row, 'width')" v-if="!row.isEditingWidth">
              {{ row.width || '--' }}
            </div>
            <el-input
              v-else
              ref="widthInput"
              v-model="row.width"
              @blur="stopEditing(row, 'width')"
              @keyup.enter="stopEditing(row, 'width')"
              type="number"
              size="mini"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="height" label="高(CM)">
          <template v-slot="{ row }">
            <div
              style="width: 100%; cursor: pointer"
              @dblclick="startEditing(row, 'height')"
              v-if="!row.isEditingHeight"
            >
              {{ row.height || '--' }}
            </div>
            <el-input
              v-else
              ref="heightInput"
              v-model="row.height"
              @blur="stopEditing(row, 'height')"
              @keyup.enter="stopEditing(row, 'height')"
              type="number"
              size="mini"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editMeasureShow = false">取消</el-button>
        <el-button type="primary" @click="editMeasureDataSubmit">确认</el-button>
      </span>
    </Dialog>
javascript 复制代码
 methods: {
   stopEditing (row, field) {
      // 假设 传递 weight
      // field.charAt(0).toUpperCase() + field.slice(1)
      // 这个位置组成  field.charAt(0).toUpperCase()  // W
      // 这个位置组成  field.slice(1)  // eight
      //  我是为了规范驼峰 对应这里的控制  v-if="!row.isEditingWeight"
      // 不想的话直接这样 `isEditing${field}`  对应的if 也得改
      this.$set(row, `isEditing${field.charAt(0).toUpperCase() + field.slice(1)}`, false)
    },

    startEditing (row, field) {
      this.$set(row, `isEditing${field.charAt(0).toUpperCase() + field.slice(1)}`, true)
      this.$nextTick(() => {
        const input = this.$refs[`${field}Input`]
        if (input) {
          input.focus()
          // input.select() // 全选 输入框中的内容 按需开启
        }
      })
    },

  editMeasureDataSubmit(){
    // to do something
    }
}
css 复制代码
// 样式也加一下 增大区域
.tableData2_class {
  width: 100%;
  margin-top: 20px;
  ::v-deep .el-table__row {
    height: 54px;
  }
}
相关推荐
程序员小张丶8 分钟前
React Native在HarmonyOS 5.0阅读类应用开发中的实践
javascript·react native·react.js·阅读·harmonyos5.0
EndingCoder9 分钟前
React Native 是什么?为什么学它?
javascript·react native·react.js
摸鱼仙人~29 分钟前
Redux Toolkit 快速入门指南:createSlice、configureStore、useSelector、useDispatch 全面解析
开发语言·javascript·ecmascript
程序员小张丶1 小时前
基于React Native开发HarmonyOS 5.0主题应用技术方案
javascript·react native·react.js·主题·harmonyos5.0
teeeeeeemo2 小时前
Vue数据响应式原理解析
前端·javascript·vue.js·笔记·前端框架·vue
Sahas10192 小时前
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ is not explicitly defined.
前端·javascript·vue.js
Jinxiansen02112 小时前
Vue 3 实战:【加强版】公司通知推送(WebSocket + token 校验 + 心跳机制)
前端·javascript·vue.js·websocket·typescript
JohnYan2 小时前
Bun技术评估 - 05 SQL
javascript·后端·bun
前端农民晨曦2 小时前
深入浏览器事件循环与任务队列架构
前端·javascript·面试
Spider_Man2 小时前
JavaScript对象那些坑:初学者必踩的“陷阱”与进阶秘籍
前端·javascript