manual-scroll.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 ref="tableRef" :columns="columns" :data="data" :width="width" :height="height" fixed />
  18. </template>
  19. </el-auto-resizer>
  20. </div>
  21. </template>
  22. <script lang="ts" setup>
  23. import { ref } from 'vue'
  24. import type { TableV2Instance } from 'element-plus'
  25. const generateColumns = (length = 10, prefix = 'column-', props?: any) =>
  26. Array.from({ length }).map((_, columnIndex) => ({
  27. ...props,
  28. key: `${prefix}${columnIndex}`,
  29. dataKey: `${prefix}${columnIndex}`,
  30. title: `Column ${columnIndex}`,
  31. width: 150,
  32. }))
  33. const generateData = (columns: ReturnType<typeof generateColumns>, length = 200, prefix = 'row-') =>
  34. Array.from({ length }).map((_, rowIndex) => {
  35. return columns.reduce(
  36. (rowData, column, columnIndex) => {
  37. rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex}`
  38. return rowData
  39. },
  40. {
  41. id: `${prefix}${rowIndex}`,
  42. parentId: null,
  43. }
  44. )
  45. })
  46. const columns = generateColumns(10)
  47. const data = generateData(columns, 200)
  48. const tableRef = ref<TableV2Instance>()
  49. const scrollDelta = ref(200)
  50. const scrollRows = ref(10)
  51. function scrollByPixels() {
  52. tableRef.value?.scrollToTop(scrollDelta.value)
  53. }
  54. function scrollByRows() {
  55. tableRef.value?.scrollToRow(scrollRows.value)
  56. }
  57. </script>