element-ui 表格实现单元格可编辑的方法

<template>
  <el-table
    :data="tableData"
    border
    @cell-mouse-enter="handleMouseEnter"
    @cell-mouse-leave="handleMouseOut"
    style="width: 100%">
    <el-table-column
      label="日期"
      width="180">
      <template scope="scope">
      <span v-if="!scope.row.editeFlag">{{ scope.row.name }}</span>
      <span v-if="scope.row.editeFlag" class="cell-edit-input"><el-input v-model="scope.row.name" placeholder="请输入内容"></el-input></span>
      <span v-if="!scope.row.editeFlag" style="margin-left:10px;" class="cell-icon"  @click="handleEdit(scope.$index,scope.row)">  <i class="el-icon-edit"></i> </span>
      <span v-if="scope.row.editeFlag"  style="margin-left:10px;"  class="cell-icon"  @click="handleSave(scope.$index,scope.row)">  <i class="el-icon-document"></i> </span>
      </template>
    </el-table-column>
  </el-table>

 
</template>

<script>
export default{


  data(){
    return {
       tableData:[
    {
name:"test",
 editeFlag:false
    },
   {
      name:"test",
      editeFlag:false
    },
   {
     name:"test",
     editeFlag:false
    },
    {
name:"test",
editeFlag:false
},
    ], 
      inputColumn1:""//第一列的输入框
    }
  },
  methods:{
     handleEdit:function(index,row){
        //遍历数组改变editeFlag
        console.log(row)
        console.log(index);
        this.tableData[index].editeFlag=true;


    },
    handleSave:function(index,row){
        //保存数据,向后台取数据
         this.tableData[index].editeFlag=false;
    },
    handleMouseEnter:function(row, column, cell, event){


      cell.children[0].children[1].style.color="black";
    },
    handleMouseOut:function(row, column, cell, event){


      cell.children[0].children[1].style.color="#ffffff";
    }
  }
}

</script>

<style>
.cell-edit-input .el-input, .el-input__inner {
  width:100px;
}
.cell-icon{
  cursor:pointer;
  color:#fff;
}
</style>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值