React Table Part 2: Sorting and Filtering

Published on 9 March 2023 at 13:51

React Table is a popular library that simplifies the process of creating complex tables in React applications. In a previous blog post, we covered the basics of React Table and created a simple table. In the previous post (Part 1), we defined "column.js", "MOCK_DATA.json", "table.css" and the BasicTable. Therefore, we will not repeat the same but instead will continue form there. if it gets confusing, kindly let us know it the comment below so that we may help. In this post, we will explore some more advanced features of React Table, such as sorting and filtering.

 

Sorting

 

Sorting is a common feature in tables that allows users to organize data based on a specific column. React Table makes it easy to implement sorting with the useSortBy hook.

First, we need to import the useSortBy hook from React Table:

import { useTable, useSortBy } from 'react-table'

Next, we can add the useSortBy hook to our useTable function:

const {

getTableProps,

getTableBodyProps,

headerGroups,

footer groups,

rows,

prepareRow,

} = useTable({

columns,

dates, },

useSortBy)

 

Now, we can add sorting functionality to our table headers and footers. We can do this by mapping through the headerGroups and footerGroups arrays and rendering each column header with a sort button. The column.getSortByToggleProps() function returns props that we can spread onto the header element to enable sorting. We can also add an arrow icon to indicate the sorting direction:

 

Now let us look at the result:

 

Fig. 1: Table before sort

Fig. 2: Table  with country sorted from A - B

 

Fig. 3: Table with country sorted from Z - A

 

Filtering

 

Filtering is another common feature in tables that allows users to search for specific data within a table. React Table provides us with the useFilters and useGlobalFilter hooks to enable filtering.

First, we need to create a filter component that will render a search input field. This component will be used for individual column filtering:

This component receives the column object as a prop and extracts the filterValue and setFilter functions from it. filterValue represents the current filter value for the column, while setFilter is a function that sets the new filter value when the user types something in the input field.

 

We can also add a global filter component that will search for data across all columns. This component will be used to search for data in the entire table:

 

This component receives the filter and setFilter functions as props from React Table. filter represents the current global filter value, while setFilter is a function that sets the new global filter value when the user types something in the input field.

 

Notice that we are using the useAsyncDebounce hook from React Table. This hook debounces the user's input by a certain number of milliseconds (in this case, 1000), which means that the setFilter function will only be called after the user has finished typing for at least that amount of time. This helps to reduce unnecessary rendering and improve performance.

 

Now that we have our two filter components, let's integrate them into our table. First, we need to modify our columns array to include the Filter property for each column that we want to be filterable:

Now, let us look at the results:

 

Fig. 4:  Table before filter/search

 

Fig. 5: Table with a globa filter that searches through the entire table where there is "Te".

 

Fig. 6: Table with column filter that searches through a specific column(First Name) where there is "Te".

 

In conclusion, creating a filtering and sorting table using React-Table library is a powerful way to manage and display data in a customizable way. It provides many useful features such as pagination, sorting, and filtering, which can greatly improve the user experience of your web application. We have covered how to implement these features step-by-step, from installing the library to using various hooks such as useTable, useFilters, and useGlobalFilter.

 

While React-Table provides many built-in options for customization, it is also highly extensible and allows for more advanced customizations if needed. By following this guide, you should have a solid foundation for creating your own filtering and sorting table using React-Table. Overall, React-Table is a great choice for managing data in a user-friendly way and can greatly enhance the functionality and user experience of your web application.

 

In the next post, we will talk about pagination and row selecting and other complex things related to React-tables. We hope this will help you through your developer journey. If you like what we are doing comment below and  follow us on our social platforms where we interact with readers.

Add comment

Comments

There are no comments yet.