Element-UI table 单元格数据可编辑

Element-UI table 单元格数据可编辑

一开始的状态点击编辑

点击保存

// template代码
<el-table v-loading="loading" :data="tabels" @selection-change="handleSelectionChange">
  <el-table-column type="selection" width="55" align="center" />
  <el-table-column label="表名" prop="name" width="120" align="center"/>
  <el-table-column label="表解释" width="200" align="center">
    <template slot-scope="scope">
      <el-input 
      placeholder="请输入内容" 
      v-model="scope.row.remark" 
      :disabled="scope.row.update == 0?true:false" 
      style="width:75%"
      ></el-input>
      <i 
      class="el-icon-edit-outline" 
      style="margin-left:10px" 
      v-if="scope.row.update == 0" 
      @click="scope.row.update = 1"
      ></i>
      <i 
      class="el-icon-check" 
      style="margin-left:10px" 
      v-else 
      @click="updateRemark(scope.row)"
      ></i>
    </template>
  </el-table-column>
</el-table-column>

// script代码
<script>
import { getTableList,updateRemark } from "@/api/table";

export default {
  name: "Role",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      tabels: [],
      // 遮罩层
      loading: true,
    };
  },
  created() {
    this.getList();
  },
  methods: {
  // 更新表中数据
    updateRemark(rows){
      console.log(rows)
      updateRemark({id:rows.id,remark:rows.remark}).then(
        res=>{
          console.log(res)
          // 修改update状态为不可编辑
          rows.update = 0
        }
      );
    },
    handleClick(rows){
      console.log(rows) 
    },
    // 获取表格数据
    getList(){
      getTableList().then(
        res=>{
          // 为每条数据加上update来控制是否编辑
          // update为0是,不可编辑,为1时,可编辑
          res.rows.forEach(item =>{
            item.update = 0
          })
          this.tabels = res.rows
          this.loading = false
        }
      );
    },
    // 选择器
    handleSelectionChange(e){
      console.log(e)
    }
  }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值