reactjs 从Django API获取React中两个日期之间的数据

06odsfpq  于 2023-01-01  发布在  React
关注(0)|答案(1)|浏览(119)

我正在尝试过滤两个日期之间的数据并将其显示在表中。目前,我的表显示了数据库中的所有数据,但我希望用户能够选择从该日期到该日期,以及指定日期之间的数据显示在表中。我的代码:

import React from "react";
import APIHandler from "../utils/APIHandler";
import _ from "lodash";
import moment from "moment";
import DatePicker from "react-datepicker";
import { initiateGetTransactions } from "../components/Transactions";
import ReactHTMLTableToExcel from "react-html-table-to-excel";

class FarmerStatementComponent extends React.Component {

    constructor(props) {
        super(props);
        console.log(props.match.params.id);
    }

    state = {
        errorRes: false,
        errorMessage: "",
        btnMessage: 0,
        sendData: false,
        orders: [],
        startDate: new Date(),
        endDate: new Date(),
        dataLoaded: false,
        formSubmitted: false,
        name: "",
        id: "",
    };  

    componentDidMount() {
        this.fetchFarmerData().then(response => {
            console.log(response.data);
            this.setState({
                orders: response.data
            });
        });
    }

    handleStartDateChange = (date) => {
        this.setState({
            startDate: date
        });
    }

    handleEndDateChange = (date) => {
        this.setState({
            endDate: date
        });
    }

    handleSubmit = (event) => {
        debugger;

        event.preventDefault();

        const data = { startDate:this.state.startDate, endDate:this.state.endDate};

        
    }

    async fetchFarmerData() {
        var apihandler = new APIHandler();
        var farmerdata = await apihandler.fetchFarmerDetails(this.props.match.params.id);
        console.log(farmerdata);
        this.setState({ orders: farmerdata.data.data.orders });
        this.setState({ name: farmerdata.data.data.name });
        this.setState({ phone: farmerdata.data.data.phone });
        this.setState({ dataLoaded: true });
    }

    print() {
        window.print();
    }

    render() {
       
        return (
            <section className="content">
                 <div className="container-fluid">
                    <div className="block-header">
                       <h2>FARMER STATEMENT</h2>
                    </div>

                    <div className="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        <div className="card">
                            <div className="header">
                                <h2>
                                    {this.state.name}'S STATEMENT
                                </h2>

                                <div className="header-dropdown m-r--5">
                                    <h2>
                                        {this.state.id} 
                                    </h2>
                                </div>
                                
                            </div>

                            <div className="body">
                                <div className="table-responsive">
                                    <div id="DataTables_Table_1_wrapper" className="dataTables_wrapper form-inline dt-bootstrap">
                                        <div className="dt-buttons">
                                            <a className="dt-button buttons-csv buttons-html5" tabIndex="0" aria-controls="DataTables_Table_1" href="#">
                                                <span>
                                                <ReactHTMLTableToExcel 
                                                    className="btn btn-success"
                                                    tabIndex="0" 
                                                    aria-controls="DataTables_Table_1"
                                                    table="DataTables_Table_1"
                                                    filename="FarmerStatement"
                                                    sheet="Sheet"
                                                    buttonText="Excel"
                                                />
                                                </span>
                                            </a> 
                                            <a className="dt-button buttons-print" tabIndex="0" aria-controls="DataTables_Table_1" href="#" onClick={this.print}>
                                                <span onClick={() => this.print()}>Print</span>
                                            </a>
                                            <br/>
                                            <form onSubmit={this.handleSubmit}>
                                                <button type="submit" className="btn btn-info" >
                                                    Generate Report
                                                </button>
                                            </form>
                                        </div>
                                        
                                        <div id="DataTables_Table_1_filter" className="dataTables_filter">
                                            <form onSubmit={this.handleSubmit}>
                                                <label>
                                                    <i className="material-icons">date_range</i>Start Date:
                                                        <DatePicker
                                                            selected={this.state.startDate}
                                                            className="form-control datepicker" 
                                                            name="Start Date: 30/07/2022"
                                                            aria-controls="DataTables_Table_1"
                                                            value={this.state.startDate}
                                                            onChange={this.handleStartDateChange}
                                                        />
                                                </label>
                                                <br/>
                                                <label>
                                                    <i className="material-icons">date_range</i>End Date:
                                                        <DatePicker
                                                            selected={this.state.endDate}
                                                            className="form-control datepicker" 
                                                            name="End Date: 30/07/2022"
                                                            aria-controls="DataTables_Table_1"
                                                            value={this.state.endDate}
                                                            onChange={this.handleEndDateChange}
                                                        />
                                                </label>
                                            </form>    
                                        </div>
                                        {this.state.dataLoaded === false ? (
                                            <div className="text-center">
                                                <div className="preloader pl-size-xl">
                                                    <div className="spinner-layer">
                                                        <div className="circle-clipper left">
                                                            <div className="circle"></div>
                                                        </div>
                                                        <div className="circle-clipper right">
                                                            <div className="circle"></div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        ) : ""}

                                        <table className="table table-bordered table-striped table-hover dataTable js-exportable" id="DataTables_Table_1" role="grid" aria-describedby="DataTables_Table_1_info">
                                            <thead>
                                                <tr role="row">
                                                    <th className="sorting_asc" tabIndex="0" aria-controls="DataTables_Table_1" rowSpan="1" colSpan="1" aria-sort="ascending" aria-label="Name: activate to sort column descending" style={{width: "40px"}}>
                                                        #ORDER NO
                                                    </th>
                                                    <th className="sorting" tabIndex="0" aria-controls="DataTables_Table_1" rowSpan="1" colSpan="1" aria-label="Position: activate to sort column ascending" style={{width: "150px"}}>
                                                        CUSTOMER NAME
                                                    </th>
                                                    <th className="sorting" tabIndex="0" aria-controls="DataTables_Table_1" rowSpan="1" colSpan="1" aria-label="Office: activate to sort column ascending" style={{width: "101px"}}>
                                                        KILOS
                                                    </th>
                                                    <th className="sorting" tabIndex="0" aria-controls="DataTables_Table_1" rowSpan="1" colSpan="1" aria-label="Age: activate to sort column ascending" style={{width: "100px"}}>
                                                        PRICE PER KG
                                                    </th>
                                                    <th className="sorting" tabIndex="0" aria-controls="DataTables_Table_1" rowSpan="1" colSpan="1" aria-label="Start date: activate to sort column ascending" style={{width: "115px"}}>
                                                        ORDERED ON
                                                    </th>
                                                </tr>
                                            </thead>
                                            
                                            <tbody>   
                                            {this.state.orders.map((farmer) => (
                                                <tr role="row" className="odd" key={farmer.id}>
                                                    <td className="sorting_1">{farmer.id}</td>
                                                    <td className="sorting_1">{farmer.customer.name}</td>
                                                    <td className="sorting_1">{farmer.kgs}</td>
                                                    <td className="sorting_1">{farmer.price}</td>
                                                    <td className="sorting_1">{new Date(farmer.added_on).toLocaleString()}</td>
                                                </tr>
                                            ))}
                                            </tbody>
                                        </table>
                                        <div className="dataTables_info" id="DataTables_Table_1_info" role="status" aria-live="polite">
                                            
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                 </div>
            </section>    
             
        );
    }
    
}

export default FarmerStatementComponent;
wvt8vs2t

wvt8vs2t1#

您可以在查询中添加筛选器,也可以在传递到表之前对来自数据库的传入数据进行筛选

相关问题