element table表格实现编辑并后台提交数据

需求:

  1. 点击单元格可以进行编辑
  2. 离开之后进行数据保存
  3. 单元格数据进行判空

实现
1、table表格

<el-table v-loading="loading"
              :data="userList"
              :row-class-name="tableRowClassName"
              @cell-click="tabClick"
              @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户id" align="center" prop="userId"/>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span v-if="scope.row.sex==0"></span>
          <span v-if="scope.row.sex==1"></span>
        </template>
      </el-table-column>
      <el-table-column label="电话号码" align="center" prop="phoneNumber" width="200px"/>
      <el-table-column label="邮箱" align="center" prop="email" width="200px">
        <template slot-scope="scope">
          <span v-if="scope.row.index === tabClickIndex && tabClickLabel === '邮箱'">
            <el-input v-model="scope.row.email" placeholder="请输入邮箱" size="mini" @blur="inputBlur(scope.row)"/>
          </span>
          <span v-else>{{scope.row.email}}</span>
        </template>
      </el-table-column>
      <el-table-column label="毕业学校" align="center" prop="school" width="300px"/>
      <el-table-column label="学历" align="center" prop="education">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.education_information" :value="scope.row.education"/>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" width="200px"/>
    </el-table>

2、JS

data() {
    return {
      tabClickIndex: null, // 点击的单元格
      tabClickLabel: '', // 当前点击的列名
    }
},
method(){
	// table表格直接编辑
    tableRowClassName({ row, rowIndex }) {
      // 把每一行的索引放进row
      row.index = rowIndex
    },
    // 添加明细原因 row 当前行 column 当前列
    tabClick(row, column, cell, event) {
      switch (column.label) {
        case '邮箱':
          this.tabClickIndex = row.index
          this.tabClickLabel = column.label
          break
        default: return
      }
    },
    // 失去焦点初始化
    inputBlur(item) {
      this.tabClickIndex = null
      this.tabClickLabel = ''
      console.log(item)
      if (item.email == ""){
        this.$modal.msgWarning("邮箱不能为空");
        this.getList();
      }else {
        // 失去焦点 提交数据
        updateUser(item).then(response  => {
          this.$modal.msgSuccess("提交成功");
          this.getList();
        })
      }
    },
},

3、效果
在这里插入图片描述

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值