useTableHeight.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * 计算表格高度
  3. * @param titleRef 标题容器
  4. * @param queryRef 查询条件的容器
  5. */
  6. export function useTableHeight(titleRef: Ref<HTMLElement | null>, queryRef: Ref<HTMLElement | null>) {
  7. const computeTableHeight = ref({
  8. titleHeight: 0,
  9. queryHeight: 0,
  10. headerHeight: 50,
  11. dividerHeight: 32,
  12. toolbarHeight: 51,
  13. padding: 40,
  14. });
  15. const totalOtherHeight = ref(0);
  16. const tableHeight = ref(0);
  17. const computeHeight = () => {
  18. const titleElement = unref(titleRef);
  19. const queryElement = unref(queryRef);
  20. computeTableHeight.value.titleHeight = titleElement ? titleElement.scrollHeight : 0;
  21. computeTableHeight.value.queryHeight = queryElement ? queryElement.scrollHeight : 0;
  22. totalOtherHeight.value =
  23. computeTableHeight.value.titleHeight +
  24. computeTableHeight.value.queryHeight +
  25. computeTableHeight.value.headerHeight +
  26. computeTableHeight.value.dividerHeight +
  27. computeTableHeight.value.toolbarHeight +
  28. computeTableHeight.value.padding;
  29. tableHeight.value = window.innerHeight - totalOtherHeight.value;
  30. };
  31. onMounted(() => {
  32. computeHeight(); // 组件挂载时计算一次高度
  33. window.addEventListener('resize', computeHeight); // 监听窗口大小变化
  34. });
  35. onUnmounted(() => {
  36. window.removeEventListener('resize', computeHeight); // 移除事件监听器
  37. });
  38. return { tableHeight: tableHeight as Ref<number> }; // 返回类型注释
  39. }