Skip to content

Commit

Permalink
Merge pull request #578 from Louiszhai/pre_test
Browse files Browse the repository at this point in the history
feat(table): virtual scroll table optimize
  • Loading branch information
chaishi authored Mar 17, 2022
2 parents d8a6e1d + 10407a8 commit e7448cb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/hooks/useVirtualScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ const useVirtualScroll = ({
fixedHeight = false,
lineHeight = 30,
bufferSize = 20,
threshold = 100,
}: {
data: any;
container: any;
fixedHeight: boolean;
lineHeight: number;
bufferSize: number;
threshold: number;
}) => {
const state = reactive({
visibleData: [],
cachedHeight: [],
cachedScrollY: [],
});
const isVirtual = computed(() => data.value.length > threshold);
const updateId = ref(0);
const trs = new Map(); // 当前展示的行元素和数据

Expand Down Expand Up @@ -56,7 +59,7 @@ const useVirtualScroll = ({
const average = maxScrollY / cachedHeight.length; // 平均高度
return maxScrollY + (data.value.length - cachedHeight.length) * average; // 预估总高度
}
return data.value.length * lineHeight;
return isVirtual.value ? data.value.length * lineHeight : 0;
});
const translateY = computed(() => {
const { visibleData } = state;
Expand Down Expand Up @@ -163,6 +166,7 @@ const useVirtualScroll = ({

// 滚动时动态计算和渲染
const handleScroll = () => {
if (!isVirtual.value) return;
// if (revising) {
// return false; // 修正滚动条时,暂停滚动逻辑
// }
Expand Down Expand Up @@ -229,6 +233,7 @@ const useVirtualScroll = ({

!fixedHeight && watch(updateId, calculateScrollY, { flush: 'post' });
const handleRowMounted = () => {
if (!isVirtual.value) return;
updateId.value++;
};
watch(data, () => {
Expand All @@ -242,7 +247,11 @@ const useVirtualScroll = ({
start = 0;
// revising = false;
trs.clear();
updateVisibleData();
if (data.value.length <= threshold) {
state.visibleData = data.value;
} else {
updateVisibleData();
}
container.value && (container.value.scrollTop = 0);
});
let mounted = false;
Expand All @@ -260,7 +269,7 @@ const useVirtualScroll = ({
const entry = entries[0];
if (entry.isIntersecting || entry.intersectionRatio) {
mounted = true;
refreshContainer();
isVirtual.value && refreshContainer();
ob.unobserve(container.value);
}
});
Expand Down
5 changes: 3 additions & 2 deletions src/table/base-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,21 @@ export default defineComponent({
translateY = null,
handleScroll: handleVirtualScroll = null,
handleRowMounted = null,
} = isVirtual.value
} = type === 'virtual'
? useVirtualScroll({
container: tableContentRef,
data,
fixedHeight: isFixedRowHeight,
lineHeight: rowHeight,
bufferSize,
threshold: props.scroll?.threshold,
})
: {};
provide('tableContentRef', tableContentRef);
provide('rowHeightRef', ref(rowHeight));

let lastScrollY = -1;
const onInnerScroll = isVirtual.value
const onInnerScroll = type === 'virtual'
? (e: WheelEvent) => {
onTableContentScroll(e);
const target = (e.target || e.srcElement) as HTMLElement;
Expand Down

0 comments on commit e7448cb

Please sign in to comment.