Help When filtering Data from the server with React

0

Does someone tell me how I can add a condition to the data I call from my database? I have a license plate where I call everything, and what I want is just to show in a ReactTable those that have today's date .. Here the code

class Matricula extends Component {
    state = {
        datos:[],
        today: new Date()

    }

    componentDidMount = () => {
    this.fetchData()
    }
    fetchData = async () => {
        try {
          const response = await getAll('matricula') //llamo todos los registros
          console.log("ver: ", response.data); //en la consola puedo ver todas las matriculas Id, Fecha, nombre
          if (response.data.fecha.toLocaleString() === this.state.today.toLocaleDateString()) { // no se que me falta
          this.setState({
            status: "done",
            datos: response.data,
          });

        }
        } catch (error) {
          this.setState({
            status: "error"
          });
        }
      };



    render() {

        const data = this.state.matriculas;
        return (
                <ReactTable 
                    data={data} 
                    contentEditable
                    filterable
                    collapseOnDataChange={false}

          columns={[
                {
                  Header: "Id",
                  accessor: "id"
                },
                {
                  Header: "Name",
                  accessor: "Name"
                },
                {
                  Header: "Date",
                  accessor: "date",
                  id: "date",
                }

              ]
            }

          defaultPageSize={14}
          className="-striped -highlight"
        />
)}
export default Matricula;
    
asked by Json 26.10.2018 в 05:05
source

2 answers

0

That's something you could pre-filter on the server. On the contrary, if you are driving dates in JS I would recommend using the library moment.js.

You could do something like this:

import moment from 'moment';

const iscurrentDate = moment(response.data.fecha).isSame(new Date());
if (iscurrentDate) {
  // Lanza tu condición
}
    
answered by 26.10.2018 в 17:01
0

Actually, it is convenient to do it from the front, because you have more freedoms in that way. It would use moments to parser the current date and the dates of the registrations. Then I would generate a filter method on the license plate array by a variable and return only those with today's date.

    
answered by 24.11.2018 в 20:35