vue 可编辑表格

基于vue2.0+element-ui的可编辑表格

效果如下图:

点击“编辑”,此行变为可编辑状态

点击“保存”,此行变为不可编辑状态并保存修改后的数据

 

 

 

<template>
  <div class="container">
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="序号" type="index" width="55" align="center">
      </el-table-column>
      <el-table-column label="日期" prop="date" align="center">
        <template slot-scope="scope">
          <span v-show="scope.row.isShow">{{ scope.row.date }}</span>
          <el-input
            type="text"
            :placeholder="scope.row.date"
            v-model="scope.row.date"
            v-show="!scope.row.isShow"
          />
        </template>
      </el-table-column>
      <el-table-column label="姓名" prop="name" align="center">
        <template slot-scope="scope">
          <span v-show="scope.row.isShow">{{ scope.row.name }}</span>
          <el-input
            type="text"
            :placeholder="scope.row.name"
            v-model="scope.row.name"
            v-show="!scope.row.isShow"
          />
        </template>
      </el-table-column>
      <el-table-column label="地址" prop="address" align="center">
        <template slot-scope="scope">
          <span v-show="scope.row.isShow">{{ scope.row.address }}</span>
          <el-input
            type="text"
            :placeholder="scope.row.address"
            v-model="scope.row.address"
            v-show="!scope.row.isShow"
          />
        </template>
      </el-table-column>
      <el-table-column align="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            v-show="scope.row.isShow"
            >编辑</el-button
          >
          <el-button
            @click="hold(scope.$index, scope.row)"
            size="mini"
            v-show="!scope.row.isShow"
            >保存</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div @click="addLine()" style="cursor: pointer; color: #409eff">
      添加一行
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          isShow: true,
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
          isShow: true,
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
          isShow: true,
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
          isShow: true,
        },
      ],
    };
  },
  methods: {
    /* 编辑 */
    handleEdit(index, row) {
      console.log(index, row);
      row.isShow = false;
    },
    /* 保存 */
    hold(index, row) {
      row.isShow = true;
    },
    /* 删除 */
    handleDelete(index, row) {
      console.log(index, row);
      this.tableData.splice(index, 1);
    },
    /* 添加新的一行 (默认是可编辑状态)*/
    addLine() {
      let obj = {
        date: "",
        name: "",
        address: "",
        isShow: false,
      };
      this.tableData.push(obj);
    },
  },
};
</script>

<style scoped>
/* 除去input的边框 */
/* ::v-deep .el-input__inner {
  border: none !important;
} */
</style>

  • 5
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值