Cannot make all filters work for a column of dates #363
-
|
I was trying to make all filters work in a date column. I have tried two ways so far but neither produced satisfying results.
Is there a way that I can make all filters work on a date column? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
MRT handles making two instances of your Filter component when it's a range, so the first example just needs to be adjusted slightly to work with ranges. To distinguish between the two generated components, a rangeFilterIndex is passed to the Filter-instance. So you'd need to do something like this in the range case Filter: ({ column, rangeFilterIndex }) => (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
onChange={(newValue) => {
column.setFilterValue((oldValues) => {
// Update the correct index of the filter value
const newValues = oldValues ?? ["", ""];
newValues[rangeFilterIndex] = newValue;
return newValues;
});
}}
renderInput={(params) => (
<TextField
{...params}
helperText={"Filter mode: " + column.getFilterFn().name}
sx={{ minWidth: "120px" }}
variant="standard"
/>
)}
value={column.getFilterValue()?.[rangeFilterIndex] || null} // Index into the filter value
/>
</LocalizationProvider>
);If the filter variant is not a range, rangeFilterIndex is undefined, so that case should be handled as well. |
Beta Was this translation helpful? Give feedback.
-
|
Here is a complete answer. |
Beta Was this translation helpful? Give feedback.
MRT handles making two instances of your Filter component when it's a range, so the first example just needs to be adjusted slightly to work with ranges.
To distinguish between the two generated components, a rangeFilterIndex is passed to the Filter-instance. So you'd need to do something like this in the range case