5. Adding Sort and Filters
In the previous Adding List Page section, we have displayed blog posts data in a table. Now we will learn how to add sorting and filtering to the table to user can have more control over the data.
Adding Sorting
We will use <Table.Column/>'s sorter prop to add sorting to the table.
Open
src/pages/blog-posts/list.tsxfile.Add
sorterprop to the<Table.Column/>component of theidcolumn.<Table.Column
dataIndex="id"
title="Id"
sorter
/>Now, you can sort the data by
idon the table.If you want to add multiple sorting capabilities to the table, you can add
sorterprop by priority. For example, if you want to sort the data byidandnameon the table, you can addsorterprop as below:<Table.Column
dataIndex="id"
title="Id"
sorter
/>
<Table.Column
dataIndex="id"
title="Name"
sorter={{ multiple: 1 }}
/>Now, the data will be sorted by
idfirst and then byname.
Adding Filters
We will use <Table.Column/>'s filterDropdown prop and <FilterDropdown/> component to add filters to the table.
<FilterDropdown/> component is a wrapper component. It serves as a bridge between its child input and refine's useTable hook. It provides the necessary props to the child input and handles the logic of filtering the data. It also provides a filter and clear button to make the filtering process easier.
Refer to the <FilterDropdown/> documentation for more information →
Also, to get more information about the filterDropdown prop, you can refer to the Ant Design's <Table/> documentation.
In this tutorial, we will add filters capabilities to the category column. To do this, we will use <Select/> component from Ant Design as the child input of <FilterDropdown/> component. So, we will also use useSelect hook to get the necessary props like options for <Select/> component.
Refer to the Ant Desing <Select/> documentation for more information →
Refer to the useSelect documentation for more information →
Open
src/pages/blog-posts/list.tsxfile and import the following components and hooks like below.import { FilterDropdown, Select, useSelect } from "@pankod/refine-antd";Call
useSelecthook withcategoriesresource to fill the<Select/>component with category options.const { selectProps: categorySelectProps } = useSelect({
resource: "categories",
});Then, follow the steps below:
- Add
filterDropdownprop to the<Table.Column/>component of thecategorycolumn. - Add the
<FilterDropdown/>component as the return value of the prop. Then, pass thepropsto the<FilterDropdown/>component. - Add the
<Select/>component as the child of<FilterDropdown/>component. Then, pass thecategorySelectPropsto the<Select/>component.
<Table.Column
dataIndex={["category", "id"]}
title="category"
render={(value) => {
if (isLoading) {
return <TextField value="Loading..." />;
}
return (
<TextField
value={categoriesData?.data.find((item) => item.id === value)?.title}
/>
);
}}
filterDropdown={(props) => (
<FilterDropdown {...props}>
<Select
style={{ minWidth: 200 }}
mode="multiple"
placeholder="Select Category"
{...categorySelectProps}
/>
</FilterDropdown>
)}
/>- Add
Now, you can search and filter the data by category on the table.