dynamic-height.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <template>
  2. <el-table-v2 :columns="columns" :data="data" :sort-by="sort" :estimated-row-height="40" :width="700" :height="400" fixed @column-sort="onColumnSort" />
  3. </template>
  4. <script lang="tsx" setup>
  5. import { ref } from 'vue'
  6. import { ElButton, ElTag, TableV2FixedDir, TableV2SortOrder } from 'element-plus'
  7. import type { Column, SortBy } from '@element-plus/components/table-v2'
  8. const longText = 'Quaerat ipsam necessitatibus eum quibusdam est id voluptatem cumque mollitia.'
  9. const midText = 'Corrupti doloremque a quos vero delectus consequatur.'
  10. const shortText = 'Eius optio fugiat.'
  11. const textList = [shortText, midText, longText]
  12. // generate random number in range 0 to 2
  13. let id = 0
  14. const dataGenerator = () => ({
  15. id: `random:${++id}`,
  16. name: 'Tom',
  17. date: '2016-05-03',
  18. description: textList[Math.floor(Math.random() * 3)],
  19. })
  20. const columns: Column<any>[] = [
  21. {
  22. key: 'id',
  23. title: 'Id',
  24. dataKey: 'id',
  25. width: 150,
  26. sortable: true,
  27. fixed: TableV2FixedDir.LEFT,
  28. },
  29. {
  30. key: 'name',
  31. title: 'Name',
  32. dataKey: 'name',
  33. width: 150,
  34. align: 'center',
  35. cellRenderer: ({ cellData: name }) => <ElTag>{name}</ElTag>,
  36. },
  37. {
  38. key: 'description',
  39. title: 'Description',
  40. dataKey: 'description',
  41. width: 150,
  42. cellRenderer: ({ cellData: description }) => <div style="padding: 10px 0;">{description}</div>,
  43. },
  44. {
  45. key: 'operations',
  46. title: 'Operations',
  47. cellRenderer: () => (
  48. <>
  49. <ElButton size="small">Edit</ElButton>
  50. <ElButton size="small" type="danger">
  51. Delete
  52. </ElButton>
  53. </>
  54. ),
  55. width: 150,
  56. align: 'center',
  57. },
  58. ]
  59. const data = ref(
  60. Array.from({ length: 200 })
  61. .map(dataGenerator)
  62. .sort((a, b) => (a.name > b.name ? 1 : -1))
  63. )
  64. const sort = ref<SortBy>({ key: 'name', order: TableV2SortOrder.ASC })
  65. const onColumnSort = (sortBy: SortBy) => {
  66. const order = sortBy.order === 'asc' ? 1 : -1
  67. const dataClone = [...data.value]
  68. dataClone.sort((a, b) => (a[sortBy.key] > b[sortBy.key] ? order : -order))
  69. sort.value = sortBy
  70. data.value = dataClone
  71. }
  72. </script>