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

265 lines
6.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, ref } from "vue";
import {
Chip,
Pagination,
PaginationContent,
PaginationItem,
PaginationLink,
PaginationNext,
PaginationNextIcon,
PaginationPrevious,
PaginationPreviousIcon,
PaginationSummary,
Table,
TableBody,
TableCell,
TableColumn,
TableContent,
TableFooter,
TableHeader,
TableRow,
TableScrollContainer,
TableSortableColumnHeader,
} from "@heroui-vue/components";
import {
createTable,
getCoreRowModel,
getPaginationRowModel,
getSortedRowModel,
type PaginationState,
type SortingState,
} from "@tanstack/table-core";
interface User {
id: number;
name: string;
role: string;
status: "在职" | "未激活" | "休假";
email: string;
}
const statusColorMap: Record<string, "success" | "danger" | "warning"> = {
休假: "warning",
在职: "success",
未激活: "danger",
};
const users: User[] = [
{
email: "kate@acme.com",
id: 1,
name: "Kate Moore",
role: "首席执行官",
status: "在职",
},
{
email: "john@acme.com",
id: 2,
name: "John Smith",
role: "首席技术官",
status: "在职",
},
{
email: "sara@acme.com",
id: 3,
name: "Sara Johnson",
role: "首席营销官",
status: "休假",
},
{
email: "michael@acme.com",
id: 4,
name: "Michael Brown",
role: "首席财务官",
status: "在职",
},
{
email: "emily@acme.com",
id: 5,
name: "Emily Davis",
role: "产品经理",
status: "未激活",
},
{
email: "davis@acme.com",
id: 6,
name: "Davis Wilson",
role: "首席设计师",
status: "在职",
},
{
email: "olivia@acme.com",
id: 7,
name: "Olivia Martinez",
role: "前端工程师",
status: "在职",
},
{
email: "james@acme.com",
id: 8,
name: "James Taylor",
role: "后端工程师",
status: "在职",
},
];
// TanStack-style column model + sorting state driving the Vue table surface.
const columns = [
{ id: "name", header: "姓名" },
{ id: "role", header: "角色" },
{ id: "status", header: "状态" },
{ id: "email", header: "邮箱" },
] as const;
const sorting = ref<SortingState>([]);
const pagination = ref<PaginationState>({ pageIndex: 0, pageSize: 4 });
const ROWS_PER_PAGE = pagination.value.pageSize;
// Real TanStack table model: core + sorted + paginated row models.
const table = computed(() =>
createTable({
data: users,
columns: [
{ id: "name", accessorKey: "name" },
{ id: "role", accessorKey: "role" },
{ id: "status", accessorKey: "status" },
{ id: "email", accessorKey: "email" },
],
state: { sorting: sorting.value, pagination: pagination.value },
onStateChange: () => {},
renderFallbackValue: null,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
}),
);
const pageRows = computed(() =>
table.value.getPaginationRowModel().rows.map((r) => r.original),
);
const pageCount = computed(() => Math.max(1, table.value.getPageCount()));
const pages = computed(() =>
Array.from({ length: pageCount.value }, (_, i) => i + 1),
);
const pageIndex = computed(() => pagination.value.pageIndex);
const start = computed(() => pageIndex.value * ROWS_PER_PAGE + 1);
const end = computed(() =>
Math.min((pageIndex.value + 1) * ROWS_PER_PAGE, users.length),
);
const canPrevious = computed(() => pageIndex.value > 0);
const canNext = computed(() => pageIndex.value < pageCount.value - 1);
function goToPage(index: number) {
pagination.value = {
...pagination.value,
pageIndex: Math.max(0, Math.min(pageCount.value - 1, index)),
};
}
const sortDescriptor = computed(() => {
const s = sorting.value[0];
return s
? {
column: s.id,
direction: (s.desc ? "descending" : "ascending") as
"ascending" | "descending",
}
: null;
});
function onSortChange(
d: { column: string; direction: "ascending" | "descending" | "none" } | null,
) {
if (!d || d.direction === "none") {
sorting.value = [];
return;
}
sorting.value = [{ id: d.column, desc: d.direction === "descending" }];
}
function dirOf(id: string): "ascending" | "descending" | undefined {
const s = sorting.value[0];
return s && s.id === id ? (s.desc ? "descending" : "ascending") : undefined;
}
</script>
<template>
<Table>
<TableScrollContainer>
<TableContent
aria-label="TanStack 表格示例"
class="min-w-[600px]"
:sort-descriptor="sortDescriptor"
@update:sort-descriptor="onSortChange"
>
<TableHeader>
<TableColumn
v-for="col in columns"
:key="col.id"
:id="col.id"
:is-row-header="col.id === 'name'"
allows-sorting
>
<TableSortableColumnHeader :sort-direction="dirOf(col.id)">
{{ col.header }}
</TableSortableColumnHeader>
</TableColumn>
</TableHeader>
<TableBody>
<TableRow
v-for="user in pageRows"
:key="user.id"
:row-key="String(user.id)"
>
<TableCell>{{ user.name }}</TableCell>
<TableCell>{{ user.role }}</TableCell>
<TableCell>
<Chip
:color="statusColorMap[user.status]"
size="sm"
variant="soft"
>
{{ user.status }}
</Chip>
</TableCell>
<TableCell>{{ user.email }}</TableCell>
</TableRow>
</TableBody>
</TableContent>
</TableScrollContainer>
<TableFooter>
<Pagination size="sm">
<PaginationSummary>
{{ start }}–{{ end }} / 共 {{ users.length }} 条
</PaginationSummary>
<PaginationContent>
<PaginationItem>
<PaginationPrevious
:is-disabled="!canPrevious"
@click="goToPage(pageIndex - 1)"
>
<PaginationPreviousIcon />
上一页
</PaginationPrevious>
</PaginationItem>
<PaginationItem v-for="p in pages" :key="p">
<PaginationLink
:is-active="p === pageIndex + 1"
@click="goToPage(p - 1)"
>
{{ p }}
</PaginationLink>
</PaginationItem>
<PaginationItem>
<PaginationNext
:is-disabled="!canNext"
@click="goToPage(pageIndex + 1)"
>
下一页
<PaginationNextIcon />
</PaginationNext>
</PaginationItem>
</PaginationContent>
</Pagination>
</TableFooter>
</Table>
</template>