-
New to react and react-table. If anyone could provide a Date Range filter example with two inputs startDate and endDate similar to NumberRangeColumnFilter, would really help me understand this. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 8 replies
-
Was able to get a rough version working. Filter Code :
Define Custom filter type like this:
Filter Function Def:
Finally call it inside main Table function alongside other filterTypes if you have any:
This is pretty rough and basic, if someone could rectify and suggest something stronger, I would really appreciate it. Thanks 🤓👍🏼 |
Beta Was this translation helpful? Give feedback.
-
Hello, here is a more robust snippet, and handle :
function dateBetweenFilterFn(rows, id, filterValues) {
const sd = filterValues[0] ? new Date(filterValues[0]) : undefined
const ed = filterValues[1] ? new Date(filterValues[1]) : undefined
if (ed || sd) {
return rows.filter(r => {
const cellDate = new Date(r.values[id])
if (ed && sd) {
return cellDate >= sd && cellDate <= ed
} else if (sd){
return cellDate >= sd
} else if (ed){
return cellDate <= ed
}
})
} else {
return rows
}
} function DateRangeColumnFilter({
column: {
filterValue = [],
preFilteredRows,
setFilter,
id
}})
{
const [min, max] = React.useMemo(() => {
let min = preFilteredRows.length ? new Date(preFilteredRows[0].values[id]) : new Date(0)
let max = preFilteredRows.length ? new Date(preFilteredRows[0].values[id]) : new Date(0)
preFilteredRows.forEach(row => {
const rowDate = new Date(row.values[id])
min = rowDate <= min ? rowDate : min
max = rowDate >= max ? rowDate : max
})
return [min, max]
}, [id, preFilteredRows])
return (
<div>
<input
min={min.toISOString().slice(0, 10)}
onChange={e => {
const val = e.target.value
setFilter((old = []) => [val ? val : undefined, old[1]])
}}
type="date"
value={filterValue[0] || ''}
/>
{' to '}
<input
max={max.toISOString().slice(0, 10)}
onChange={e => {
const val = e.target.value
setFilter((old = []) => [old[0], val ? val.concat('T23:59:59.999Z') : undefined])
}}
type="date"
value={filterValue[1]?.slice(0, 10) || ''}
/>
</div>
)
} |
Beta Was this translation helpful? Give feedback.
-
I'm sorry for disturbing this topic but I faced with the same question |
Beta Was this translation helpful? Give feedback.
-
Pls how do I make this wwork in my code? I'm trying to implement date-filter in react-table |
Beta Was this translation helpful? Give feedback.
-
Hey @verbeckii and @Tosinkoa! Since I'm new I was also struggling to make it work, so when I did it I created a CodeSandox to help. Credits to TheWidlarzGroup since I'm using their template which I find easier to work on (https://github.com/TheWidlarzGroup). I will be using @dolie implementation. Notice that in filters.js I've commented "min" and "max" in order to being able to select any date we want to. You can try to uncomment to see what happens. https://codesandbox.io/s/smoosh-resonance-6zs39h?file=/src/App.js |
Beta Was this translation helpful? Give feedback.
-
Hope this helps someone |
Beta Was this translation helpful? Give feedback.
Was able to get a rough version working.
Filter Code :