vue element el-table点击编辑单元格内容

记录修改单元格内容,template:

<template>
<el-table
      size="mini"
      :data="tableData"
      @cell-click="handleCellClick">
      <el-table-column label="序号" type="index" align="center" width="45"></el-table-column>
      <el-table-column
        v-for="(item, index) in tableColumn"
        :key="index"
        :prop="item.prop"
        :label="item.label"
        :align="item.align"
        :min-width="item.width"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <!-- 获取需要修改的单元格,转换编辑状态 -->
          <template v-if="item.prop=='code'">
            <el-input size="mini" v-model.trim="scope.row[item.prop]" v-show="scope.row.isEdit"
                      maxlength="7" onkeyup="this.value=this.value.replace(/[^\d.]/g,'')"
                      @change="handleEdit(scope.row, scope.$index)"></el-input>
            <span v-show="!scope.row.isEdit">{{ scope.row[item.prop] }}</span>
          </template>
          <template v-else>{{ scope.row[item.prop] }}</template>
        </template>
      </el-table-column>
    </el-table>
</template>

methods::初始化数据时,添加是否可编辑

// 初始化数据
async getlist() {
  let params = { page: this.page, context: this.formSearch };
  const { data, code, message } = await apilist(params);
  if (code === 0 && data) {
    // 添加所有编辑状态为false
    data.data.forEach(item=>{
      return item.isEdit=false
    })
    this.tableData = data.data;
  }
  setTimeout(() => {
    this.isLoading = false;
  }, 200);
},

methods:点击单元格时获取是否为需要修改的字段

// 点击单元格修改
async handleCellClick(row, column, cell, event){
  // 所有其他行设置为false(作为关闭编辑)
  this.tableData.forEach(item=>{
    return item.isEdit=false
  })
  // 查询点击对象是否为修改的对象
  if(column.property==='code'){
    row.isEdit = true;  // 查询对应的字段展示输入框
  }
},

methods:完成编辑后请求数据

// 修改OA编码提交
async handleEdit(index, row) {
  let params={code:row.code}
  const {data,code,message}=await apiUpdateCode(params)
  this.$message({type:code===0?"success":"warning", message:message})
  row.isEdit = false;
  await this.getlist(); // 初始化列表
},

完成。


 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁小桐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值