前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >可编辑的表格

可编辑的表格

作者头像
tianyawhl
发布2022-09-28 15:25:26
5860
发布2022-09-28 15:25:26
举报
文章被收录于专栏:前端之攻略前端之攻略
代码语言:javascript
复制
<template>
  <div>
    <el-button @click="test">test</el-button>
    <el-table :data="tableData" :row-class-name = "tableRowClassName" @cell-click="cellClick" style="width: 100%">
      <el-table-column  label="日期" width="180">
        <template slot-scope="scope">
          <el-input v-model="scope.row.date" @blur="handleInputBlur" v-if="scope.$index ===rowIndex && cellClickLabel == '日期'" clearable></el-input>
          <span v-else>{{scope.row.date}}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="180">
         <template slot-scope="scope">
          <el-input v-model="scope.row.name" @blur="handleInputBlur" v-if="scope.$index ===rowIndex && cellClickLabel == '姓名'" clearable></el-input>
          <span v-else>{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址">
         <template slot-scope="scope">
          <span>{{scope.row.address}}</span>
        </template>
      </el-table-column>
    </el-table>
    
  </div>
</template>

数据

代码语言:javascript
复制
      rowIndex:null,
      cellClickLabel:"",
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        }
      ],
代码语言:javascript
复制
  methods: {
    test(){
     console.log(this.tableData)
    },
      tableRowClassName({ row, rowIndex }) {
         // 把每一行的索引放进row
         row.index = rowIndex
      },
      // 添加明细原因 row 当前行 column 当前列
      cellClick(row, column, cell, event) {
        console.log("点击行")
        this.rowIndex = row.index
        this.cellClickLabel = column.label
      },  
      //input框失去焦点事件
      handleInputBlur(row, event, column){   //当 input 失去焦点 时,input 切换为 span,并且让下方 表格消失(注意,与点击表格事件的执行顺序)
        console.log("失去焦点")
        this.rowIndex = ""
        this.cellClickLabel = ''
      },   
      handleChange() {
        console.log("change")
        this.rowIndex = ""
        this.cellClickLabel = ''
      }
  },
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档