实现表格双击后变输入框

双击后效果,本次需求我的代码中始终只会保持一个输入框 每次都会执行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;
  }
}
相关推荐
徐小夕5 分钟前
从 RAG 工具到文档处理平台,聊聊 LlamaIndex 为什么成了 AI 应用的标配底座
前端·vue.js·github
晓得迷路了1 小时前
栗子前端技术周刊第 156 期 - jQuery 4.1、TanStack Query v6 路线图...
前端·javascript·node.js
时间的情敌20 小时前
Three.js GLTF 阀门 / 泵体模型加载、材质修改、状态联动(正常 / 故障 / 告警变色)
开发语言·javascript·three.js·材质
bai2731 天前
日志里的 [Object] 不是空对象:console.log 默认只展开两层,console.dir 也一样
javascript·后端
狗头大军之江苏分军1 天前
《潮水漫过十七岁》 补一下,运动会
前端·javascript·后端
狗头大军之江苏分军1 天前
《潮水漫过十七岁》书都带回去
前端·javascript·后端
计算机毕设定制辅导-无忧学长1 天前
《基于SpringBoot的肩周炎健康管理系统设计与实现》
java·vue.js·spring boot·mysql·肩周炎健康管理系统
map_3d_vis1 天前
控制百度地图视野:setCenter、setZoom、panTo、flyTo 和 setViewport
前端·javascript·地图·百度地图
snow@li2 天前
前端:导入EXCEL并解析文件内容,demo
前端·javascript·vue.js
RuoyiOffice2 天前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office