Tanstack Tables (formerly known as React Table JS) is a powerful and flexible data table library for React. One of its many useful features is the ability to apply both global and column filters simultaneously. This allows you to easily narrow down your data set to only the most relevant information. In this article, we will go over how to use global and column filters together in Tanstack Tables.
Global Filters
Global filters are applied to all columns in the table. They are useful when you want to quickly narrow down the data set based on a particular value. For example, you might want to filter a list of products by only showing those that are in stock.
To add a global filter to your table, you will need to use the useGlobalFilter hook. This hook provides a state and a setter for the global filter value. Here is an example of how to use the useGlobalFilter hook:
const {
globalFilter,
setGlobalFilter,
} = useGlobalFilter({
defaultValue: '',
});
In this example, the defaultValue prop is set to an empty string, which means that no filter will be applied when the table is first rendered. You can then use the globalFilter state to set the filter value, like this:
setGlobalFilter('in stock');
This will filter the table to only show rows where the in stock value is present in any column.
Column Filters
Column filters are applied to individual columns in the table. They are useful when you want to filter a column by a specific value. For example, you might want to filter a list of products by only showing those that are in a particular category.
To add a column filter to your table, you will need to use the useColumnFilter hook. This hook provides a state and a setter for the column filter value. Here is an example of how to use the useColumnFilter hook:
const {
columnFilters,
setColumnFilters,
} = useColumnFilter({
column: 'category',
defaultValue: [],
});
In this example, the column prop is set to the name of the column that you want to filter. The defaultValue prop is set to an empty array, which means that no filter will be applied when the table is first rendered. You can then use the setColumnFilters setter to set the filter value, like this:
setColumnFilters(['electronics']);
This will filter the table to only show rows where the category column is equal to electronics.
Using Global and Column Filters Together
You can use global and column filters together to create powerful and flexible data tables. To do this, you will need to use the useGlobalFilter and useColumnFilter hooks together. Here is an example of how to use these hooks together:
const {
globalFilter,
setGlobalFilter,
} = useGlobalFilter({
defaultValue: '',
});
const {
columnFilters,
setColumnFilters,
} = useColumnFilter({
column: 'category',
defaultValue: [],
});
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable({
columns,
data,
globalFilter,
columnFilters,
});
In this example, the useTable hook is used to create the table. The globalFilter and columnFilters states are passed to the useTable hook as props. This allows the table to use both global and column filters simultaneously.
You can then use the getTableProps and getTableBodyProps functions to render the table and its body. The headerGroups and rows arrays can be used to render the table headers and rows, respectively. The prepareRow function is used to prepare the row data for rendering.
Here is an example of how to render the table headers:
{headerGroups.map(headerGroup => (
{headerGroup.headers.map(column => (
{column.render('Header')}
{column.canFilter && (
{column.render('Filter')}
)}
))}
))}
In this example, the headerGroups and columns arrays are used to render the table headers. The canFilter prop is used to check if the column can be filtered. If the column can be filtered, the Filter component is rendered. This component is provided by the useTable hook and is used to render the filter UI for the column.
Here is an example of how to render the table rows:
{rows.map(row => {
prepareRow(row);
return (
{row.cells.map(cell => (
{cell.render('Cell')}
))}
);
})}
In this example, the rows array is used to render the table rows. The cells array is used to render the cells for each row. The getCellProps function is used to set the props for each cell. The render function is used to render the cell content.
Tanstack Tables (React Table JS) is a powerful and flexible data table library for React. It allows you to easily create data tables with advanced features, such as global and column filters. By using these features together, you can create powerful and flexible data tables that allow your users to quickly and easily find the information they need.
| Reference | Description |
|---|---|
| Global Filter | Documentation for the global filter feature in Tanstack Tables (React Table JS). |
| Column Filter | Documentation for the column filter feature in Tanstack Tables (React Table JS). |
| useTable | Documentation for the useTable hook in Tanstack Tables (React Table JS). |