当前位置:   article > 正文

element-plus表格上下键滚动_element plus table上下移动

element plus table上下移动
  1. <template>
  2. <div>
  3. <el-table
  4. ref="singleTableRef"
  5. width="200px"
  6. :data="tableData"
  7. highlight-current-row
  8. height="500"
  9. >
  10. <el-table-column label="姓名" prop="name"></el-table-column>
  11. <!-- 其他列定义 -->
  12. <el-table-column>
  13. <template #scope>
  14. <el-table-column label="操作">
  15. <el-button @click="deleteRow(scope.$index)">删除</el-button>
  16. </el-table-column>
  17. </template>
  18. </el-table-column>
  19. </el-table>
  20. </div>
  21. </template>
  22. <script>
  23. export default {
  24. data() {
  25. return {
  26. tableData: [
  27. { name: 'xiaodong1' },
  28. { name: 'xiaodong2' },
  29. { name: 'xiaodong3' },
  30. { name: 'xiaodong4' },
  31. { name: 'xiaodong5' },
  32. { name: 'xiaodong6' },
  33. { name: 'xiaodong7' },
  34. { name: 'xiaodong8' },
  35. { name: 'xiaodong9' },
  36. { name: 'xiaodong10' },
  37. { name: 'xiaodong11' },
  38. { name: 'xiaodong12' },
  39. { name: 'xiaodong13' },
  40. { name: 'xiaodong14' },
  41. { name: 'xiaodong15' },
  42. { name: 'xiaodong16' },
  43. { name: 'xiaodong17' },
  44. { name: 'xiaodong18' },
  45. { name: 'xiaodong19' },
  46. { name: 'xiaodong20' },
  47. { name: 'xiaodong21' },
  48. { name: 'xiaodong22' },
  49. { name: 'xiaodong23' },
  50. ], // 表格数据
  51. selectedRowIndex: 0, // 当前选中行索引
  52. }
  53. },
  54. mounted() {
  55. document.addEventListener('keydown', this.handleKeyDown)
  56. this.$refs.singleTableRef.setCurrentRow(this.tableData[0])
  57. },
  58. methods: {
  59. handleKeyDown(event) {
  60. // 更新选中行索引
  61. if (event.key === 'ArrowUp') {
  62. // 向上箭头键,选中上一行
  63. if (this.selectedRowIndex > 0) {
  64. this.selectedRowIndex--
  65. } else {
  66. this.selectedRowIndex = this.tableData.length - 1
  67. }
  68. // // 滚动到选中行
  69. const rowElement = document.querySelector(
  70. `.el-table__row:nth-child(${this.selectedRowIndex + 1})`,
  71. )
  72. this.$refs.singleTableRef.setCurrentRow(this.tableData[this.selectedRowIndex])
  73. rowElement.scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'smooth' })
  74. } else if (event.key === 'ArrowDown') {
  75. // 向下箭头键,选中下一行
  76. if (this.selectedRowIndex < this.tableData.length - 1) {
  77. this.selectedRowIndex++
  78. } else {
  79. this.selectedRowIndex = 0
  80. }
  81. // // 滚动到选中行
  82. const rowElement = document.querySelector(
  83. `.el-table__row:nth-child(${this.selectedRowIndex + 1})`,
  84. )
  85. this.$refs.singleTableRef.setCurrentRow(this.tableData[this.selectedRowIndex])
  86. rowElement.scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'smooth' })
  87. }
  88. },
  89. },
  90. }
  91. </script>
  92. <style lang="scss">
  93. </style>

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

闽ICP备14008679号