absent-sapphire
absent-sapphire9mo ago

client side pagination

how to make this page handle pagination at client side
8 Replies
kapa.ai
kapa.ai9mo ago
Hi @mfarhanzachary. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To handle pagination on the client side in refine, you need to set the pagination.mode property to "client" in your useTable or useSimpleList hook. Here is an example of how to do it with useTable:
useTable({
columns,
refineCoreProps: {
pagination: {
mode: "client",
},
},
});
useTable({
columns,
refineCoreProps: {
pagination: {
mode: "client",
},
},
});
And here is an example of how to do it with useSimpleList:
const { listProps } = useSimpleList<IProduct>();

return (
<AntdList
{...listProps}
renderItem={renderItem}
pagination={{
...listProps.pagination,
position: "top",
size: "small",
mode: "client",
}}
/>
);
const { listProps } = useSimpleList<IProduct>();

return (
<AntdList
{...listProps}
renderItem={renderItem}
pagination={{
...listProps.pagination,
position: "top",
size: "small",
mode: "client",
}}
/>
);
Remember that by default, pagination happens on the server side. If you want to do pagination handling on the client side, you can pass the pagination.mode property and set it to "client". You can also disable the pagination by setting it to "off". Sources: - Refine Blog (https://refine.dev/blog/tanstack-react-table#pagination) - Refine Documentation - useSimpleList (https://refine.dev/docs/ui-integrations/ant-design/hooks/use-simple-list#pagination) - Refine Documentation - useTable (https://refine.dev/docs/data/hooks/use-table#pagination) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai9mo ago
<@757407865391808603> kapa.ai is still learning and improving, please let us know how it did by reacting below
absent-sapphire
absent-sapphire9mo ago
i am using chakra ui @kapa.ai
kapa.ai
kapa.ai9mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- If you're using Chakra UI with refine, you can still handle client-side pagination. However, the method to implement it might be slightly different. In the context of Chakra UI, you would use the useTable hook for pagination. The useTable hook provides properties like pageSize, current, and setCurrent which you can use to manage pagination. Here's an example of how you might use these properties:
const { tableQueryResult, setFilters, current, setCurrent, pageSize } = useTable<IProduct>({
resource: "products",
queryOptions: {
initialData: products,
},
initialPageSize: 9,
meta: { populate: ["image"] },
});
const { tableQueryResult, setFilters, current, setCurrent, pageSize } = useTable<IProduct>({
resource: "products",
queryOptions: {
initialData: products,
},
initialPageSize: 9,
meta: { populate: ["image"] },
});
In this example, current is the current page, setCurrent is a function to set the current page, and pageSize is the number of items per page. You can use these properties to create a custom pagination component. For example, you might create a Pagination component like this:
export const Pagination: React.FC<PaginationProps> = ({ current, pageCount, setCurrent }) => {
const pagination = usePagination({
current,
pageCount,
});

return (
<Box display="flex" justifyContent="flex-end">
<HStack my="3" spacing="1">
{pagination?.prev && (
<IconButton
aria-label="previous page"
onClick={() => setCurrent(current - 1)}
disabled={!pagination?.prev}
variant="outline"
>
<IconChevronLeft size="18" />
</IconButton>
)}

{pagination?.items.map((page) => {
if (typeof page === "string") return <span key={page}>...</span>;

return (
<Button key={page} onClick={() => setCurrent(page)} variant={page === current ? "solid" : "outline"}>
{page}
</Button>
);
})}
{pagination?.next && (
<IconButton aria-label="next page" onClick={() => setCurrent(current + 1)} variant="outline">
<IconChevronRight size="18" />
</IconButton>
)}
</HStack>
</Box>
);
};
export const Pagination: React.FC<PaginationProps> = ({ current, pageCount, setCurrent }) => {
const pagination = usePagination({
current,
pageCount,
});

return (
<Box display="flex" justifyContent="flex-end">
<HStack my="3" spacing="1">
{pagination?.prev && (
<IconButton
aria-label="previous page"
onClick={() => setCurrent(current - 1)}
disabled={!pagination?.prev}
variant="outline"
>
<IconChevronLeft size="18" />
</IconButton>
)}

{pagination?.items.map((page) => {
if (typeof page === "string") return <span key={page}>...</span>;

return (
<Button key={page} onClick={() => setCurrent(page)} variant={page === current ? "solid" : "outline"}>
{page}
</Button>
);
})}
{pagination?.next && (
<IconButton aria-label="next page" onClick={() => setCurrent(current + 1)} variant="outline">
<IconChevronRight size="18" />
</IconButton>
)}
</HStack>
</Box>
);
};
In this component, usePagination is a hook from the @refinedev/chakra-ui package that provides pagination controls and related props. Sources: - Refine Blog (https://refine.dev/blog/handcrafted-nextjs-e-commerce-app-tutorial-strapi-chakra-ui#adding-pagination) - Refine Documentation (https://refine.dev/docs/ui-integrations/chakra-ui/introduction) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai9mo ago
<@757407865391808603> kapa.ai is still learning and improving, please let us know how it did by reacting below
absent-sapphire
absent-sapphire9mo ago
how can i provide pageCount value ? in this code @kapa.ai
import { useTable } from "@refinedev/react-table";

const {
getHeaderGroups,
getRowModel,
setOptions,
refineCore: {
setCurrent,
pageCount,
current,
tableQueryResult: { data: tableData },
},
} = useTable({
columns,
});
import { useTable } from "@refinedev/react-table";

const {
getHeaderGroups,
getRowModel,
setOptions,
refineCore: {
setCurrent,
pageCount,
current,
tableQueryResult: { data: tableData },
},
} = useTable({
columns,
});
kapa.ai
kapa.ai9mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The pageCount value is automatically calculated by the useTable hook based on the total number of records and the number of records per page (pageSize). It represents the total number of pages. If you want to manually set the pageCount, you would need to override the getList method of your data provider to return the correct total count of records. This total count is then used by useTable to calculate the pageCount. Here's an example of how you might override the getList method:
const dataProvider = {
...myDataProvider,
getList: (resource, params) => {
return myDataProvider.getList(resource, params).then((response) => {
return {
...response,
total: myTotalCount, // replace myTotalCount with your total count
};
});
},
};
const dataProvider = {
...myDataProvider,
getList: (resource, params) => {
return myDataProvider.getList(resource, params).then((response) => {
return {
...response,
total: myTotalCount, // replace myTotalCount with your total count
};
});
},
};
In this example, myDataProvider is your original data provider and myTotalCount is your total count of records. You would replace myTotalCount with the actual total count of your records. Please note that this is a general example and the actual implementation might vary depending on your data provider and your data source. Sources: - Refine Documentation - useTable (https://refine.dev/docs/data/hooks/use-table#pagecount) - Refine Github Issue (https://github.com/refinedev/refine/issues/2802) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai9mo ago
<@757407865391808603> kapa.ai is still learning and improving, please let us know how it did by reacting below