当前位置:   article > 正文

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

el-table keydown
  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>

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