fix(table): ensure that the table height is correct when the data is empty

This commit is contained in:
Vben 2021-03-09 23:23:40 +08:00
parent 1418dc6a59
commit 53867a8461
4 changed files with 94 additions and 90 deletions

View File

@ -13,6 +13,7 @@
- 确保 `table action` 的值被正确更新 - 确保 `table action` 的值被正确更新
- 修复页面切换的动画无法关闭 - 修复页面切换的动画无法关闭
- 修复`PageWrapper`title 不显示 - 修复`PageWrapper`title 不显示
- 修复表格数据为空时高度计算错误
## 2.0.3 (2021-03-07) ## 2.0.3 (2021-03-07)

View File

@ -66,7 +66,7 @@
const headerRef = ref<ComponentRef>(null); const headerRef = ref<ComponentRef>(null);
const footerRef = ref<ComponentRef>(null); const footerRef = ref<ComponentRef>(null);
const footerHeight = ref(0); const footerHeight = ref(0);
const { prefixCls } = useDesign('page-wrapper'); const { prefixCls, prefixVar } = useDesign('page-wrapper');
const { contentHeight, setPageHeight, pageHeight } = usePageContext(); const { contentHeight, setPageHeight, pageHeight } = usePageContext();
const getClass = computed(() => { const getClass = computed(() => {
@ -110,40 +110,39 @@
} }
nextTick(() => { nextTick(() => {
//fix:in contentHeight mode: delay getting footer and header dom element to get the correct height //fix:in contentHeight mode: delay getting footer and header dom element to get the correct height
setTimeout(() => { const footer = unref(footerRef);
const footer = unref(footerRef); const header = unref(headerRef);
const header = unref(headerRef); footerHeight.value = 0;
footerHeight.value = 0; const footerEl = footer?.$el;
const footerEl = footer?.$el;
if (footerEl) { if (footerEl) {
footerHeight.value += footerEl?.offsetHeight ?? 0; footerHeight.value += footerEl?.offsetHeight ?? 0;
} }
let headerHeight = 0; let headerHeight = 0;
const headerEl = header?.$el; const headerEl = header?.$el;
if (headerEl) { if (headerEl) {
headerHeight += headerEl?.offsetHeight ?? 0; headerHeight += headerEl?.offsetHeight ?? 0;
} }
//fix:subtract content's marginTop and marginBottom value // fix:subtract content's marginTop and marginBottom value
let subtractHeight = 0; let subtractHeight = 0;
const attributes = getComputedStyle( const { marginBottom, marginTop } = getComputedStyle(
document.querySelectorAll('.vben-page-wrapper-content')[0] document.querySelectorAll(`.${prefixVar}-page-wrapper-content`)?.[0]
); );
if (attributes.marginBottom) { if (marginBottom) {
const contentMarginBottom = Number(attributes.marginBottom.replace(/[^\d]/g, '')); const contentMarginBottom = Number(marginBottom.replace(/[^\d]/g, ''));
subtractHeight += contentMarginBottom; subtractHeight += contentMarginBottom;
} }
if (attributes.marginTop) { if (marginTop) {
const contentMarginTop = Number(attributes.marginTop.replace(/[^\d]/g, '')); const contentMarginTop = Number(marginTop.replace(/[^\d]/g, ''));
subtractHeight += contentMarginTop; subtractHeight += contentMarginTop;
} }
setPageHeight?.( setPageHeight?.(
unref(contentHeight) - unref(footerHeight) - headerHeight - subtractHeight unref(contentHeight) - unref(footerHeight) - headerHeight - subtractHeight
); );
}, 400);
}); });
}, },
{ {
flush: 'post',
immediate: true, immediate: true,
} }
); );
@ -171,6 +170,7 @@
.@{prefix-cls}-content { .@{prefix-cls}-content {
margin: 16px 16px 0 16px; margin: 16px 16px 0 16px;
} }
.ant-page-header { .ant-page-header {
&:empty { &:empty {
padding: 0; padding: 0;

View File

@ -161,7 +161,8 @@
getProps, getProps,
tableElRef, tableElRef,
getColumnsRef, getColumnsRef,
getRowSelectionRef getRowSelectionRef,
getDataSourceRef
); );
const { customRow } = useCustomRow(getProps, { const { customRow } = useCustomRow(getProps, {

View File

@ -14,14 +14,15 @@ export function useTableScroll(
propsRef: ComputedRef<BasicTableProps>, propsRef: ComputedRef<BasicTableProps>,
tableElRef: Ref<ComponentRef>, tableElRef: Ref<ComponentRef>,
columnsRef: ComputedRef<BasicColumn[]>, columnsRef: ComputedRef<BasicColumn[]>,
rowSelectionRef: ComputedRef<TableRowSelection<any> | null> rowSelectionRef: ComputedRef<TableRowSelection<any> | null>,
getDataSourceRef: ComputedRef<Recordable[]>
) { ) {
const tableHeightRef: Ref<Nullable<number>> = ref(null); const tableHeightRef: Ref<Nullable<number>> = ref(null);
const modalFn = useModalContext(); const modalFn = useModalContext();
// const [debounceCalcTableHeight] = useDebounce(calcTableHeight, 80); //320 Greater than animation time 280
const [debounceRedoHeight] = useDebounce(redoHeight, 250); const [debounceRedoHeight] = useDebounce(redoHeight, 300);
const getCanResize = computed(() => { const getCanResize = computed(() => {
const { canResize, scroll } = unref(propsRef); const { canResize, scroll } = unref(propsRef);
@ -34,6 +35,7 @@ export function useTableScroll(
debounceRedoHeight(); debounceRedoHeight();
}, },
{ {
flush: 'post',
immediate: true, immediate: true,
} }
); );
@ -59,77 +61,77 @@ export function useTableScroll(
async function calcTableHeight() { async function calcTableHeight() {
const { resizeHeightOffset, pagination, maxHeight } = unref(propsRef); const { resizeHeightOffset, pagination, maxHeight } = unref(propsRef);
if (!unref(getCanResize)) return; const tableData = unref(getDataSourceRef);
if (!unref(getCanResize) || tableData.length === 0) return;
await nextTick(); await nextTick();
//Add a delay to get the correct bottomIncludeBody paginationHeight footerHeight headerHeight //Add a delay to get the correct bottomIncludeBody paginationHeight footerHeight headerHeight
setTimeout(() => { const table = unref(tableElRef);
const table = unref(tableElRef); if (!table) return;
if (!table) return;
const tableEl: Element = table.$el; const tableEl: Element = table.$el;
if (!tableEl) return; if (!tableEl) return;
const headEl = tableEl.querySelector('.ant-table-thead ');
const headEl = tableEl.querySelector('.ant-table-thead '); if (!headEl) return;
if (!headEl) return; // Table height from bottom
const { bottomIncludeBody } = getViewportOffset(headEl);
// Table height from bottom height-custom offset
// Table height from bottom const paddingHeight = 32;
const { bottomIncludeBody } = getViewportOffset(headEl); const borderHeight = 0;
// Table height from bottom height-custom offset // Pager height
let paginationHeight = 2;
const paddingHeight = 32; if (!isBoolean(pagination)) {
const borderHeight = 0; if (!paginationEl) {
// Pager height paginationEl = tableEl.querySelector('.ant-pagination') as HTMLElement;
let paginationHeight = 2;
if (!isBoolean(pagination)) {
if (!paginationEl) {
paginationEl = tableEl.querySelector('.ant-pagination') as HTMLElement;
}
if (paginationEl) {
const offsetHeight = paginationEl.offsetHeight;
paginationHeight += offsetHeight || 0;
} else {
// TODO First fix 24
paginationHeight += 24;
}
} }
if (paginationEl) {
let footerHeight = 0; const offsetHeight = paginationEl.offsetHeight;
if (!isBoolean(pagination)) { paginationHeight += offsetHeight || 0;
if (!footerEl) { } else {
footerEl = tableEl.querySelector('.ant-table-footer') as HTMLElement; // TODO First fix 24
} else { paginationHeight += 24;
const offsetHeight = footerEl.offsetHeight;
footerHeight += offsetHeight || 0;
}
} }
}
let headerHeight = 0; let footerHeight = 0;
if (headEl) { if (!isBoolean(pagination)) {
headerHeight = (headEl as HTMLElement).offsetHeight; if (!footerEl) {
footerEl = tableEl.querySelector('.ant-table-footer') as HTMLElement;
} else {
const offsetHeight = footerEl.offsetHeight;
footerHeight += offsetHeight || 0;
} }
}
let height = let headerHeight = 0;
bottomIncludeBody - if (headEl) {
(resizeHeightOffset || 0) - headerHeight = (headEl as HTMLElement).offsetHeight;
paddingHeight - }
borderHeight -
paginationHeight -
footerHeight -
headerHeight;
height = (height > maxHeight! ? (maxHeight as number) : height) ?? height; let height =
setHeight(height); bottomIncludeBody -
(resizeHeightOffset || 0) -
paddingHeight -
borderHeight -
paginationHeight -
footerHeight -
headerHeight;
if (!bodyEl) { height = (height > maxHeight! ? (maxHeight as number) : height) ?? height;
bodyEl = tableEl.querySelector('.ant-table-body'); setHeight(height);
}
bodyEl!.style.height = `${height}px`; if (!bodyEl) {
}, 200); bodyEl = tableEl.querySelector('.ant-table-body');
}
bodyEl!.style.height = `${height}px`;
} }
useWindowSizeFn(calcTableHeight, 200); useWindowSizeFn(calcTableHeight, 280);
const getScrollX = computed(() => { const getScrollX = computed(() => {
let width = 0; let width = 0;