..._el-table keyown">
当前位置:   article > 正文

el-table 方向键移动高亮_el-table keyown

el-table keyown
  1. <template>
  2.   <div class="main">
  3.     <el-table
  4.       ref="tree"
  5.       :data="tableData"
  6.       height="500"
  7.       highlight-current-row
  8.       style="width: 100%"
  9.     >
  10.       <el-table-column prop="date" label="日期" width="180"> </el-table-column>
  11.       <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
  12.       <el-table-column prop="address" label="地址"> </el-table-column>
  13.     </el-table>
  14.     <el-button @click="get">获取</el-button>
  15.   </div>
  16. </template>
  17. <script>
  18. export default {
  19.   mounted() {
  20.     for (let i = 0; i < 100; i++) {
  21.       this.tableData.push({
  22.         date: "2016-05-02",
  23.         name: "王小虎" + i,
  24.         address: "上海市普陀区金沙江路 1518 弄",
  25.       });
  26.     }
  27.     let _this = this;
  28.     this.setCurRow(this.tableData[0])
  29.     window.addEventListener("keydown", (e) => {
  30.       if (!_this.currow) return;
  31.       if (!this.$refs.tree) return;
  32.       const Tbody = _this.$refs.tree.$el.querySelector(
  33.         ".el-table__body-wrapper> table > tbody"
  34.       );
  35.       if (e.keyCode == 37) {
  36.         //
  37.         e.preventDefault();
  38.       } else if (e.keyCode == 38) {
  39.         //
  40.         for (let i in _this.tableData) {
  41.           if (_this.tableData[i] == _this.currow) {
  42.             if (i > 0) {
  43.               _this.setCurRow(_this.tableData[Number(i) - 1]);
  44.               break;
  45.             }
  46.           }
  47.         }
  48.         setTimeout(() => {
  49.           let jlTop = document.getElementsByClassName("current-row")[0];
  50.           let action = _this.elementInView(jlTop);
  51.           if (action) {
  52.             Tbody.querySelector(".el-table__row.current-row").scrollIntoView({
  53.               behavior: "instant",
  54.               block: "start",
  55.               inline: "nearest",
  56.             });
  57.           }
  58.         });
  59.         e.preventDefault();
  60.       } else if (e.keyCode == 40) {
  61.         //
  62.         for (let i in _this.tableData) {
  63.           if (_this.tableData[i] == _this.currow) {
  64.             if (i < _this.tableData.length - 1) {
  65.               _this.setCurRow(_this.tableData[Number(i) + 1]);
  66.               break;
  67.             }
  68.           }
  69.         }
  70.         setTimeout(() => {
  71.           let jlTop = document.getElementsByClassName("current-row")[0];
  72.           let action = _this.elementInView(jlTop);
  73.           if (action) {
  74.             Tbody.querySelector(".el-table__row.current-row").scrollIntoView({
  75.               behavior: "instant",
  76.               block: "end",
  77.               inline: "nearest",
  78.             });
  79.           }
  80.         }, 0);
  81.         e.preventDefault();
  82.       } else if (e.keyCode == 13) {
  83.         //回车
  84.       }
  85.     });
  86.   },
  87.   watch: {},
  88.   data() {
  89.     return {
  90.       tableData: [],
  91.       currow: null,
  92.     };
  93.   },
  94.   directives: {},
  95.   computed: {},
  96.   methods: {
  97.     init() {},
  98.     setCurRow(aRow) {
  99.       if (this.$refs.tree) {
  100.         this.$refs.tree.setCurrentRow(aRow);
  101.       }
  102.       this.currow = aRow;
  103.     },
  104.     elementInView(element) {
  105.       const top = element.offsetTop; //目标元素距离顶部高度
  106.       let text = "";
  107.       let scrollTop = this.$refs.tree.bodyWrapper.scrollTop; //滚动条高度
  108.       const Tbody = this.$refs.tree.$el.querySelector(
  109.         ".el-table__body-wrapper> table > tbody"
  110.       );
  111.       const viewEl = this.$refs.tree.$el.querySelector(
  112.         ".el-table__body-wrapper"
  113.       );
  114.       let viewHeight = viewEl.getBoundingClientRect().height;
  115.       let TbodyHeight = Tbody.getBoundingClientRect().height;
  116.       if (
  117.         top >= scrollTop &&
  118.         TbodyHeight - top - element.getBoundingClientRect().height >
  119.           TbodyHeight - viewHeight - scrollTop
  120.       ) {
  121.         text = "";
  122.       } else {
  123.         if (top > scrollTop) {
  124.           text = "bottom";
  125.         } else {
  126.           text = "top";
  127.         }
  128.       }
  129.       return text;
  130.     },
  131.     get() {},
  132.   },
  133.   created() {},
  134. };
  135. </script>

找到更简单的方式

  1. <template>
  2. <el-table
  3. v-tabIndex
  4. style="width: 100%; border: 1px solid #ebeef5"
  5. row-class-name="singleTableRow"
  6. :data="tableData"
  7. >
  8. <el-table-column prop="date" label="日期" width="180"> </el-table-column>
  9. <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
  10. <el-table-column prop="address" label="地址"> </el-table-column>
  11. </el-table>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. tableData: [
  18. {
  19. date: "2016-05-02",
  20. name: "王小虎",
  21. address: "上海市普陀区金沙江路 1518 弄",
  22. },
  23. {
  24. date: "2016-05-04",
  25. name: "王小虎",
  26. address: "上海市普陀区金沙江路 1517 弄",
  27. },
  28. {
  29. date: "2016-05-01",
  30. name: "王小虎",
  31. address: "上海市普陀区金沙江路 1519 弄",
  32. },
  33. {
  34. date: "2016-05-03",
  35. name: "王小虎",
  36. address: "上海市普陀区金沙江路 1516 弄",
  37. },
  38. ],
  39. };
  40. },
  41. methods: {
  42. tabKeydown(e) {
  43. if (e.which == 40) {
  44. if (e.target.nextElementSibling) e.target.nextElementSibling.focus();
  45. e.preventDefault();
  46. } else if (e.which == 38) {
  47. if (e.target.previousElementSibling)
  48. e.target.previousElementSibling.focus();
  49. e.preventDefault();
  50. } else if (e.which == 13) {
  51. e.preventDefault();
  52. }
  53. },
  54. },
  55. directives: {
  56. tabIndex: {
  57. bind(el, binding, vnode) {
  58. const _this = vnode.context;
  59. _this.$nextTick(() => {
  60. let trNodes = el.querySelectorAll(
  61. ".el-table__body-wrapper > .el-table__body > tbody .singleTableRow"
  62. );
  63. Array.prototype.forEach.call(trNodes, (node, idx, list) => {
  64. node.setAttribute("tabindex", "0");
  65. node.onkeydown = _this.tabKeydown;
  66. });
  67. });
  68. },
  69. componentUpdated(el, binding, vnode) {
  70. const _this = vnode.context;
  71. _this.$nextTick(() => {
  72. let trNodes = el.querySelectorAll(
  73. ".el-table__body-wrapper > .el-table__body > tbody .singleTableRow"
  74. );
  75. Array.prototype.forEach.call(trNodes, (node, idx, list) => {
  76. node.setAttribute("tabindex", "0");
  77. node.onkeydown = _this.tabKeydown;
  78. });
  79. });
  80. },
  81. },
  82. },
  83. };
  84. </script>
  85. <style>
  86. .singleTableRow:focus {
  87. background-color: #f5f7fa !important;
  88. }
  89. </style>

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签