通过我的测试我发现两个两种方法来编辑单元格的内容

第一种点击编辑:

我是给td里添加一个input,将值赋值给input,当失去焦点的时候将input的值付给td原来的内容,然后将input删除,

代码如下:

  1.  
 let oldel = cell.children[0]
  1. if (column.label != "日期") {
  2. if(cell.children.length>1){return} ////防止点击cell再次创建input输入框
  3. var cellInput = document.createElement("input");
  4. cellInput.setAttribute("type", "text");
  5. cellInput.setAttribute("class", "edit");
  6. cellInput.value =cell.children[0].innerText;
  1. cellInput.style.width = "100%";
  2. cellInput.style.border = "none";
  3. cellInput.style.backgroundColor = "transparent";
  4. cellInput.style.paddingLeft = "10px";
  5. cellInput.style.outline = "none";
  6. oldel.style.display = " none";
  7. cell.appendChild(cellInput);
  8. cellInput.focus();    //主动聚焦
  9. cellInput.onblur = function() {
  10. oldel.innerHTML = cellInput.value;
  11. oldel.style.display = "block";
  12. cell.removeChild(cellInput);
  13. //event.target.innerHTML = cellInput.value;
  14. };
  15. }

第二种方法:

通过contentEditable来控制元素可以输入编辑

代码如下:

  1. celledit(row, column, cell, event) {
  2. cell.contentEditable = true;
  3. cell.focus()
  4. }

不需要太多,只要两行就行;

上面实现了点击编辑单个单元格的功能,现在还要实现通过键盘按上下左右在不同单元格进行切换;这样更利于用户体验

因为我使用的是Element+vue,如果你也使用的这个复制粘贴可以拿过去直接用;所以如果其他使用这个可能要进行一些改变;

  1. let self = this;
  2. if (boolen == true) {
  3. document.onkeydown = function(e) {
  4. console.log(e);
  5. var curel = document.activeElement; //当前元素
  6. var curcellIndex = curel.cellIndex; // 当前元素行单元格索引
  7. var curRowIndex = curel.parentElement.sectionRowIndex; //当前元素行的索引;
  8. var curtbody = curel.parentElement.parentElement.children; //当前tbody内容的整个表单
  9. curel.onblur = function() {
  10. console.log(curel.innerText);
  11. self.check(curel.innerText);
  12. };
  13. if (e.keyCode == 38) {
  14. //按上键
  15. if (curRowIndex - 1 < 0) {
  16. curel.contentEditable = false;
  17. curtbody[curtbody.length - 1].children[
  18. curcellIndex
  19. ].contentEditable = true;
  20. curtbody[curtbody.length - 1].children[curcellIndex].focus();
  21. } else {
  22. curel.contentEditable = false;
  23. curtbody[curRowIndex - 1].children[
  24. curcellIndex
  25. ].contentEditable = true;
  26. curtbody[curRowIndex - 1].children[curcellIndex].focus();
  27. }
  28. } else if (e.keyCode == 37) {
  29. let preCellindex =
  30. curtbody[curtbody.length - 1].children.length - 1;
  31. console.log("preRow", curel.parentElement.children.length);
  32. //键盘按钮左键
  33. if (curcellIndex - 1 == 0) {
  34. if (curRowIndex - 1 < 0) {
  35. curel.contentEditable = false;
  36. curtbody[curtbody.length - 1].children[
  37. preCellindex
  38. ].contentEditable = true;
  39. curtbody[curtbody.length - 1].children[preCellindex].focus();
  40. } else {
  41. curel.contentEditable = false;
  42. curtbody[curRowIndex - 1].children[
  43. preCellindex
  44. ].contentEditable = true;
  45. curtbody[curRowIndex - 1].children[preCellindex].focus();
  46. }
  47. } else {
  48. curel.contentEditable = false;
  49. curel.parentElement.children[
  50. curcellIndex - 1
  51. ].contentEditable = true;
  52. curel.parentElement.children[curcellIndex - 1].focus();
  53. }
  54. } else if (e.keyCode == 39 || e.keyCode == 9) {
  55. //键盘按钮右键
  56. e.preventDefault();
  57. if (curcellIndex + 1 == curel.parentElement.children.length) {
  58. if (curRowIndex + 1 == curtbody.length) {
  59. curel.contentEditable = false;
  60. curtbody[0].children[1].contentEditable = true;
  61. curtbody[0].children[1].focus();
  62. } else {
  63. curel.contentEditable = false;
  64. curtbody[curRowIndex + 1].children[1].contentEditable = true;
  65. curtbody[curRowIndex + 1].children[1].focus();
  66. }
  67. } else {
  68. curel.contentEditable = false;
  69. curel.parentElement.children[
  70. curcellIndex + 1
  71. ].contentEditable = true;
  72. curel.parentElement.children[curcellIndex + 1].focus();
  73. }
  74. } else if (e.keyCode == 40 || e.keyCode == 13) {
  75. //向下按钮按键
  76. e.preventDefault();
  77. if (curRowIndex + 1 == curtbody.length) {
  78. curel.contentEditable = false;
  79. curtbody[0].children[curcellIndex].contentEditable = true;
  80. curtbody[0].children[curcellIndex].focus();
  81. } else {
  82. curel.contentEditable = false;
  83. curtbody[curRowIndex + 1].children[
  84. curcellIndex
  85. ].contentEditable = true;
  86. curtbody[curRowIndex + 1].children[curcellIndex].focus();
  87. }
  88. }
  89. };
  90. }

vue+element-ui 实现table单元格点击编辑,并且按上下左右键单元格之间切换的更多相关文章

  1. VUE+Element UI实现简单的表格行内编辑效果

    原理是通过Css控制绑定的输入控件与显示值,在选中行样式下对控件进行隐藏或显示 <!DOCTYPE html> <html> <head> <meta cha ...

  2. (Element UI 组件 Table)去除单元格底部的横线

    Element UI 组件 Table 有一个属性 border,添加它可以增加纵向边框,但是无法控制横线边框,因此即使是最简单的 el-table,也会包含一个底部横线. 这个底部横线其实是一个 b ...

  3. Vue+element ui table 导出到excel

    需求: Vue+element UI table下的根据搜索条件导出当前所有数据 参考: https://blog.csdn.net/u010427666/article/details/792081 ...

  4. 分享一个自搭的框架,使用Spring boot+Vue+Element UI

    废弃,新的:https://www.cnblogs.com/hackyo/p/10453243.html 特点:前后端分离,可遵循restful 框架:后端使用Spring boot,整合了aop.a ...

  5. Vue + Element UI 实现权限管理系统

    Vue + Element UI 实现权限管理系统 前端篇(一):搭建开发环境 https://www.cnblogs.com/xifengxiaoma/p/9533018.html

  6. vue + element ui 实现实现动态渲染表格

    前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...

  7. vue + element ui 表格自定义表头,提供线上demo

    前言:工作中用到 vue+element ui 的前端框架,需要使用自定义表头,需要使用 re.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9710826.h ...

  8. vue+element ui 的上传文件使用组件

    前言:工作中用到 vue+element ui 的前端框架,使用到上传文件,则想着封装为组件,达到复用,可扩展.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9 ...

  9. vue+element ui 的表格列使用组件

    前言:工作中用到 vue+element ui 的前端框架,有这个场景:很多表格的列有许多一样的,所以考虑将列封装为组件.转载请注明出处:https://www.cnblogs.com/yuxiaol ...

  10. vue+element ui 的tab 动态增减,切换时提示用户是否切换

    前言:工作中用到 vue+element ui 的前端框架,动态添加 Tab,删除 Tab,切换 Tab 时提示用户是否切换等,发现 element ui  有一个 bug,这里记录一下如何实现.转载 ...

随机推荐

  1. 4、wordcount程序原理剖析及Spark架构原理

    一.wordcount程序原理深度剖析 二.Spark架构原理 1.

  2. ROS模拟

    亲测,在古月大大这篇博客中的一条命令最好改为rostopic pub /cmd_vel geometry/Twist -r 10 -- '[0.2,0,0]' '[0,0,0.5]'. http:// ...

  3. Arrays.toString的作用

    Arrays.toString()的作用是用来很方便地输出数组,而不用一个一个地输出数组中的元素. 这个方法是是用来将数组转换成String类型输出的,入参可以是long,float,double,i ...

  4. Java基本的线程操作(附代码)

    啦啦啦啦,从头整理一遍java并发的内容.开始是基本的线程操作 线程状态切换: 新建线程: @Test public void newTread(){ Thread t1 = new Thread(n ...

  5. docker安装ubuntu以后无ifconfig命令解决办法

    解决: 1.apt-get  update 2.apt install net-tools   #ifcongig 3.apt install iputils-ping     # ping

  6. Ubuntu16.04 apache2+php7.0+mysql5.7环境搭建

    今天配置一下web环境,很常见的apache+php+mysql的网站环境: 步骤一:安装apache sudo apt install apache2 步骤二:安装php7 1.安装PHP7和响应的 ...

  7. java——反射

    http://www.cnblogs.com/hxsyl/archive/2013/03/23/2977593.html

  8. Linux 基于WEB开源的系统管理工具webmin

    Webmin是目前功能最强大的基于Web的Unix系统管理工具.管理员通过浏览器访问Webmin的各种管理功能并完成相应的管理动作.目前Webmin支持绝大多数的Unix系统,这些系统除了各种版本的l ...

  9. ISO/IEC 9899:2011 条款5——5.2.3 信号与中断

    5.2.3 信号与中断 1.函数应该被设计为它们可以被一个信号在任一时刻打断,或是被一个信号处理所调用,或是两者都发生,对于初期不发生改变,但仍然处于活动状态,调用的控制流(在中断之后),函数返回值, ...

  10. 123457123456#0#-----com.twoapp.ErTongHuaHua01--前拼后广--儿童绘画填色游戏jiemei

    com.twoapp.ErTongHuaHua01----儿童绘画填色游戏jiemei