--- sidebar_label: "Table" --- import { TypeList } from "docs-ui" export const metadata = { title: `Table - Admin Components`, } # {metadata.title} The listing pages in the Admin show a table with pagination. ![Example of a table in the product listing page](https://res.cloudinary.com/dza7lstvk/image/upload/v1728295658/Medusa%20Resources/list_ddt9zc.png) To create a component that shows a table with pagination, create the file `src/admin/components/table.tsx` with the following content: ```tsx title="src/admin/components/table.tsx" import { useMemo } from "react" import { Table as UiTable } from "@medusajs/ui" export type TableProps = { columns: { key: string label?: string render?: (value: unknown) => React.ReactNode }[] data: Record[] pageSize: number count: number currentPage: number setCurrentPage: (value: number) => void } export const Table = ({ columns, data, pageSize, count, currentPage, setCurrentPage }: TableProps) => { const pageCount = useMemo(() => { return Math.ceil(data.length / pageSize) }, [data, pageSize]) const canNextPage = useMemo(() => { return currentPage < pageCount - 1 }, [currentPage, pageCount]) const canPreviousPage = useMemo(() => { return currentPage - 1 >= 0 }, [currentPage]) const nextPage = () => { if (canNextPage) { setCurrentPage(currentPage + 1) } } const previousPage = () => { if (canPreviousPage) { setCurrentPage(currentPage - 1) } } return (
{columns.map((column, index) => ( {column.label || column.key} ))} {data.map((item, index) => { const rowIndex = "id" in item ? item.id as string : index return ( {columns.map((column, index) => ( <> {column.render && column.render(item[column.key])} {!column.render && ( <>{item[column.key] as string} )} ))} ) })}
) } ``` The `Table` component uses the component from the [UI package](!ui!/components/table), with additional styling and rendering of data. It accepts the following props: React.ReactNode`", optional: true, description: "By default, the data is shown as-is in the table. You can use this function to change how the value is rendered. The function receives the value is a parameter and returns a React node." } ] }, { name: "data", type: "`Record[]`", optional: false, description: "The data to show in the table for the current page. The keys of each object should be in the `columns` array." }, { name: "pageSize", type: "`number`", optional: false, description: "The number of items to show per page." }, { name: "count", type: "`number`", optional: false, description: "The total number of items." }, { name: "currentPage", type: "`number`", optional: false, description: "A zero-based index indicating the current page's number." }, { name: "setCurrentPage", type: "`(value: number) => void`", optional: false, description: "A function used to change the current page." } ]} /> --- ## Example Use the `Table` component in any widget or UI route. For example, create the widget `src/admin/widgets/product-widget.tsx` with the following content: ```tsx title="src/admin/widgets/product-widget.tsx" import { defineWidgetConfig } from "@medusajs/admin-sdk" import { StatusBadge } from "@medusajs/ui" import { Table } from "../components/table" import { useState } from "react" import { Container } from "../components/container" const ProductWidget = () => { const [currentPage, setCurrentPage] = useState(0) return ( { const isEnabled = value as boolean return ( {isEnabled ? "Enabled" : "Disabled"} ) } } ]} data={[ { name: "John", is_enabled: true }, { name: "Jane", is_enabled: false } ]} pageSize={2} count={2} currentPage={currentPage} setCurrentPage={setCurrentPage} /> ) } export const config = defineWidgetConfig({ zone: "product.details.before", }) export default ProductWidget ``` This widget also uses the [Container](../container.mdx) custom component.