documentation/versioned_docs/version-3.xx.xx/api-reference/antd/hooks/table/useTable.md
import BasicUsageLivePreview from "./_partial-use-table-basic-usage-live-preview.md"; import SorterLivePreview from "./_partial-use-table-sorter-live-preview.md"; import FilteringPreview from "./_partial-use-table-filtering-live-preview.md"; import SearchPreview from "./_partial-use-table-search-live-preview.md"; import RelationalLivePreview from "./_partial-use-table-relational-live-preview.md"; import PropResource from "@site/src/partials/prop-resource";
By using useTable, you can get properties that are compatible with Ant Design <Table> component. All features such as sorting, filtering, and pagination come out of the box. Under the hood it uses useList for the fetch.
For all the other features, you can refer to the Ant Design <Table> documentation.
:::info
useTable hook is extended from useTable hook from the @pankod/refine-core package. This means that you can use all the features of useTable hook.
:::
In basic usage, useTable returns the data as it comes from the endpoint. By default, it reads resource from the URL.
This feature comes out of the box with the tableProps.pagination. It generates the pagination links for the <Table> component instead of react state and overrides <Table>'s pagination.itemRender value. It also syncs the pagination state with the URL.
It also syncs the pagination state with the URL if you enable the syncWithLocation.
If you want to make a change in the pagination of the <Table>. You should pass the pagination object of the tableProps to the pagination property of the <Table> as below. You can override the values of the pagination object as your need.
const { tableProps } = useTable<IPost>();
<Table
{...tableProps}
rowKey="id"
// highlight-start
pagination={{
...tableProps.pagination,
position: ["bottomCenter"],
size: "small",
}}
// highlight-end
>
// ---
</Table>;
:::info
To disable pagination, you can set hasPagination property to false which is true by default. If hasPagination has set to false, pagination elements will be hidden in the <Table/>. If you want to handle the pagination on the client-side you can override the pagination property in tableProps.
:::
If we want to give a column the sorting property, the corresponding <Table.Column> component must be given the sorter property.
It also syncs the sorting state with the URL if you enable the syncWithLocation.
:::caution
During the sorting process, the key property of your <Column /> component is used as the property name in the API request. If your Column component does not have a key value, the dataIndex property is used.
It can be used when your DataIndex and your sorting key are different.
:::
:::tip
When using multiple sorting, multiple value is required for sorter property. Which specifies the priority of the column in sorting.
:::
We can use the filterDropdown property from <Table.Column> to make filtering based on the column values. In order to do this, we need to put the filtering form inside the <FilterDropdown> component and pass the properties coming to the function to these component's properties.
It also syncs the filtering state with the URL if you enable the syncWithLocation.
:::caution
If you're using the initialSorter or initialFilter, don't forget to add getDefaultSortOrder or defaultFilteredValue to your <Table.Column> component. Otherwise, during filter and paging operations, the initialSorter might be lost.
// ---
const { tableProps, sorter, filters } = useTable({
initialSorter: [
{
field: "title",
order: "asc",
},
],
initialFilter: [
{
field: "status",
operator: "eq",
value: "published",
},
],
});
// ---
<Table.Column
dataIndex="title"
title="Title"
// highlight-next-line
defaultSortOrder={getDefaultSortOrder("title", sorter)}
/>
<Table.Column
dataIndex="status"
title="Status"
render={(value) => <TagField value={value} />}
// highlight-next-line
defaultFilteredValue={getDefaultFilter("status", filters)}
filterDropdown={(props) => (
<FilterDropdown {...props}>
<Radio.Group>
<Radio value="published">Published</Radio>
<Radio value="draft">Draft</Radio>
<Radio value="rejected">Rejected</Radio>
</Radio.Group>
</FilterDropdown>
)}
/>
// ---
:::
We can use onSearch and searchFormProps properties to make custom filter form. onSearch is a function that is called when the form is submitted. searchFormProps is a property that is passed to the <Form> component.
This feature is only available if you use a Live Provider.
When the useTable hook is mounted, it will call the subscribe method from the liveProvider with some parameters such as channel, resource etc. It is useful when you want to subscribe to live updates.
Refer to the liveProvider documentation for more information →
resource<PropResource hook={{ name:"useList", URL:"/docs/api-reference/core/hooks/data/useList/" }} method={{ name:"getList", URL:"/docs/api-reference/core/providers/data-provider/#getlist" }} />
useTable({
resource: "categories",
});
onSearchWhen searchFormProps.onFinish is called, the onSearch function is called with the values of the form. The onSearch function should return CrudFilters | Promise<CrudFilters>.
Also, onSearch will set the current page to 1.
It's useful when you want to filter the data with any query.
const { searchFormProps, tableProps } = useTable({
onSearch: (values) => {
return [
{
field: "title",
operator: "contains",
value: values.title,
},
];
},
});
// --
<List>
<Form {...searchFormProps}>
<Space>
<Form.Item name="title">
<Input placeholder="Search by title" />
</Form.Item>
<SaveButton onClick={searchFormProps.form?.submit} />
</Space>
</Form>
<Table {...tableProps} rowKey="id">
<Table.Column title="Title" dataIndex="title" />
</Table>
</List>;
// ---
dataProviderNameIf there is more than one dataProvider, you should use the dataProviderName that you will use. It is useful when you want to use a different dataProvider for a specific resource.
useTable({
dataProviderName: "second-data-provider",
});
initialCurrentDefault:
1
Sets the initial value of the page index.
useTable({
initialCurrent: 2, // This will cause the table to initially display the data for page 2, rather than the default of page 1
});
initialPageSizeDefault:
10
Sets the initial value of the page size.
useTable({
initialPageSize: 20, // This will cause the table to initially display 20 rows per page, rather than the default of 10
});
initialSorterSets the initial value of the sorter. The initialSorter is not permanent. It will be cleared when the user changes the sorter. If you want to set a permanent value, use the permanentSorter prop.
useTable({
initialSorter: [
{
field: "title",
order: "asc",
},
],
});
permanentSorterSets the permanent value of the sorter. The permanentSorter is permanent and unchangeable. It will not be cleared when the user changes the sorter. If you want to set a temporary value, use the initialSorter prop.
useTable({
permanentSorter: [
{
field: "title",
order: "asc",
},
],
});
initialFilterSets the initial value of the filter. The initialFilter is not permanent. It will be cleared when the user changes the filter. If you want to set a permanent value, use the permanentFilter prop.
useTable({
initialFilter: [
{
field: "title",
operator: "contains",
value: "Foo",
},
],
});
permanentFilterSets the permanent value of the filter. The permanentFilter is permanent and unchangeable. It will not be cleared when the user changes the filter. If you want to set a temporary value, use the initialFilter prop.
useTable({
permanentFilter: [
{
field: "title",
operator: "contains",
value: "Foo",
},
],
});
defaultSetFilterBehaviorDefault:
merge
The filtering behavior can be set to either "merge" or "replace".
When the filter behavior is set to "merge", it will merge the new filter with the existing filters. This means that if the new filter has the same column as an existing filter, the new filter will replace the existing filter for that column. If the new filter has a different column than the existing filters, it will be added to the existing filters.
When the filter behavior is set to "replace", it will replace all existing filters with the new filter. This means that any existing filters will be removed and only the new filter will be applied to the table.
You can also override the default value by using the second parameter of the setFilters function.
useTable({
defaultSetFilterBehavior: "replace",
});
hasPaginationDefault:
true
Determines whether to use server-side pagination or not.
useTable({
hasPagination: false,
});
syncWithLocationDefault:
false
When you use the syncWithLocation feature, the useTable's state (e.g. sort order, filters, pagination) is automatically encoded in the query parameters of the URL, and when the URL changes, the useTable state is automatically updated to match. This makes it easy to share table state across different routes or pages, and to allow users to bookmark or share links to specific table views.
Also, you can set this value globally on <Refine> component.
useTable({
syncWithLocation: true,
});
queryOptionsuseTable uses useList hook to fetch data. You can pass queryOptions.
useTable({
queryOptions: {
retry: 3,
},
});
metaDatametaData is used following two purposes:
In the following example, we pass the headers property in the metaData object to the create method. With similar logic, you can pass any properties to specifically handle the data provider methods.
useTable({
// highlight-start
metaData: {
headers: { "x-meta-data": "true" },
},
// highlight-end
});
const myDataProvider = {
//...
getList: async ({
resource,
pagination,
hasPagination,
sort,
filters,
// highlight-next-line
metaData,
}) => {
// highlight-next-line
const headers = metaData?.headers ?? {};
const url = `${apiUrl}/${resource}`;
//...
//...
// highlight-next-line
const { data, headers } = await httpClient.get(`${url}`, { headers });
return {
data,
};
},
//...
};
successNotification
NotificationProvideris required for this prop to work.
After data is fetched successfully, useTable can call open function from NotificationProvider to show a success notification. With this prop, you can customize the success notification.
useTable({
successNotification: (data, values, resource) => {
return {
message: `${data.title} Successfully fetched.`,
description: "Success with no errors",
type: "success",
};
},
});
errorNotification
NotificationProvideris required for this prop to work.
After data fetching is failed, useTable will call open function from NotificationProvider to show an error notification. With this prop, you can customize the error notification.
useTable({
errorNotification: (data, values, resource) => {
return {
message: `Something went wrong when getting ${data.id}`,
description: "Error",
type: "error",
};
},
});
liveMode
LiveProvideris required for this prop to work.
Determines whether to update data automatically ("auto") or not ("manual") if a related live event is received. It can be used to update and show data in Realtime throughout your app. For more information about live mode, please check Live / Realtime page.
useTable({
liveMode: "auto",
});
onLiveEvent
LiveProvideris required for this prop to work.
The callback function is executed when new events from a subscription have arrived.
useTable({
onLiveEvent: (event) => {
console.log(event);
},
});
liveParams
LiveProvideris required for this prop to work.
Params to pass to liveProvider's subscribe method.
tablePropsThe props needed by the <Table> component.
onChangeThe callback function is executed when a user interacts(filter, sort, etc.) with the table.
🚨
useTablehandles sorting, filtering, and pagination with this function. If you override this function, you need to handle these operations manually.
const { tableProps } = useTable()
<Table {...tableProps} onChange={tableProps.onChange} rowKey="id">
<Table.Column title="Title" dataIndex="title" />
</Table>
dataSourceContains the data to be displayed in the table. Values fetched with useList hook.
loadingIndicates whether the data is being fetched.
paginationReturns pagination configuration values(pageSize, current, position, etc.).
scrollDefault:
{ x: true }
Whether the table can be scrollable.
searchFormPropsIt returns <Form> instance of Ant Design. When searchFormProps.onFinish is called, it will trigger onSearch function.
You can also use searchFormProps.form.submit to submit the form manually.
It's useful when you want to create a filter form for your <Table>.
import { IResourceComponentsProps, HttpError } from "@pankod/refine-core";
import {
List,
Table,
useTable,
Form,
SaveButton,
Input,
} from "@pankod/refine-antd";
interface IPost {
id: number;
title: string;
}
interface ISearch {
title: string;
}
const PostList: React.FC<IResourceComponentsProps> = () => {
const { searchFormProps, tableProps } = useTable<IPost, HttpError, ISearch>({
onSearch: (values) => {
return [
{
field: "title",
operator: "contains",
value: values.title,
},
];
},
});
return (
<List>
<Form {...searchFormProps} layout="inline">
<Form.Item name="title">
<Input placeholder="Search by title" />
</Form.Item>
<SaveButton onClick={searchFormProps.form?.submit} />
</Form>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column title="Title" dataIndex="title" />
</Table>
</List>
);
};
tableQueryResultReturned values from useList hook.
sorterCurrent sorter state.
setSorter (sorter: CrudSorting) => void;
A function to set current sorter state.
filtersCurrent filters state.
setFilters((filters: CrudFilters, behavior?: SetFilterBehavior) => void) & ((setter: (prevFilters: CrudFilters) => CrudFilters) => void)
A function to set current filters state.
currentCurrent page index state. If pagination is disabled, it will be undefined.
setCurrentReact.Dispatch<React.SetStateAction<number>> | undefined;
A function to set the current page index state. If pagination is disabled, it will be undefined.
pageSizeCurrent page size state. If pagination is disabled, it will be undefined.
setPageSizeReact.Dispatch<React.SetStateAction<number>> | undefined;
A function to set the current page size state. If pagination is disabled, it will be undefined.
pageCountTotal page count state. If pagination is disabled, it will be undefined.
createLinkForSyncWithLocation(params: SyncWithLocationParams) => string;
A function creates accessible links for syncWithLocation. It takes an SyncWithLocationParams as parameters.
You can use useMany hook to fetch relational data and filter <Table> by categories with help of useSelect
| Property | Desription | Type | Default |
|---|---|---|---|
| TData | Result data of the query. Extends BaseRecord | BaseRecord | BaseRecord |
| TError | Custom error object that extends HttpError | HttpError | HttpError |
| TSearchVariables | Values for search params | {} |
| Property | Description | Type |
|---|---|---|
| searchFormProps | Ant Design <Form> props | FormProps<TSearchVariables> |
| tableProps | Ant Design <Table> props | TableProps<TData> |
| tableQueryResult | Result of the react-query's useQuery | QueryObserverResult<{`` data: TData[];`` total: number; },`` TError> |
| current | Current page index state (returns undefined if pagination is disabled) | number | undefined |
| totalPage | Total page count (returns undefined if pagination is disabled) | number | undefined |
| setCurrent | A function that changes the current (returns undefined if pagination is disabled) | React.Dispatch<React.SetStateAction<number>> | undefined |
| pageSize | Current pageSize state (returns undefined if pagination is disabled) | number | undefined |
| setPageSize | A function that changes the pageSize. (returns undefined if pagination is disabled) | React.Dispatch<React.SetStateAction<number>> | undefined |
| sorter | Current sorting state | CrudSorting |
| filters | Current filters state | CrudFilters |
| setFilters | A function that accepts a new filter state | - (filters: CrudFilters, behavior?: "merge" | "replace" = "merge") => void - (setter: (previousFilters: CrudFilters) => CrudFilters) => void |
| setSorter | A function that accepts a new sorter state. | (sorter: CrudSorting) => void |