fix: retain pending Toast and Table fidelity corrections
This commit is contained in:
@@ -25,7 +25,9 @@ import {
|
||||
import {
|
||||
createTable,
|
||||
getCoreRowModel,
|
||||
getPaginationRowModel,
|
||||
getSortedRowModel,
|
||||
type PaginationState,
|
||||
type SortingState,
|
||||
} from "@tanstack/table-core";
|
||||
|
||||
@@ -110,11 +112,11 @@ const columns = [
|
||||
{ id: "email", header: "邮箱" },
|
||||
] as const;
|
||||
|
||||
const sorting = ref<SortingState>([{ id: "name", desc: false }]);
|
||||
const page = ref(1);
|
||||
const ROWS_PER_PAGE = 4;
|
||||
const sorting = ref<SortingState>([]);
|
||||
const pagination = ref<PaginationState>({ pageIndex: 0, pageSize: 4 });
|
||||
const ROWS_PER_PAGE = pagination.value.pageSize;
|
||||
|
||||
// Real TanStack table model: core row model + sorted row model.
|
||||
// Real TanStack table model: core + sorted + paginated row models.
|
||||
const table = computed(() =>
|
||||
createTable({
|
||||
data: users,
|
||||
@@ -124,23 +126,35 @@ const table = computed(() =>
|
||||
{ id: "status", accessorKey: "status" },
|
||||
{ id: "email", accessorKey: "email" },
|
||||
],
|
||||
state: { sorting: sorting.value },
|
||||
state: { sorting: sorting.value, pagination: pagination.value },
|
||||
onStateChange: () => {},
|
||||
renderFallbackValue: null,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
getSortedRowModel: getSortedRowModel(),
|
||||
getPaginationRowModel: getPaginationRowModel(),
|
||||
}),
|
||||
);
|
||||
const sortedUsers = computed(() =>
|
||||
table.value.getSortedRowModel().rows.map((r) => r.original),
|
||||
const pageRows = computed(() =>
|
||||
table.value.getPaginationRowModel().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 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];
|
||||
@@ -160,7 +174,6 @@ function onSortChange(
|
||||
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];
|
||||
@@ -172,7 +185,7 @@ function dirOf(id: string): "ascending" | "descending" | undefined {
|
||||
<Table>
|
||||
<TableScrollContainer>
|
||||
<TableContent
|
||||
aria-label="TanStack 表格"
|
||||
aria-label="TanStack 表格示例"
|
||||
class="min-w-[600px]"
|
||||
:sort-descriptor="sortDescriptor"
|
||||
@update:sort-descriptor="onSortChange"
|
||||
@@ -192,7 +205,7 @@ function dirOf(id: string): "ascending" | "descending" | undefined {
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
<TableRow
|
||||
v-for="user in paginatedItems"
|
||||
v-for="user in pageRows"
|
||||
:key="user.id"
|
||||
:row-key="String(user.id)"
|
||||
>
|
||||
@@ -215,25 +228,30 @@ function dirOf(id: string): "ascending" | "descending" | undefined {
|
||||
<TableFooter>
|
||||
<Pagination size="sm">
|
||||
<PaginationSummary>
|
||||
{{ sortedUsers.length }} 条 · 第 {{ page }} / {{ totalPages }} 页
|
||||
{{ start }}–{{ end }} / 共 {{ users.length }} 条
|
||||
</PaginationSummary>
|
||||
<PaginationContent>
|
||||
<PaginationItem>
|
||||
<PaginationPrevious
|
||||
:is-disabled="page === 1"
|
||||
@click="page = Math.max(1, page - 1)"
|
||||
:is-disabled="!canPrevious"
|
||||
@click="goToPage(pageIndex - 1)"
|
||||
>
|
||||
<PaginationPreviousIcon />
|
||||
上一页
|
||||
</PaginationPrevious>
|
||||
</PaginationItem>
|
||||
<PaginationItem>
|
||||
<PaginationLink is-active>{{ page }}</PaginationLink>
|
||||
<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="page === totalPages"
|
||||
@click="page = Math.min(totalPages, page + 1)"
|
||||
:is-disabled="!canNext"
|
||||
@click="goToPage(pageIndex + 1)"
|
||||
>
|
||||
下一页
|
||||
<PaginationNextIcon />
|
||||
|
||||
Reference in New Issue
Block a user