【问题标题】:Reactjs Pagination - unable to paginate the dataReactjs Pagination - 无法对数据进行分页
【发布时间】:2018-04-06 01:16:19
【问题描述】:

请找到我为分页尝试过的以下代码。我有 50 条记录,但我无法显示超过 10 条记录。而且我不知道迭代到下 10 条记录直到 50 条记录。

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>
);
}

【问题讨论】:

    标签: javascript reactjs pagination


    【解决方案1】:

    必须在handlePageChange(page)中传递页码,并且必须为当前页面设置页码(this.setState({ currentPage: page });)。

    请找到工作代码:

    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>
       );
      }
    }
    

    【讨论】:

      【解决方案2】:

      这里是:

      首先:Pagination 只是一个插件,用来做分页工作,它不会自动改变你的数据,你需要通过onChange={this.handlePageChange} 捕捉页面变化事件

      在此之后,您必须获取该值并将其设置为状态,以便您可以重新渲染视图并获取新的当前页面,例如 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>
          );
      }
      

      【讨论】:

      • 它不工作。分页导航栏中仅显示一个按钮。我有 50 条记录。所以它应该显示 5 个按钮(每个 Button 10 条记录)。
      • 请您为此创建一个 jsfiddle,代码中存在丢失的问题。
      • 我已经为它创建了一个 jsfiddle....jsfiddle.net/Karthikeyanas16/70kjtn3r/1
      • 请在下方找到更新后的代码链接。 :jsfiddle.net/Karthikeyanas16/70kjtn3r/4 它是一个没有分页的工作代码。但分页后,它只呈现前 10 条记录,并且只显示一个 Bootstrap Nav Bar 按钮。
      • 它应该在右侧的白框中显示您的代码输出,我认为这是您第一次使用 jsfiddle
      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      相关资源
      最近更新 更多