247 lines
6.0 KiB
Vue
247 lines
6.0 KiB
Vue
<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,
|
|
getSortedRowModel,
|
|
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>([{ id: "name", desc: false }]);
|
|
const page = ref(1);
|
|
const ROWS_PER_PAGE = 4;
|
|
|
|
// Real TanStack table model: core row model + sorted row model.
|
|
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 },
|
|
onStateChange: () => {},
|
|
renderFallbackValue: null,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
getSortedRowModel: getSortedRowModel(),
|
|
}),
|
|
);
|
|
const sortedUsers = computed(() =>
|
|
table.value.getSortedRowModel().rows.map((r) => r.original),
|
|
);
|
|
const paginatedItems = computed(() => {
|
|
const start = (page.value - 1) * ROWS_PER_PAGE;
|
|
return sortedUsers.value.slice(start, start + ROWS_PER_PAGE);
|
|
});
|
|
const totalPages = computed(() =>
|
|
Math.ceil(sortedUsers.value.length / ROWS_PER_PAGE),
|
|
);
|
|
|
|
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" }];
|
|
page.value = 1;
|
|
}
|
|
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 paginatedItems"
|
|
: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>
|
|
{{ sortedUsers.length }} 条 · 第 {{ page }} / {{ totalPages }} 页
|
|
</PaginationSummary>
|
|
<PaginationContent>
|
|
<PaginationItem>
|
|
<PaginationPrevious
|
|
:is-disabled="page === 1"
|
|
@click="page = Math.max(1, page - 1)"
|
|
>
|
|
<PaginationPreviousIcon />
|
|
上一页
|
|
</PaginationPrevious>
|
|
</PaginationItem>
|
|
<PaginationItem>
|
|
<PaginationLink is-active>{{ page }}</PaginationLink>
|
|
</PaginationItem>
|
|
<PaginationItem>
|
|
<PaginationNext
|
|
:is-disabled="page === totalPages"
|
|
@click="page = Math.min(totalPages, page + 1)"
|
|
>
|
|
下一页
|
|
<PaginationNextIcon />
|
|
</PaginationNext>
|
|
</PaginationItem>
|
|
</PaginationContent>
|
|
</Pagination>
|
|
</TableFooter>
|
|
</Table>
|
|
</template>
|