首页 > 其他分享 >VUE el-table表格实现双击编辑,单机空白处放弃修改,回车提交修改

VUE el-table表格实现双击编辑,单机空白处放弃修改,回车提交修改

时间:2023-07-31 11:15:10浏览次数:37  
标签:el 表格 columnName column prop 修改 table 双击 row

VUE el-table表格实现双击编辑,单机空白处放弃修改,回车提交修改

template

    <el-row>
      <el-col :span="24">
        <el-table @cell-dblclick="handleCellDBClick" :data="tabledata" border>
          <!-- 生成列-->
          <el-table-column align="center" v-for="column in columns" :key="column.prop"
                           :label="column.columnName" :property="column.prop">
            <template slot-scope="scope">
              <!-- 双击时展示input,save实现数据保存-->
              <el-input :id="column.prop" type="text" v-if="scope.row.isEdit === column.prop"
                        v-model="scope.row[column.prop]"
                        @keyup.enter.native="save(scope)" @blur="blurfun(scope)"/>
              <span v-else :class="[scope.row.id ? '' : 'summary']">{{
                  scope.row[column.prop]
                }}</span>
            </template>
          </el-table-column>
        </el-table>
        <br>
        <!--分页器-->
        <el-pagination
            @current-change="page"
            background
            layout="prev, pager, next"
            :prev-text="'上一页'"
            :next-text="'下一页'"
            :page-size="6"
            :total="total">
        </el-pagination>
      </el-col>
    </el-row>

script

 data() {
    return {
      //  数据量
      total: 100,
      //  表格数据
      tabledata: [
        {
          id: 1,
          name: '张三',
          entrytime: '2022-07-31',
          education: '本科',
          role: '学生',
          username: '23520981',
          password: '123456',
        },
        {
          id: 2,
          name: '张三',
          entrytime: '2022-07-31',
          education: '本科',
          role: '学生',
          username: '23520981',
          password: '123456',
        },
        {
          id: 3,
          name: '张三',
          entrytime: '2022-07-31',
          education: '本科',
          role: '学生',
          username: '23520981',
          password: '123456',
        },
        {
          id: 4,
          name: '张三',
          entrytime: '2022-07-31',
          education: '本科',
          role: '学生',
          username: '23520981',
          password: '123456',
        },
      ],
      //	定义字段
      columns: [
        {
          columnName: '序号',
          prop: 'id'
        },
        {
          columnName: '姓名',
          prop: 'name'
        },
        {
          columnName: '入职时间',
          prop: 'entrytime'
        },
        {
          columnName: '学历',
          prop: 'education'
        },
        {
          columnName: '角色',
          prop: 'role'
        },
        {
          columnName: '用户名',
          prop: 'username'
        },
        {
          columnName: '密码',
          prop: 'password'
        },
      ],
      oldinfo: '',
    }
  },
      
  methods: {
    page(val) {
      this.$message({message: `当前页: ${val}`, type: 'success'})
    },
    handleCellDBClick(row, column, cell, event) {
      this.oldinfo = row[column.property]
      const columnName = column.property
      const unEditColumns = ['id']
      //如果点击的是地区这一列,则提示不可编辑
      if (unEditColumns.includes(columnName)) {
        return this.$message({message: '当前列不可编辑', type: 'error'})
      }
      //设置isEdit属性
      this.$set(row, 'isEdit', columnName)
      this.$nextTick(() => {
        //input标签获取焦点
        document.getElementById(columnName).focus()
      })
    },
    save({row, column}, val, eventStr) {
      this.oldinfo = row[column.property]
      //数据复位,此时input隐藏,展示span
      this.$set(row, 'isEdit', null)
      //调用后台接口保存数据
      //     ...
    },
    blurfun({row, column}, val, eventStr) {
      //  失去焦点事件
      row[column.property] = this.oldinfo
      this.oldinfo = ''
      this.$set(row, 'isEdit', null)
    },

  },

style

/*表格样式*/
/* 基本表格样式 */
table {
    border-collapse: collapse;
    width: 100%;
}

table td, table th {
    border: 1px solid #ddd;
    padding: 8px;
}

table th {
    background-color: #f2f2f2;
    text-align: left;
    height: 50px;
    line-height: 50px;
    font-weight: bold;
}

table td {
    text-align: left;
    font-family: '微软雅黑 Bold', '微软雅黑 Regular', '微软雅黑';
    font-size: 18px;
    cursor: pointer;
    /*font-weight: bold;*/
}

/* 斑马线表格 */
table tr:nth-child(even) {
    background-color: #f2f2f2;
}

/* 悬浮表格 */
table:hover tr:not(:hover) {
    opacity: 0.2;
}

/*tfoot {*/
/*    text-align: center;*/
/*}*/

el-pagination {
    text-align: center;
}

标签:el,表格,columnName,column,prop,修改,table,双击,row
From: https://www.cnblogs.com/chunyouqudongwuyuan/p/17592905.html

相关文章

  • 第16章 进程管理与SELinux初探
    第16章进程管理与SELinux初探一个程序被加载到内在当中运作,那么在内存中的那个数据就被称为进程(process)。进程是操作系统上非常重要的概念,所有系统上面跑的数据都会以进程的形态存在。16.1什么是进程(process)在Linux系统中:触发任何一个事件时,系统都会将他定义成为一个......
  • 一篇关于预测“未来”的教程:运行在 Intel AIxBoard™ 开发板上的 TDengine
    英特尔数字化开发套件AIxBoard是一款AI架构的人工智能嵌入式开发板,体积小巧功能强大,可以在时序数据预测、图像分类、目标检测分割和语音处理等应用中并行运行多个神经网络。作为一款面向专业创客、开发者的功能强大的小型计算机,借助开源人工智能工具套件OpenVINO,AIxBoard......
  • PascalCase & camelCase & kebabCase介绍
    原文链接:https://www.cnblogs.com/qdkfyym/p/13528076.html一、PascalCase  帕斯卡拼写法(也叫大骆驼拼写法)  PascalCase 帕斯卡拼写法是一种计算机编程中的变量命名方法。它主要的特点是将描述变量作用所有单词的首字母大写,然后直接连接起来,单词之间没有连接符。比......
  • camel components
    http://camel.apache.org/components.htmlComponentsIncludedCamelincludesthefollowingComponentimplementationsviaURIs.Component/ArtifactId/URIDescriptionAHC/camel-ahcahc:hostname:[port]TocallexternalHTTPservicesusingAsyncHttpClientA......
  • Hive select查询语句
     创建表CREATETABLEt_usa_covid19(count_datestring,countystring,statestring,fipsint,casesint,deathsint)rowformatdelimitedfieldsterminatedby",";--将数据load加载到t_usa_covid19表对应的路径下loaddatalocalinpath......
  • kernel: oracle (xxxx): Using mlock ulimits for SHM_HUGETLB is deprecated
    Oracle数据库运行在linux6/7中,启用大页之后,我们经常在/var/log/messages里面会看到类似这样的记录:Jul3109:46:27p1erpdb01kernel:oracle(6444):UsingmlockulimitsforSHM_HUGETLBisdeprecated.-------官方文档HowToAddanon-rootgrouptohugetlb_shm_grou......
  • OpenAI API访问速度不佳?试试用Vercel来加速!
    前言众所周知,使用openAIAPI在国内访问体验并不佳,经常遇到访问较慢或者访问失败的问题。本文着重讲讲怎么解决这个问题,让我们日常开发和使用能够更方便的体验到AI带来的便利为了帮大家省钱,也为了方便操作,实现这套方案只需要以下这三样东西一个可使用的OpenAIkey一个可以正......
  • 怎么还有函数参数不支持引用的?(Excel函数集团)
    函数公式中的参数绝大部分情况下是可以用以下四种:常量引用公式运算(包括函数、四则运算等)自定义名称有少部分函数的参数只支持引用,比如Sumif的第一、第三参数,Subtotal除第一参数以外的其他所有参数等等。但是,有一个函数的参数的特殊用法,只要是引用就会甩脸子,这个函数就是XL......
  • Python elasticsearch 使用示例
    这里简单的罗列了些关于ES的自动化运维过程中可能用到的脚本DEMO创建索引并设置shards数#省略部分代码fromelasticsearchimportElasticsearchsrc_es=Elasticsearch(hosts=configs.es_source_host,maxsize=16)dest_es=Elasticsearch(hosts=configs.es_dest_host,ma......
  • 使用Python脚本实现ElasticSearch的在线数据迁移
    该脚本的功能,类似于elasticsearch-dump,二者都是基于scroll来实现的(包括reindex底层也是scroll)。依赖包#我这里演示的ES是7.x的,如果下面的脚本运行报错,请考虑调整这里的python的elasticsearch包版本pipinstallelasticsearch==7.13.1配置文件vimconfigs.py#-*-coding:utf-8......