【问题标题】:Bootstrap's Pagination in reactjs - Data coming from MongoDBBootstrap 在 reactjs 中的分页 - 来自 MongoDB 的数据
【发布时间】:2017-10-23 05:09:30
【问题描述】:

我正在尝试在 reactjs 应用程序中进行分页。请在下面找到代码。 最后无法呈现列表。它给出了空白列表。有人可以帮我找出这里的错误吗? 请找到完整的代码。

    return (
  <table className="table table-hover" >
    <thead >
      <tr>
        <th>Order Number</th>
        <th>Customer Name</th>
        <th>Created By</th>
      </tr>
    </thead>
    <SearchResultsList items={this.props.results} open={this.props.open} />
  </table>
);
    }
    }
class SearchResultsList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  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++;
                <SearchResultsItem key={item.id} item={item} open={this.props.open} />
              }
            })
        }
      </tbody>
    );
    <Pagination
      ClassName="items-pagination pull-right"
      bsSize="medium"
      maxButton={10}
      first last next prev boundaryLinks
      item={pages}
      activePage={current_page} />
  }
    class SearchResultsItem extends React.Component {
  constructor(props) {
    super(props);
    this.handleInputChange = this.handleInputChange.bind(this);
    this.state = {};
  }
  handleInputChange(e) {
    const target = e.target;
    const name = target.name;
    const value = target.type === 'checkbox' ? target.checked : target.value;
    this.setState({ [name]: value });
  }
  render() {
    return (
      <tr style={{ fontsize: '10', cursor: 'pointer' }}
    <td>{this.props.item.order_number}</td>
      <td>{this.props.item.customer_name}</td>
      <td>{this.props.item.buyer_name}</td>
    </tr >
    );
  }
}

请在下面找到演示数据。来自 mongodb 的数据作为 JSON 对象。

{ 
"_id" : "658ecv57b-3853-46f4-af6c-cb1ca111137f", 
"order_number" : "1000123", 
"customer_name" : "rahul", 
"buyer_name" : "rahul", 
}
{ 
"_id" : "1258ecv57b-3853-46f4-af6c-cb1ca111137f", 
"order_number" : "1000123", 
"customer_name" : "Bruce", 
"buyer_name" : "Bruce", 
}
{ 
"_id" : "658ecv57b-3853-46f4-af6c-cb1ca1111312f", 
"order_number" : "1000123", 
"customer_name" : "mike", 
"buyer_name" : "mike", 
}
{ 
"_id" : "658ecv57b-3853-46f4-af6c-cb112111137f", 
"order_number" : "1000123", 
"customer_name" : "linda", 
"buyer_name" : "linda", 
}

【问题讨论】:

    标签: javascript mongodb reactjs pagination


    【解决方案1】:

    您在SearchResultsList 的map 函数中错过了return。

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

    【讨论】:

    • 但是你需要从你的.map函数中返回&lt;SearchResultsItem key={ item.id } item={ item } open={ this.props.open } /&gt;;,否则.map会返回void。
    • 您的分页导航栏不在退货声明中。
    • 我已经更新了我的代码,并在 return 语句中包含了 Pagination Nav Bar。但不知道如何移动到下一页 - 下一页。现在只能显示 50 条记录中的 10 条记录。我已经打开了另一个问题。请找到网址:stackoverflow.com/questions/46924112/…
    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 2019-08-20
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    相关资源
    最近更新 更多