Restore filters on refresh

When I use useTable to display data and one of my column is filtered using filterDropdown and a Select component, I have filter parameters reflected in URL since using also syncWithLocation flag. When I refresh the page using F5, how my filters from url could be restored to the select used to filter my column ?
5 Replies
kapa.ai
kapa.ai3w ago
Hi @quick691. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To restore filters on page refresh when using useTable with syncWithLocation, you need to use the getDefaultFilter helper function from @refinedev/core to populate the column's default filter value from the URL parameters. Here's how you can implement this:
import { getDefaultFilter } from "@refinedev/core";
import { useTable } from "@refinedev/core";
import { Table, FilterDropdown, Select } from "@refinedev/antd";

const MyComponent = () => {
const { tableProps, filters } = useTable({
syncWithLocation: true,
});

return (
<Table {...tableProps}>
<Table.Column
dataIndex="myField"
title="My Field"
defaultFilteredValue={getDefaultFilter("myField", filters, "eq")}
filterDropdown={(props) => (
<FilterDropdown {...props}>
<Select
style={{ minWidth: 200 }}
placeholder="Select an option"
options={[
{ label: "Option 1", value: "option1" },
{ label: "Option 2", value: "option2" }
]}
/>
</FilterDropdown>
)}
/>
</Table>
);
};
import { getDefaultFilter } from "@refinedev/core";
import { useTable } from "@refinedev/core";
import { Table, FilterDropdown, Select } from "@refinedev/antd";

const MyComponent = () => {
const { tableProps, filters } = useTable({
syncWithLocation: true,
});

return (
<Table {...tableProps}>
<Table.Column
dataIndex="myField"
title="My Field"
defaultFilteredValue={getDefaultFilter("myField", filters, "eq")}
filterDropdown={(props) => (
<FilterDropdown {...props}>
<Select
style={{ minWidth: 200 }}
placeholder="Select an option"
options={[
{ label: "Option 1", value: "option1" },
{ label: "Option 2", value: "option2" }
]}
/>
</FilterDropdown>
)}
/>
</Table>
);
};
For fields that require type conversion (like numbers or booleans), you'll need to handle the conversion manually. The getDefaultFilter function returns the filter value as a string from the URL. Note that there have been some issues with this functionality in the past, as mentioned in GitHub Issue #6001, but using the getDefaultFilter helper is the recommended approach. If you're using nested fields or fields with special characters, make sure to use the correct field path:
defaultFilteredValue={getDefaultFilter("nestedField][id", filters, "eq")}
defaultFilteredValue={getDefaultFilter("nestedField][id", filters, "eq")}
This approach will ensure that your filters are properly restored from the URL when the page is refreshed. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@347285614531444756> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
conscious-sapphire
conscious-sapphireOP3w ago
@kapa.ai I'm already using defaultFilteredValue={getDefaultFilter( "sensor_template_id", filters, "eq" )} my filters object coming from useParsed() is the following showing sensor_template_id is set (filters":[{"field":"sensor_template_id","operator":"in","value":["1"]}]) but my select used to filter is not updating accordingly
kapa.ai
kapa.ai3w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I see the issue you're facing. The problem might be related to a type mismatch between the value in the URL (which is a string) and the value expected by the Select component (which might be a number or another type). This is a known issue in refine, as mentioned in Github Issue #4863, where the binding of useSelect in filters with Ant Design doesn't work correctly when refreshing the page. There are a few potential solutions: 1. Make sure you're using filteredValue instead of defaultFilteredValue. As mentioned in Github Issue #4755, replacing defaultFilteredValue with filteredValue can solve this issue because defaultFilteredValue only runs on the initial render. 2. If you're using a Select component that expects numbers, you might need to convert the string values from the URL to numbers:
<Table.Column
dataIndex="sensor_template_id"
title="Sensor Template"
filteredValue={getDefaultFilter("sensor_template_id", filters, "in")}
filterDropdown={(props) => (
<FilterDropdown
{...props}
mapValue={(selectedKeys) =>
selectedKeys.map((i) => parseInt(i.toString()))
}
>
<Select
style={{ minWidth: 200 }}
mode="multiple"
placeholder="Select Sensor Template"
{...sensorTemplateSelectProps}
/>
</FilterDropdown>
)}
/>
<Table.Column
dataIndex="sensor_template_id"
title="Sensor Template"
filteredValue={getDefaultFilter("sensor_template_id", filters, "in")}
filterDropdown={(props) => (
<FilterDropdown
{...props}
mapValue={(selectedKeys) =>
selectedKeys.map((i) => parseInt(i.toString()))
}
>
<Select
style={{ minWidth: 200 }}
mode="multiple"
placeholder="Select Sensor Template"
{...sensorTemplateSelectProps}
/>
</FilterDropdown>
)}
/>
The mapValue function in FilterDropdown is specifically designed to transform the selected keys based on certain events, which can help with type conversion issues. 3. If you're using useParsed(), there might be stability issues with it as mentioned in a Discord support thread, which could affect the consistency of your filters. If none of these solutions work, you might need to implement a custom solution similar to what was suggested in Github Issue #6001, where you manually parse the URL parameters and convert them to the appropriate types. Let me know if any of these approaches help with your specific issue! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@347285614531444756> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?