Reactjs Pagination - unable to paginate the data

2019-03-03 07:14发布

问题:

Please find the below code that i have tried for pagination. I have 50 records but i cant able to show more than 10 records. And i dont have idea to iterate to next 10 records till 50 records.

render() {
  const per_page=10;
  const pages = Math.ceil(this.props.items.length / per_page);
  const current_page = 1;
  const start_offset = (current_page - 1) * per_page;
  let start_count =0;
  return (<tbody>{ this.props.items
.sort((a, b) => moment(b.order_date) - moment(a.order_date) || b.order_number - a.order_number)
.map((item, index) => {
    if (index >= start_offset && start_count < per_page) {
      start_count++;
      return <SearchResultsItem key={item.id} item={item} open={this.props.open} />;
    }
  })
}
<Pagination
  className="items-pagination pull-right"
  bsSize="medium"
  maxButton={10}
  first last next prev boundaryLinks
  item={pages}
  activePage={current_page} />
</tbody>
);
}

回答1:

Here it is :

First of all : Pagination is just plugin to make pagination work, it wont change your data automatically , you need to catch the page change event via onChange={this.handlePageChange}

After this, you have to get that value and set it in to state so you can rerender the view and get new current page like const current_page = this.state.current_page || 1;

handlePageChange(pageNumber) {
    console.log(`current_page is ${pageNumber}`);
    this.setState({current_page: pageNumber});
}

render() {
    const per_page=10;
    const pages = Math.ceil(this.props.items.length / per_page);
    const current_page = this.state.current_page || 1;
    const start_offset = (current_page - 1) * per_page;
    let start_count =0;

    return (
        <tbody>
            {   this.props.items
                .sort((a, b) => moment(b.order_date) - moment(a.order_date) || b.order_number - a.order_number)
                .map((item, index) => {
                    if (index >= start_offset && start_count < per_page) {
                        start_count++;
                        return <SearchResultsItem key={item.id} item={item} open={this.props.open} />;
                    }
                })
            }

            <Pagination
            className="items-pagination pull-right"
            bsSize="medium"
            maxButton={10}
            first last next prev boundaryLinks
            item={pages}
            activePage={this.state.current_page || 1}
            onChange={this.handlePageChange} />

        </tbody>
    );
}


回答2:

Have to pass the page number in the handlePageChange(page) and have to set the page number for the current page (this.setState({ currentPage: page } );).

Please find the working code :

class SearchResults extends React.Component {
 constructor(props) {
 super(props);
 this.state = {  };
}

handlePageChange(page) {
this.setState({ currentPage: page } );
}

render() {
  return (
  <div className="panel panel-primary">
    <div className="panel-heading">ORDERS LIST</div>
    <table className="table table-hover" }} >
      <thead >
        <tr>
          <th>Customer Name</th>
          <th>Assignee</th>
          <th>Status</th>
          <th>Creation Date </th>
        </tr>
      </thead>
      <SearchResultsList items={ this.props.results } open={ this.props.open 
     } 
      current_Page={ this.state.currentPage }  />
    </table>

    <Pagination id="content" className="users-pagination pull-right" 
    bsSize="medium" 
    first last  next  prev  boundaryLinks items={numPages} 
    activePage={ this.state.currentPage } onSelect={ this.handlePageChange } 
    />
   </div>
   );
  }
}