1

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

enter image description here

4
  • You wanna filter it on front-end side or server(API) side? Commented Dec 6, 2021 at 2:42
  • from front-end side @wisnuaryadipa Commented Dec 6, 2021 at 3:24
  • Please read how to ask a question on stackoverflow Commented Dec 6, 2021 at 3:25
  • @silencedogood ok please give me some solution also Commented Dec 6, 2021 at 3:35

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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>
Looks like you can filter here just fine: datasData.filter( //filter logic ).map((data ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.