请帮忙...我有一个fetchData函数,可以从DB Table Matricula中获取数据,我只需要捕获日期= Today的记录
如何仅接收日期与当天相同的数据?
class Matricula extends Component {
state = {
datos:[],
today: new Date()
}
componentDidMount = () => {
this.fetchData()
}
fetchData = async () => {
try {
const response = await getAll('matricula')
console.log("ver: ", response.data);
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;
getAll函数是 导出函数getAll(entity){ 返回axios({ 方法:“获取”, baseURL:API_URL, 标头:headers(), url:实体, }) }
答案 0 :(得分:0)
最佳方法是询问所需的数据,这意味着仅询问当今的矩阵。
如果您不能更改此设置,则应该在将它们存储在状态之前对其进行过滤,如下所示:
this.setState({
status: "done",
datos: response.data.filter((matricula)=>{
return matricula.date === this.state.today //not a proper dates comparison
}),
});
这里,我假设您的矩阵具有属性date
,并且将其与您的this.state.today
进行比较以将其过滤掉。请记住,您应该进行正确的日期比较,这取决于您存储数据的格式this should help