refactor(system): 实现表格自适应高度布局,抽离通用滚动工具hooks
1. 新增useTableScroll组合式函数,动态计算表格滚动高度 2. 改造多个系统页面布局为弹性伸缩容器,解决页面滚动问题 3. 统一表格滚动配置,优化页面视觉体验
This commit is contained in:
1 parent
cbc0a936b7
commit
4048aeb6fb
7 files changed
+234
-4
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { ref, onMounted, onBeforeUnmount } from 'vue';
|
||||
|
||||
/**
|
||||
* 监听表格父容器高度,动态计算 a-table 的 scroll.y。
|
||||
* 自动测量 a-table 内部的分页器高度,从而得到准确的 tbody 高度。
|
||||
*/
|
||||
export function useTableScroll(minHeight = 120) {
|
||||
const wrapperRef = ref<HTMLElement | null>(null);
|
||||
const tableHeight = ref<number>(minHeight);
|
||||
let observer: ResizeObserver | null = null;
|
||||
|
||||
function update() {
|
||||
if (!wrapperRef.value) return;
|
||||
const wrapperHeight = wrapperRef.value.clientHeight;
|
||||
|
||||
// 动态获取分页器高度(如果存在)
|
||||
const paginationEl = wrapperRef.value.querySelector('.ant-table-pagination') as HTMLElement | null;
|
||||
const paginationHeight = paginationEl ? paginationEl.offsetHeight : 0;
|
||||
|
||||
// scroll.y = 容器总高 - 分页器高度(表头由 a-table 自动固定,无需扣除)
|
||||
const tbodyHeight = wrapperHeight - paginationHeight;
|
||||
tableHeight.value = Math.max(tbodyHeight, minHeight);
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (!wrapperRef.value) return;
|
||||
observer = new ResizeObserver(update);
|
||||
observer.observe(wrapperRef.value);
|
||||
|
||||
// 初始测量
|
||||
update();
|
||||
|
||||
// MutationObserver 用于监听分页器的动态添加/移除
|
||||
const mutationObserver = new MutationObserver(() => {
|
||||
update();
|
||||
});
|
||||
mutationObserver.observe(wrapperRef.value, { childList: true, subtree: true });
|
||||
// 保存以便卸载时使用
|
||||
(wrapperRef.value as any).__mutationObserver = mutationObserver;
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
observer?.disconnect();
|
||||
observer = null;
|
||||
if (wrapperRef.value && (wrapperRef.value as any).__mutationObserver) {
|
||||
(wrapperRef.value as any).__mutationObserver.disconnect();
|
||||
}
|
||||
});
|
||||
|
||||
return { wrapperRef, tableHeight };
|
||||
}
|
||||
Reference in new issue
Block a user