fix(virtualization): measure natural rows and align viewport geometry
This commit is contained in:
Vendored
+74
-17
@@ -107,9 +107,13 @@ const listboxRef = ref<{ focus: (key?: never) => void } | null>(null);
|
||||
|
||||
// ---- variable-height table window ----
|
||||
const tableCount = ref(300);
|
||||
const tableRowHeight = (index: number) => (index % 2 ? 44 : 72);
|
||||
const unknownTableRows = ref(false);
|
||||
const tableWidth = ref(800);
|
||||
const tableRowHeight = (index: number) =>
|
||||
unknownTableRows.value ? undefined : index % 2 ? 44 : 72;
|
||||
const tableRows = useVirtualRows({
|
||||
count: () => tableCount.value,
|
||||
keys: () => Array.from({ length: tableCount.value }, (_, i) => i + 1),
|
||||
rowHeight: tableRowHeight,
|
||||
estimatedRowHeight: fallbackRow,
|
||||
loaderHeight: 40,
|
||||
@@ -136,7 +140,10 @@ function jumpToRow(index: number) {
|
||||
let detach: (() => void) | undefined;
|
||||
onMounted(() => {
|
||||
const element = scrollElement();
|
||||
if (element) detach = bindVirtualRows(tableRows, element);
|
||||
if (element)
|
||||
detach = bindVirtualRows(tableRows, element, {
|
||||
content: element.querySelector("tbody") ?? undefined,
|
||||
});
|
||||
});
|
||||
onUnmounted(() => detach?.());
|
||||
</script>
|
||||
@@ -183,7 +190,8 @@ onUnmounted(() => detach?.());
|
||||
virtualized
|
||||
:row-height="resolvedRowHeight"
|
||||
:estimated-row-height="fallbackRow"
|
||||
:heading-height="headingHeight"
|
||||
:heading-height="estimatedRows ? () => undefined : headingHeight"
|
||||
:estimated-heading-height="32"
|
||||
:loader-height="40"
|
||||
:gap="8"
|
||||
:padding="12"
|
||||
@@ -277,7 +285,13 @@ onUnmounted(() => detach?.());
|
||||
<section style="display: grid; gap: 8px">
|
||||
<h2>表格虚拟窗口(useVirtualRows)</h2>
|
||||
<div style="display: flex; gap: 8px">
|
||||
<Button size="sm" variant="secondary" @click="jumpToRow(150)">
|
||||
<Button @click="unknownTableRows = !unknownTableRows">
|
||||
切换表格未知行高
|
||||
</Button>
|
||||
<Button @click="tableWidth = tableWidth === 800 ? 520 : 800">
|
||||
表格宽度 {{ tableWidth }}
|
||||
</Button>
|
||||
<Button size="sm" variant="secondary" @click="jumpToRow(149)">
|
||||
定位第 150 行
|
||||
</Button>
|
||||
<Button
|
||||
@@ -293,12 +307,18 @@ onUnmounted(() => detach?.());
|
||||
<div
|
||||
ref="scrollRef"
|
||||
data-slot="table-virtual-viewport"
|
||||
style="height: 300px; width: 100%; overflow: auto"
|
||||
:style="{
|
||||
height: '300px',
|
||||
width: `${tableWidth}px`,
|
||||
overflow: 'auto',
|
||||
}"
|
||||
>
|
||||
<TableContent
|
||||
aria-label="可变行高表格"
|
||||
style="min-width: 520px; table-layout: fixed"
|
||||
:aria-rowcount="tableCount + 1"
|
||||
:row-keys="tableItems.map((row) => row.id)"
|
||||
@focus-row="(_key, index) => jumpToRow(index)"
|
||||
>
|
||||
<TableHeader aria-rowindex="1">
|
||||
<TableColumn id="name" is-row-header :min-width="160">
|
||||
@@ -311,22 +331,59 @@ onUnmounted(() => detach?.());
|
||||
aria-hidden="true"
|
||||
:style="{ height: tableWindow.padTop + 'px' }"
|
||||
/>
|
||||
<TableRow
|
||||
v-for="row in visibleTableRows"
|
||||
:key="row.id"
|
||||
:row-key="row.id"
|
||||
:aria-rowindex="row.id + 1"
|
||||
:style="{
|
||||
height: `${tableRows.rowExtentOf(row.id - 1)}px`,
|
||||
}"
|
||||
>
|
||||
<TableCell>{{ row.name }}</TableCell>
|
||||
<TableCell>行高 44/72 交替</TableCell>
|
||||
</TableRow>
|
||||
<template v-for="row in visibleTableRows" :key="row.id">
|
||||
<TableRow
|
||||
:row-key="row.id"
|
||||
:aria-rowindex="row.id + 1"
|
||||
:ref="(node) => tableRows.measureRow(row.id - 1, node)"
|
||||
:height="tableRowHeight(row.id - 1)"
|
||||
>
|
||||
<TableCell>{{ row.name }}</TableCell>
|
||||
<TableCell>
|
||||
{{
|
||||
unknownTableRows
|
||||
? "需要测量的表格内容,宽度改变后会重新换行。".repeat(
|
||||
10,
|
||||
)
|
||||
: "行高 44/72 交替"
|
||||
}}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
<tr
|
||||
v-if="row.id < tableCount"
|
||||
aria-hidden="true"
|
||||
style="height: 4px"
|
||||
/>
|
||||
</template>
|
||||
<tr
|
||||
v-if="tableWindow.end < tableCount"
|
||||
aria-hidden="true"
|
||||
:style="{ height: tableWindow.padBottom + 'px' }"
|
||||
/>
|
||||
<template v-else>
|
||||
<tr
|
||||
v-if="tableCount"
|
||||
aria-hidden="true"
|
||||
style="height: 4px"
|
||||
/>
|
||||
<tr role="presentation">
|
||||
<td colspan="2" style="padding: 0">
|
||||
<div
|
||||
role="status"
|
||||
data-probe-table-loader
|
||||
style="
|
||||
height: 40px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
"
|
||||
>
|
||||
加载更多…
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr aria-hidden="true" style="height: 12px" />
|
||||
</template>
|
||||
</TableBody>
|
||||
</TableContent>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user