useTableScroll.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import type { BasicTableProps, TableRowSelection, BasicColumn } from '../types/table';
  2. import { Ref, ComputedRef, ref } from 'vue';
  3. import { computed, unref, nextTick, watch } from 'vue';
  4. import { getViewportOffset } from '/@/utils/domUtils';
  5. import { isBoolean } from '/@/utils/is';
  6. import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
  7. import { useModalContext } from '/@/components/Modal';
  8. import { onMountedOrActivated } from '/@/hooks/core/onMountedOrActivated';
  9. import { useDebounceFn } from '@vueuse/core';
  10. export function useTableScroll(
  11. propsRef: ComputedRef<BasicTableProps>,
  12. tableElRef: Ref<ComponentRef>,
  13. columnsRef: ComputedRef<BasicColumn[]>,
  14. rowSelectionRef: ComputedRef<TableRowSelection | null>,
  15. getDataSourceRef: ComputedRef<Recordable[]>,
  16. wrapRef: Ref<HTMLElement | null>,
  17. formRef: Ref<ComponentRef>,
  18. ) {
  19. const tableHeightRef: Ref<Nullable<number | string>> = ref(167);
  20. const modalFn = useModalContext();
  21. // Greater than animation time 280
  22. const debounceRedoHeight = useDebounceFn(redoHeight, 100);
  23. const getCanResize = computed(() => {
  24. const { canResize, scroll } = unref(propsRef);
  25. return canResize && !(scroll || {}).y;
  26. });
  27. watch(
  28. () => [unref(getCanResize), unref(getDataSourceRef)?.length],
  29. () => {
  30. debounceRedoHeight();
  31. },
  32. {
  33. flush: 'post',
  34. },
  35. );
  36. function redoHeight() {
  37. nextTick(() => {
  38. calcTableHeight();
  39. });
  40. }
  41. function setHeight(height: number) {
  42. tableHeightRef.value = height;
  43. // Solve the problem of modal adaptive height calculation when the form is placed in the modal
  44. modalFn?.redoModalHeight?.();
  45. }
  46. // No need to repeat queries
  47. let paginationEl: HTMLElement | null;
  48. let footerEl: HTMLElement | null;
  49. let bodyEl: HTMLElement | null;
  50. async function calcTableHeight() {
  51. const { resizeHeightOffset, pagination, maxHeight, isCanResizeParent, useSearchForm } =
  52. unref(propsRef);
  53. const tableData = unref(getDataSourceRef);
  54. const table = unref(tableElRef);
  55. if (!table) return;
  56. const tableEl: Element = table.$el;
  57. if (!tableEl) return;
  58. if (!bodyEl) {
  59. bodyEl = tableEl.querySelector('.ant-table-body');
  60. if (!bodyEl) return;
  61. }
  62. const hasScrollBarY = bodyEl.scrollHeight > bodyEl.clientHeight;
  63. const hasScrollBarX = bodyEl.scrollWidth > bodyEl.clientWidth;
  64. if (hasScrollBarY) {
  65. tableEl.classList.contains('hide-scrollbar-y') &&
  66. tableEl.classList.remove('hide-scrollbar-y');
  67. } else {
  68. !tableEl.classList.contains('hide-scrollbar-y') && tableEl.classList.add('hide-scrollbar-y');
  69. }
  70. if (hasScrollBarX) {
  71. tableEl.classList.contains('hide-scrollbar-x') &&
  72. tableEl.classList.remove('hide-scrollbar-x');
  73. } else {
  74. !tableEl.classList.contains('hide-scrollbar-x') && tableEl.classList.add('hide-scrollbar-x');
  75. }
  76. bodyEl!.style.height = 'unset';
  77. if (!unref(getCanResize) || !unref(tableData) || tableData.length === 0) return;
  78. await nextTick();
  79. // Add a delay to get the correct bottomIncludeBody paginationHeight footerHeight headerHeight
  80. const headEl = tableEl.querySelector('.ant-table-thead ');
  81. if (!headEl) return;
  82. // Table height from bottom height-custom offset
  83. let paddingHeight = 32;
  84. // Pager height
  85. let paginationHeight = 2;
  86. if (!isBoolean(pagination)) {
  87. paginationEl = tableEl.querySelector('.ant-pagination') as HTMLElement;
  88. if (paginationEl) {
  89. const offsetHeight = paginationEl.offsetHeight;
  90. paginationHeight += offsetHeight || 0;
  91. } else {
  92. // TODO First fix 24
  93. paginationHeight += 24;
  94. }
  95. } else {
  96. paginationHeight = -8;
  97. }
  98. let footerHeight = 0;
  99. if (!isBoolean(pagination)) {
  100. if (!footerEl) {
  101. footerEl = tableEl.querySelector('.ant-table-footer') as HTMLElement;
  102. } else {
  103. const offsetHeight = footerEl.offsetHeight;
  104. footerHeight += offsetHeight || 0;
  105. }
  106. }
  107. let headerHeight = 0;
  108. if (headEl) {
  109. headerHeight = (headEl as HTMLElement).offsetHeight;
  110. }
  111. let bottomIncludeBody = 0;
  112. if (unref(wrapRef) && isCanResizeParent) {
  113. const tablePadding = 12;
  114. const formMargin = 16;
  115. let paginationMargin = 10;
  116. const wrapHeight = unref(wrapRef)?.offsetHeight ?? 0;
  117. let formHeight = unref(formRef)?.$el.offsetHeight ?? 0;
  118. if (formHeight) {
  119. formHeight += formMargin;
  120. }
  121. if (isBoolean(pagination) && !pagination) {
  122. paginationMargin = 0;
  123. }
  124. if (isBoolean(useSearchForm) && !useSearchForm) {
  125. paddingHeight = 0;
  126. }
  127. const headerCellHeight =
  128. (tableEl.querySelector('.ant-table-title') as HTMLElement)?.offsetHeight ?? 0;
  129. console.log(wrapHeight - formHeight - headerCellHeight - tablePadding - paginationMargin);
  130. bottomIncludeBody =
  131. wrapHeight - formHeight - headerCellHeight - tablePadding - paginationMargin;
  132. } else {
  133. // Table height from bottom
  134. bottomIncludeBody = getViewportOffset(headEl).bottomIncludeBody;
  135. }
  136. let height =
  137. bottomIncludeBody -
  138. (resizeHeightOffset || 0) -
  139. paddingHeight -
  140. paginationHeight -
  141. footerHeight -
  142. headerHeight;
  143. height = (height > maxHeight! ? (maxHeight as number) : height) ?? height;
  144. setHeight(height);
  145. bodyEl!.style.height = `${height}px`;
  146. }
  147. useWindowSizeFn(calcTableHeight, 280);
  148. onMountedOrActivated(() => {
  149. calcTableHeight();
  150. nextTick(() => {
  151. debounceRedoHeight();
  152. });
  153. });
  154. const getScrollX = computed(() => {
  155. let width = 0;
  156. if (unref(rowSelectionRef)) {
  157. width += 60;
  158. }
  159. // TODO props ?? 0;
  160. const NORMAL_WIDTH = 150;
  161. const columns = unref(columnsRef).filter((item) => !item.defaultHidden);
  162. columns.forEach((item) => {
  163. width += Number.parseInt(item.width as string) || 0;
  164. });
  165. const unsetWidthColumns = columns.filter((item) => !Reflect.has(item, 'width'));
  166. const len = unsetWidthColumns.length;
  167. if (len !== 0) {
  168. width += len * NORMAL_WIDTH;
  169. }
  170. const table = unref(tableElRef);
  171. const tableWidth = table?.$el?.offsetWidth ?? 0;
  172. return tableWidth > width ? '100%' : width;
  173. });
  174. const getScrollRef = computed(() => {
  175. const tableHeight = unref(tableHeightRef);
  176. const { canResize, scroll } = unref(propsRef);
  177. return {
  178. x: unref(getScrollX),
  179. y: canResize ? tableHeight : null,
  180. scrollToFirstRowOnChange: false,
  181. ...scroll,
  182. };
  183. });
  184. return { getScrollRef, redoHeight };
  185. }