Help with Prisma Pagination for my Shadcn Table
Unanswered
shadow posted this in #help-forum
shadowOP
Hey everyone,
I'm trying to add server-side pagination with Prisma for my shadcn data table and could use some help. Right now, I'm just pulling all the data with this code:
With this, i have two questions:
How do I change the Prisma query to use
How can I get the current page info from the table and send it to the Prisma query?
I'm trying to add server-side pagination with Prisma for my shadcn data table and could use some help. Right now, I'm just pulling all the data with this code:
LinksTable.tsx
import { Link, columns } from "./columns"
import { DataTable } from "./data-table"
import { prisma } from "@/lib/prisma"
async function getData(): Promise<Link[]> {
// This grabs all links; need to add pagination here using skip and take
const links = await prisma.shortLink.findMany();
return links;
}
export default async function LinksTable() {
const data = await getData()
return (
<div className="container mx-auto py-10">
<DataTable columns={columns} data={data} />
</div>
)
}
data-table.tsx
"use client"
import {
ColumnDef,
flexRender,
getCoreRowModel,
useReactTable,
getPaginationRowModel,
} from "@tanstack/react-table"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
import { Button } from "@/components/ui/button"
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
data: TData[]
}
export function DataTable<TData, TValue>({
columns,
data,
}: DataTableProps<TData, TValue>) {
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
})
return (
<div>
<div className="rounded-none border">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(header.column.columnDef.header, header.getContext())
}
</TableHead>
))}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow key={row.id} data-state={row.getIsSelected() && "selected"}>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
<div className="flex items-center justify-end space-x-2 py-4">
<Button variant="outline" size="sm" onClick={() => table.previousPage()} disabled={!table.getCanPreviousPage()}>
Previous
</Button>
<Button variant="outline" size="sm" onClick={() => table.nextPage()} disabled={!table.getCanNextPage()}>
Next
</Button>
</div>
</div>
)
}
With this, i have two questions:
How do I change the Prisma query to use
skip
and take
so that it only fetches part of the data based on the page?How can I get the current page info from the table and send it to the Prisma query?