Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pagination #8345

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/tribler/ui/src/components/ui/simple-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,14 @@ function SimpleTable<T extends object>({
onSortingChange: setSorting,
getSubRows: (row: any) => row?.subRows,
getRowId: rowId,
autoResetPageIndex: false,
});

// If we're on an empty page, reset the pageIndex to 0
if (table.getRowModel().rows.length == 0 && table.getExpandedRowModel().rows.length != 0) {
setPagination(p => ({ ...p, pageIndex: 0 }));
}

const { t } = useTranslation();

useEffect(() => {
Expand Down Expand Up @@ -248,7 +254,7 @@ function SimpleTable<T extends object>({
function Pagination<T>({ table }: React.PropsWithChildren<{ table: ReactTable<T> }>) {
const pageIndex = table.getState().pagination.pageIndex;
const pageSize = table.getState().pagination.pageSize;
const rowCount = table.getCoreRowModel().rows.length;
const rowCount = table.getExpandedRowModel().rows.length;

const { t } = useTranslation();

Expand All @@ -257,21 +263,31 @@ function Pagination<T>({ table }: React.PropsWithChildren<{ table: ReactTable<T>
<div className="flex items-center space-x-4">
<Select defaultValue="0"
value={`${pageSize}`}
onValueChange={(value) => table.setPageSize(Number(value))}>
onValueChange={(value) => {
let size = Number(value);
if (size === 0) {
for (let row of table.getExpandedRowModel().rows) {
size += row.getLeafRows().length;
}
}
table.setPageSize(size);
}}>
<SelectPrimitive.Trigger>
<div className="px-1 py-0 hover:bg-inherit text-muted-foreground text-xs">
{pageIndex * pageSize}&nbsp;-&nbsp;
{Math.min((pageIndex + 1) * pageSize, rowCount)}&nbsp;of&nbsp;
{rowCount}
</div>
</SelectPrimitive.Trigger>
<SelectContent side="top"><SelectGroup>
<SelectLabel>Rows per page</SelectLabel>
{[10, 20, 30, 40, 50].map((pageSize) => (
<SelectItem key={pageSize} value={`${pageSize}`}>
{pageSize}
</SelectItem>
))}</SelectGroup>
<SelectContent side="top">
<SelectGroup>
<SelectLabel>Rows per page</SelectLabel>
{[10, 20, 30, 40, 50, 0].map((pageSize) => (
<SelectItem key={pageSize} value={`${pageSize}`}>
{pageSize > 0 ? pageSize : 'disable pagination'}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
<div className="flex items-center space-x-2">
Expand Down
Loading