manual-scroll.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <div class="mb-4 flex items-center">
  3. <el-form-item label="Scroll pixels" class="mr-4">
  4. <el-input v-model="scrollDelta" />
  5. </el-form-item>
  6. <el-form-item label="Scroll rows">
  7. <el-input v-model="scrollRows" />
  8. </el-form-item>
  9. </div>
  10. <div class="mb-4 flex items-center">
  11. <el-button @click="scrollByPixels"> Scroll by pixels </el-button>
  12. <el-button @click="scrollByRows"> Scroll by rows </el-button>
  13. </div>
  14. <div style="height: 400px">
  15. <el-auto-resizer>
  16. <template #default="{ height, width }">
  17. <el-table-v2
  18. ref="tableRef"
  19. :columns="columns"
  20. :data="data"
  21. :width="width"
  22. :height="height"
  23. fixed
  24. />
  25. </template>
  26. </el-auto-resizer>
  27. </div>
  28. </template>
  29. <script lang="ts" setup>
  30. import { ref } from 'vue'
  31. import type { TableV2Instance } from 'element-plus'
  32. const generateColumns = (length = 10, prefix = 'column-', props?: any) =>
  33. Array.from({ length }).map((_, columnIndex) => ({
  34. ...props,
  35. key: `${prefix}${columnIndex}`,
  36. dataKey: `${prefix}${columnIndex}`,
  37. title: `Column ${columnIndex}`,
  38. width: 150,
  39. }))
  40. const generateData = (
  41. columns: ReturnType<typeof generateColumns>,
  42. length = 200,
  43. prefix = 'row-'
  44. ) =>
  45. Array.from({ length }).map((_, rowIndex) => {
  46. return columns.reduce(
  47. (rowData, column, columnIndex) => {
  48. rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex}`
  49. return rowData
  50. },
  51. {
  52. id: `${prefix}${rowIndex}`,
  53. parentId: null,
  54. }
  55. )
  56. })
  57. const columns = generateColumns(10)
  58. const data = generateData(columns, 200)
  59. const tableRef = ref<TableV2Instance>()
  60. const scrollDelta = ref(200)
  61. const scrollRows = ref(10)
  62. function scrollByPixels() {
  63. tableRef.value?.scrollToTop(scrollDelta.value)
  64. }
  65. function scrollByRows() {
  66. tableRef.value?.scrollToRow(scrollRows.value)
  67. }
  68. </script>