vue实现表格行内编辑

<template slot-scope="scope">
  <div>
    <el-button type="success" @click="addData">添加数据</el-button>
    <el-button type="success" @click="submitData">提交</el-button>
    <el-divider></el-divider>
    <el-table :data="tableData" border stripe style="width: 100%;">
      <el-table-column prop="name" label="姓名">
        <template slot-scope="scope">
          <el-input v-if="scope.row.edit" v-model="scope.row.name" placeholder="姓名"></el-input>
          <span v-else>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="sex" label="性别">
        <template slot-scope="scope">
          <el-select v-if="scope.row.edit" v-model="scope.row.sex" placeholder="请选择">
            <el-option label="男" value="1">男</el-option>
            <el-option label="女" value="2">女</el-option>
          </el-select>
          <span v-else>
            <p v-if="scope.row.sex == 1">男</p>
            <p v-if="scope.row.sex == 2">女</p>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.edit" type="text" size="medium" @click="confirmAdd(scope.row)">
            <i class="el-icon-check" aria-hidden="true">保存</i>
          </el-button>
          <div v-else>
            <el-button type="text" size="medium" @click="editData(scope.row)">
              <i class="el-icon-edit" aria-hidden="true">编辑</i>
            </el-button>
            <el-button type="text" size="medium" @click="deleteData(scope.row, scope.$index)">
              <i class="el-icon-delete" aria-hidden="true">删除</i>
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  export default {
    name: 'Demo',
    data() {
      return {
        tableData: [],
      }
    },
    methods: {
      //添加
      addData() {
        this.tableData.push({
          edit: true,
        })
      },
      //确认添加
      confirmAdd(row) {
        row.edit = false
      },
      //修改
      editData(row) {
        row.edit = true
      },
      //删除
      deleteData(row, index) {
        this.tableData.splice(index, 1)
      },
      submitData() {
        alert(JSON.stringify(this.tableData))
      },
    },
  }
</script>

<style lang="scss" scoped></style>

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员阿明

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

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

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

打赏作者

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

抵扣说明:

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

余额充值