I am working with reactjs with axios and APIs and want to use date range filter in my data table. but its not working. please give me a example if anyone know or work on date range filter
-
You wanna filter it on front-end side or server(API) side?wisnuaryadipa– wisnuaryadipa2021-12-06 02:42:46 +00:00Commented Dec 6, 2021 at 2:42
-
from front-end side @wisnuaryadipagovind jha– govind jha2021-12-06 03:24:36 +00:00Commented Dec 6, 2021 at 3:24
-
Please read how to ask a question on stackoverflowsilencedogood– silencedogood2021-12-06 03:25:57 +00:00Commented Dec 6, 2021 at 3:25
-
@silencedogood ok please give me some solution alsogovind jha– govind jha2021-12-06 03:35:51 +00:00Commented Dec 6, 2021 at 3:35
Add a comment
|
1 Answer
If you are doing the filter on frontend side, you can introduce an array filter method right before your .map which draws your table rows. The following might help you:
...
const [dateFilter, setDateFilter] = useState({
startDate: null,
endDate: null
})
//make sure to change these states with your filter header (the 2 inputs with type="date")
return (
...
<tbody>
{data
.filter(row => {
let filterPass = true
const date = new Date(row.dateYouWannaFilterWith)
if (dateFilter.startDate) {
filterPass = filterPass && (new Date(dateFilter.startDate) < date)
}
if (dateFilter.endDate) {
filterPass = filterPass && (new Date(dateFilter.endDate) > date)
}
//if filterPass comes back `false` the row is filtered out
return filterPass
})
.map(row =>
<tr>
<td>Your</td>
<td>Table</td>
<td>Row</td>
<td>Cells</td>
</tr>
)
}
</tbody >
This way you're fecthing all the rows and your UI is doing the filter.
Alternatively you could refetch the data everytime, with filter dates in query params of your request, and your backend could handle the filtering server side, only returning the rows you need. This way you need to configure the backend. And your frontend keeps refetching with different query param values.
await fetch('your-api-url?startDate=2021-12-06T03:40:32.242Z&endDate=2021-12-06T03:40:53.584Z')
//these dates are for placeholders
2 Comments
govind jha
Thank for your reply. i am using my .map here <TableBody> {fetchedUserList ? ( datasData.map((data: any) => ( <TableRow className={data.actions} key={data.clientIn19Id} id={data.clientIn19Id}> <TableCell align="left">{data.Mobile}</TableCell> <TableCell align="left"> <Moment interval={30000} format="ll"> {data.LastLoginDate} </Moment> </TableCell>
erchis
Looks like you can filter here just fine: datasData.filter( //filter logic ).map((data ...
