191 lines
4.7 KiB
Vue
191 lines
4.7 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, onUnmounted, ref } from "vue";
|
|
import {
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableColumn,
|
|
TableContent,
|
|
TableHeader,
|
|
TableRow,
|
|
TableScrollContainer,
|
|
} 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 VIEWPORT = 300;
|
|
const OVERSCAN = 8;
|
|
const scrollTop = ref(0);
|
|
const scrollEl = ref<HTMLElement | null>(null);
|
|
|
|
const startIndex = computed(() =>
|
|
Math.max(0, Math.floor(scrollTop.value / ITEM_HEIGHT) - OVERSCAN),
|
|
);
|
|
const endIndex = computed(() =>
|
|
Math.min(
|
|
users.length,
|
|
Math.ceil((scrollTop.value + VIEWPORT) / ITEM_HEIGHT) + OVERSCAN,
|
|
),
|
|
);
|
|
const visible = computed(() => users.slice(startIndex.value, endIndex.value));
|
|
const padTop = computed(() => startIndex.value * ITEM_HEIGHT);
|
|
const padBottom = computed(() => (users.length - endIndex.value) * ITEM_HEIGHT);
|
|
|
|
function focusRow(_key: string | number, index: number) {
|
|
const node = scrollEl.value;
|
|
if (!node) return;
|
|
const top = index * ITEM_HEIGHT;
|
|
if (top < node.scrollTop) node.scrollTop = top;
|
|
else if (top + ITEM_HEIGHT > node.scrollTop + VIEWPORT)
|
|
node.scrollTop = top + ITEM_HEIGHT - VIEWPORT;
|
|
scrollTop.value = node.scrollTop;
|
|
}
|
|
function onScroll(event: Event) {
|
|
scrollTop.value = (event.target as HTMLElement).scrollTop;
|
|
}
|
|
onMounted(() => {
|
|
scrollEl.value?.addEventListener("scroll", onScroll, { passive: true });
|
|
});
|
|
onUnmounted(() => {
|
|
scrollEl.value?.removeEventListener("scroll", onScroll);
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<Table>
|
|
<div
|
|
ref="scrollEl"
|
|
class="h-[300px] w-full overflow-auto"
|
|
data-slot="table-virtual-viewport"
|
|
>
|
|
<TableContent
|
|
aria-label="1000 行虚拟化表格"
|
|
class="min-w-[700px] table-fixed"
|
|
:row-keys="rowKeys"
|
|
:aria-rowcount="users.length + 1"
|
|
@focus-row="focusRow"
|
|
>
|
|
<TableHeader aria-rowindex="1" class="h-full 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: padTop + 'px' }" />
|
|
<TableRow
|
|
v-for="user in visible"
|
|
:key="user.id"
|
|
:row-key="user.id"
|
|
:aria-rowindex="user.id + 1"
|
|
:style="{ height: ITEM_HEIGHT + 'px' }"
|
|
>
|
|
<TableCell
|
|
class="whitespace-nowrap overflow-hidden text-ellipsis"
|
|
:style="{ height: ITEM_HEIGHT + 'px' }"
|
|
>
|
|
{{ user.name }}
|
|
</TableCell>
|
|
<TableCell
|
|
class="whitespace-nowrap overflow-hidden text-ellipsis"
|
|
:style="{ height: ITEM_HEIGHT + 'px' }"
|
|
>
|
|
{{ user.role }}
|
|
</TableCell>
|
|
<TableCell
|
|
class="whitespace-nowrap overflow-hidden text-ellipsis"
|
|
:style="{ height: ITEM_HEIGHT + 'px' }"
|
|
>
|
|
{{ user.email }}
|
|
</TableCell>
|
|
</TableRow>
|
|
<tr aria-hidden="true" :style="{ height: padBottom + 'px' }" />
|
|
</TableBody>
|
|
</TableContent>
|
|
</div>
|
|
</Table>
|
|
</template>
|