useTableHeight.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. tagView: 34
  15. });
  16. const totalOtherHeight = ref(0);
  17. const tableHeight = ref(0);
  18. const computeHeight = () => {
  19. const titleElement = unref(titleRef);
  20. const queryElement = unref(queryRef);
  21. computeTableHeight.value.titleHeight = titleElement ? titleElement.scrollHeight : 0;
  22. computeTableHeight.value.queryHeight = queryElement ? queryElement.scrollHeight : 0;
  23. totalOtherHeight.value =
  24. computeTableHeight.value.titleHeight +
  25. computeTableHeight.value.queryHeight +
  26. computeTableHeight.value.headerHeight +
  27. computeTableHeight.value.dividerHeight +
  28. computeTableHeight.value.toolbarHeight +
  29. computeTableHeight.value.padding
  30. // computeTableHeight.value.tagView;
  31. tableHeight.value = window.innerHeight - totalOtherHeight.value;
  32. };
  33. onMounted(() => {
  34. computeHeight(); // 组件挂载时计算一次高度
  35. window.addEventListener('resize', computeHeight); // 监听窗口大小变化
  36. });
  37. onUnmounted(() => {
  38. window.removeEventListener('resize', computeHeight); // 移除事件监听器
  39. });
  40. return { tableHeight: tableHeight as Ref<number> }; // 返回类型注释
  41. }