Files
heroui-vue/apps/docs/demos/table-virtualization.vue
T

183 lines
4.3 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from "vue";
import {
Table,
TableBody,
TableCell,
TableColumn,
TableContent,
TableHeader,
TableRow,
TableScrollContainer,
bindVirtualRows,
useVirtualRows,
} from "@heroui-vue/components";
interface User {
id: number;
name: string;
role: string;
email: string;
}
const roles = [
"软件工程师",
"高级工程师",
"资深工程师",
"产品经理",
"设计师",
"数据分析师",
"测试工程师",
"DevOps 工程师",
"营销经理",
"销售代表",
];
const firstNames = [
"Emma",
"Liam",
"Olivia",
"Noah",
"Ava",
"James",
"Sophia",
"Oliver",
"Isabella",
"Lucas",
"Mia",
"Ethan",
"Charlotte",
"Mason",
"Amelia",
"Logan",
"Harper",
"Alexander",
"Ella",
"Benjamin",
];
const lastNames = [
"Smith",
"Johnson",
"Williams",
"Brown",
"Jones",
"Garcia",
"Miller",
"Davis",
"Rodriguez",
"Martinez",
"Anderson",
"Taylor",
"Thomas",
"Jackson",
"White",
"Harris",
"Clark",
"Lewis",
"Robinson",
"Walker",
];
function generateUsers(count: number): User[] {
const users: User[] = [];
for (let i = 0; i < count; i++) {
const firstName = firstNames[i % firstNames.length];
const lastName =
lastNames[Math.floor(i / firstNames.length) % lastNames.length];
users.push({
email: `${firstName.toLowerCase()}.${lastName.toLowerCase()}@acme.com`,
id: i + 1,
name: `${firstName} ${lastName}`,
role: roles[i % roles.length],
});
}
return users;
}
const users = generateUsers(1000);
// Matches the fixed upstream Virtualizer layoutOptions (rowHeight/headingHeight 42).
const ITEM_HEIGHT = 42;
const rowKeys = users.map((user) => user.id);
const rows = useVirtualRows({
count: () => users.length,
rowHeight: ITEM_HEIGHT,
overscan: 8,
});
const rowWindow = rows.window;
const scrollRef = ref<HTMLElement | null>(null);
const visible = computed(() =>
users.slice(rowWindow.value.start, rowWindow.value.end),
);
// The scrolling container is the viewport wrapper: a native table cannot be
// one (its height is a minimum and overflow needs a definite height — the
// pinned source scrolls its virtual grid, a non-table element).
const scrollElement = () => scrollRef.value ?? null;
function focusRow(_key: string | number, index: number) {
const element = scrollElement();
if (element) rows.scrollToIndex(element, index);
}
let detach: (() => void) | undefined;
onMounted(() => {
const element = scrollElement();
if (element)
detach = bindVirtualRows(rows, element, {
content: element.querySelector("tbody") ?? undefined,
});
});
onUnmounted(() => detach?.());
</script>
<template>
<Table>
<TableScrollContainer>
<div
ref="scrollRef"
class="h-[300px] w-full overflow-auto"
data-slot="table-virtual-viewport"
>
<TableContent
aria-label="1000 行虚拟化表格"
class="min-w-[700px]"
:row-keys="rowKeys"
:aria-rowcount="users.length + 1"
@focus-row="focusRow"
>
<TableHeader aria-rowindex="1" style="height: 42px" class="w-full">
<TableColumn id="name" is-row-header :min-width="160">
姓名
</TableColumn>
<TableColumn id="role" :min-width="220">角色</TableColumn>
<TableColumn id="email" :min-width="240">邮箱</TableColumn>
</TableHeader>
<TableBody>
<tr
aria-hidden="true"
:style="{ height: rowWindow.padTop + 'px' }"
/>
<TableRow
v-for="user in visible"
:key="user.id"
:row-key="user.id"
:aria-rowindex="user.id + 1"
:height="ITEM_HEIGHT"
>
<TableCell>
{{ user.name }}
</TableCell>
<TableCell>
{{ user.role }}
</TableCell>
<TableCell>
{{ user.email }}
</TableCell>
</TableRow>
<tr
aria-hidden="true"
:style="{ height: rowWindow.padBottom + 'px' }"
/>
</TableBody>
</TableContent>
</div>
</TableScrollContainer>
</Table>
</template>