Skip to main content
Version: 4.xx.xxSource Code

useTable

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 @refinedev/core package. This means that you can use all the features of useTable hook.

Basic usage

In basic usage, useTable returns the data as it comes from the endpoint. By default, it reads resource from the URL.

http://localhost:3000
import { HttpError } from "@refinedev/core";

import { List, TagField, useTable } from "@refinedev/antd";
import { Table } from "antd";

interface IPost {
id: number;
title: string;
content: string;
status: "published" | "draft" | "rejected";
}

const PostList: React.FC = () => {
const { tableProps } = useTable<IPost, HttpError>();

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="content" title="Content" />
<Table.Column
dataIndex="status"
title="Status"
render={(value: string) => <TagField value={value} />}
/>
</Table>
</List>
);
};

Pagination

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"
pagination={{
...tableProps.pagination,
position: ["bottomCenter"],
size: "small",
}}
>
// ---
</Table>;
info

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". Also, you can disable the pagination by setting the "off".

Sorting

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.

http://localhost:3000
import { HttpError } from "@refinedev/core";

import {
List,
TagField,
useTable,
getDefaultSortOrder,
} from "@refinedev/antd";
import { Table } from "antd";

interface IPost {
id: number;
title: string;
content: string;
status: "published" | "draft" | "rejected";
}

const PostList: React.FC = () => {
const { tableProps, sorter } = useTable<IPost>({
sorters: {
initial: [
{
field: "id",
order: "desc",
},
],
},
});

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column
dataIndex="id"
title="ID"
sorter={{ multiple: 2 }}
defaultSortOrder={getDefaultSortOrder("id", sorter)}
/>
<Table.Column
dataIndex="title"
title="Title"
sorter={{ multiple: 1 }}
defaultSortOrder={getDefaultSortOrder("title", sorter)}
/>
<Table.Column dataIndex="content" title="Content" />
<Table.Column
dataIndex="status"
title="Status"
render={(value: string) => <TagField value={value} />}
/>
</Table>
</List>
);
};
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.

Filtering

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.

http://localhost:3000
import { HttpError } from "@refinedev/core";

import {
List,
TagField,
useTable,
FilterDropdown,
} from "@refinedev/antd";
import {
Table,
Radio,
} from "antd";

interface IPost {
id: number;
title: string;
content: string;
status: "published" | "draft" | "rejected";
}

const PostList: React.FC = () => {
const { tableProps } = useTable<IPost, HttpError>();

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="content" title="Content" />
<Table.Column
dataIndex="status"
title="Status"
render={(value: string) => <TagField value={value} />}
filterDropdown={(props) => (
<FilterDropdown {...props}>
<Radio.Group>
<Radio value="published">Published</Radio>
<Radio value="draft">Draft</Radio>
<Radio value="rejected">Rejected</Radio>
</Radio.Group>
</FilterDropdown>
)}
/>
</Table>
</List>
);
};

Initial Filter and Sorter

caution

If you're using the initial, don't forget to add getDefaultSortOrder or defaultFilteredValue to your <Table.Column> component. Otherwise, hook states may not sync with the table.

// ---
const { tableProps, sorters, filters } = useTable({
sorters: {
initial: [
{
field: "title",
order: "asc",
},
],
}
filters: {
initial: [
{
field: "status",
operator: "eq",
value: "published",
},
],
},
});

// ---
<Table.Column
dataIndex="title"
title="Title"
defaultSortOrder={getDefaultSortOrder("title", sorters)}
/>
<Table.Column
dataIndex="status"
title="Status"
render={(value) => <TagField value={value} />}
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.

http://localhost:3000
import { HttpError } from "@refinedev/core";

import {
List,
TagField,
useTable,
SaveButton,
} from "@refinedev/antd";
import {
Table,
Form,
Input,
} from "antd";

interface IPost {
id: number;
title: string;
content: string;
status: "published" | "draft" | "rejected";
}

interface ISearch {
title: string;
}

const PostList: React.FC = () => {
const { tableProps, searchFormProps } = 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 dataIndex="title" title="Title" />
<Table.Column dataIndex="content" title="Content" />
<Table.Column
dataIndex="status"
title="Status"
render={(value: string) => <TagField value={value} />}
/>
</Table>
</List>
);
};

Realtime Updates

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 &#8594

Properties

resource

Default: It reads the resource value from the current URL.

It will be passed to the getList method from the dataProvider as parameter via the useList hook. The parameter is usually used as an API endpoint path. It all depends on how to handle the resource in the getList method. See the creating a data provider section for an example of how resources are handled.

useTable({
resource: "categories",
});

onSearch

When 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>;
// ---

dataProviderName

If 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",
});

pagination.current

Default: 1

Sets the initial value of the page index.

useTable({
pagination: {
current: 2,
},
});

pagination.pageSize

Default: 10

Sets the initial value of the page size.

useTable({
pagination: {
pageSize: 20,
},
});

pagination.mode

Default: "server"

It can be "off", "server" or "client".

  • "off": Pagination is disabled. All records will be fetched.
  • "client": Pagination is done on the client side. All records will be fetched and then the records will be paginated on the client side.
  • "server":: Pagination is done on the server side. Records will be fetched by using the current and pageSize values.
useTable({
pagination: {
mode: "client",
},
});

sorters.initial

Sets the initial value of the sorter. The initial is not permanent. It will be cleared when the user changes the sorter. If you want to set a permanent value, use the sorters.permanent prop.

Refer to the CrudSorting interface for more information &#8594

useTable({
sorters: {
initial: [
{
field: "name",
order: "asc",
},
],
},
});

sorters.permanent

Sets the permanent value of the sorter. The permanent 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 sorters.initial prop.

Refer to the CrudSorting interface for more information &#8594

useTable({
sorters: {
permanent: [
{
field: "name",
order: "asc",
},
],
},
});

filters.initial

Sets the initial value of the filter. The initial is not permanent. It will be cleared when the user changes the filter. If you want to set a permanent value, use the filters.permanent prop.

Refer to the CrudFilters interface for more information &#8594

useTable({
filters: {
initial: [
{
field: "name",
operator: "contains",
value: "Foo",
},
],
},
});

filters.permanent

Sets the permanent value of the filter. The permanent 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 filters.initial prop.

Refer to the CrudFilters interface for more information &#8594

useTable({
filters: {
permanent: [
{
field: "name",
operator: "contains",
value: "Foo",
},
],
},
});

filters.defaultBehavior

Default: 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({
filters: {
defaultBehavior: "replace",
},
});

syncWithLocation

Default: 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,
});

queryOptions

useTable uses useList hook to fetch data. You can pass queryOptions.

useTable({
queryOptions: {
retry: 3,
},
});

meta

meta is used following two purposes:

  • To pass additional information to data provider methods.
  • Generate GraphQL queries using plain JavaScript Objects (JSON). Please refer GraphQL for more information.

In the following example, we pass the headers property in the meta object to the create method. With similar logic, you can pass any properties to specifically handle the data provider methods.

useTable({
meta: {
headers: { "x-meta-data": "true" },
},
});

const myDataProvider = {
//...
getList: async ({
resource,
pagination,
sorters,
filters,
meta,
}) => {
const headers = meta?.headers ?? {};
const url = `${apiUrl}/${resource}`;

//...
//...

const { data, headers } = await httpClient.get(`${url}`, { headers });

return {
data,
};
},
//...
};

successNotification

NotificationProvider is 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

NotificationProvider is 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

LiveProvider is 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

LiveProvider is 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

LiveProvider is required for this prop to work.

Params to pass to liveProvider's subscribe method.

initialCurrent

Deprecated

Use pagination.current instead.

Default: 1

Sets the initial value of the page index.

useTable({
initialCurrent: 2,
});

initialPageSize

Deprecated

Use pagination.pageSize instead.

Default: 10

Sets the initial value of the page size.

useTable({
initialPageSize: 20,
});

hasPagination

Deprecated

Use pagination.mode instead.

Default: true

Determines whether to use server-side pagination or not.

useTable({
hasPagination: false,
});

initialSorter

Deprecated

Use sorters.initial instead.

Sets 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",
},
],
});

permanentSorter

Deprecated

Use sorters.permanent instead.

Sets 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",
},
],
});

initialFilter

Deprecated

Use filters.initial instead.

Sets 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",
},
],
});

permanentFilter

Deprecated

Use filters.permanent instead.

Sets 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",
},
],
});

defaultSetFilterBehavior

Deprecated

Use filters.defaultBehavior instead.

Default: 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",
});

Return Values

tableProps

The props needed by the <Table> component.

onChange

The callback function is executed when a user interacts(filter, sort, etc.) with the table.

🚨 useTable handles 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>

dataSource

Contains the data to be displayed in the table. Values fetched with useList hook.

loading

Indicates whether the data is being fetched.

pagination

Returns pagination configuration values(pageSize, current, position, etc.).

scroll

Default: { x: true }

Whether the table can be scrollable.

searchFormProps

It 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 { HttpError } from "@refinedev/core";
import { List, useTable, SaveButton } from "@refinedev/antd";
import { Table, Form, Input } from "antd";

interface IPost {
id: number;
title: string;
}

interface ISearch {
title: string;
}

const PostList: React.FC = () => {
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>
);
};

tableQueryResult

Returned values from useList hook.

sorters

Current sorters state.

setSorters

A function to set current sorters state.

 (sorters: CrudSorting) => void;

filters

Current filters state.

setFilters

((filters: CrudFilters, behavior?: SetFilterBehavior) => void) & ((setter: (prevFilters: CrudFilters) => CrudFilters) => void)

A function to set current filters state.

current

Current page index state. If pagination is disabled, it will be undefined.

setCurrent

React.Dispatch<React.SetStateAction<number>> | undefined;

A function to set the current page index state. If pagination is disabled, it will be undefined.

pageSize

Current page size state. If pagination is disabled, it will be undefined.

setPageSize

React.Dispatch<React.SetStateAction<number>> | undefined;

A function to set the current page size state. If pagination is disabled, it will be undefined.

pageCount

Total 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.

sorter

Deprecated

Use sorters instead.

Current sorters state.

setSorter

Deprecated

Use setSorters instead.

A function to set current sorters state.

 (sorters: CrudSorting) => void;

FAQ

How can I handle relational data?

You can use useMany hook to fetch relational data and filter <Table> by categories with help of useSelect

http://localhost:3000
import {
HttpError,
useMany,
getDefaultFilter,
} from "@refinedev/core";

import {
List,
TagField,
useTable,
useSelect,
FilterDropdown,
} from "@refinedev/antd";
import {
Table,
Select,
} from "antd";

interface ICategory {
id: number;
title: string;
}

interface IPost {
id: number;
title: string;
content: string;
status: "published" | "draft" | "rejected";
category: {
id: number;
};
}

const PostList: React.FC = () => {
const { tableProps, filters } = useTable<IPost, HttpError>();

// Creates the array of ids. This will filter and fetch the category data for the relevant posts.
const categoryIds =
tableProps.dataSource?.map((p) => p.category.id.toString()) || [];
// Fetches the category of each post. It uses the useMany hook to fetch the category data from the API.
const { data, isFetching } = useMany<ICategory>({
resource: "categories",
ids: categoryIds,
queryOptions: {
// Set to true only if the posts array is not empty.
enabled: categoryIds.length > 0,
},
});

// Creates the props by needed the select component for filtering the posts by category.
const { selectProps } = useSelect<ICategory>({
resource: "categories",
optionLabel: "title",
optionValue: "id",
defaultValue: getDefaultFilter("category.id", filters, "in"),
});

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
<Table.Column dataIndex="content" title="Content" />
<Table.Column
dataIndex={["category", "id"]}
title="Category"
render={(value) => {
if (isFetching) return "loading...";
// Gets the title of the category from the data object, which is the result of the useMany hook.
return data?.data.find((p) => p.id === value)?.title;
}}
filterDropdown={(props: FilterDropdownProps) => (
<FilterDropdown
{...props}
mapValue={(selectedKeys) =>
selectedKeys.map((i) => parseInt(i.toString()))
}
>
<Select
style={{ minWidth: 200 }}
mode="multiple"
{...selectProps}
/>
</FilterDropdown>
)}
/>
<Table.Column
dataIndex="status"
title="Status"
render={(value: string) => <TagField value={value} />}
/>
</Table>
</List>
);
};

API

Properties

Type Parameters

PropertyDesriptionTypeDefault
TQueryFnDataResult data returned by the query function. Extends BaseRecordBaseRecordBaseRecord
TErrorCustom error object that extends HttpErrorHttpErrorHttpError
TSearchVariablesValues for search params{}
TDataResult data returned by the select function. Extends BaseRecord. If not specified, the value of TQueryFnData will be used as the default value.BaseRecordTQueryFnData

Return values

PropertyDescriptionType
searchFormPropsAnt Design <Form> propsFormProps<TSearchVariables>
tablePropsAnt Design <Table> propsTableProps<TData>
tableQueryResultResult of the react-query's useQueryQueryObserverResult<{`` data: TData[];`` total: number; },`` TError>
totalPageTotal page count (returns undefined if pagination is disabled)number | undefined
currentCurrent page index state (returns undefined if pagination is disabled)number | undefined
setCurrentA function that changes the current (returns undefined if pagination is disabled)React.Dispatch<React.SetStateAction<number>> | undefined
pageSizeCurrent pageSize state (returns undefined if pagination is disabled)number | undefined
setPageSizeA function that changes the pageSize. (returns undefined if pagination is disabled)React.Dispatch<React.SetStateAction<number>> | undefined
sortersCurrent sorting stateCrudSorting
setSortersA function that accepts a new sorters state.(sorters: CrudSorting) => void
sorterCurrent sorting stateCrudSorting
setSorterA function that accepts a new sorters state.(sorters: CrudSorting) => void
filtersCurrent filters stateCrudFilters
setFiltersA function that accepts a new filter state- (filters: CrudFilters, behavior?: "merge" | "replace" = "merge") => void - (setter: (previousFilters: CrudFilters) => CrudFilters) => void

Example

RUN IN YOUR LOCAL
npm create refine-app@latest -- --example table-antd-use-table