使用react-bootstrap表进行分页

问题描述 投票:0回答:2

我正在使用create-react-app和react-bootstrap构建一个基本的react应用程序。我正在使用表格来显示来自react-bootstrap https://react-bootstrap.github.io/components/table/的信息。我正在尝试让分页适用于表格,但我不确定如何绕过它来使用表格。以下是我的组件的代码片段,我添加了来自react-bootstrap的分页代码片段,它只在底部显示分页页脚,但实际上并不对结果进行分页。 应用程序.js

import React, { Component } from 'react';
import Table from 'react-bootstrap/lib/Table';
import Pagination from 'react-bootstrap/lib/Pagination';

export class App extends Component {

renderList() {
  return(
    <Table striped hover bordered condensed>
      <thead>
        <tr>
          <th>Name</th>
          <th>Description</th>
          <th>Forks</th>
          <th>Clone URL</th>
        </tr>
      </thead>
      <tbody>
        {this.state.response.map((object) => {
          return (
            <tr>
              <td><a onClick={this.handleClick.bind(this,object)}>{object.name} </a></td>
              <td>{object.description}</td>
              <td><Glyphicon glyph="cutlery" /> {object.forks_count}</td>
              <td>{object.clone_url}</td>
            </tr>
          );
        })}
        </tbody>
      </Table>
    );
  }

  renderPagination() {
    let items = [];
    for (let number = 1; number <= 10; number++) {
      items.push(
       <Pagination.Item>{number}</Pagination.Item>
      );
    }
    return (
      <Pagination bsSize="small">{items}</Pagination>
    );
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Netflix Repositories</h1>
        </header>
        {this.renderList()}
        <CommitList ref={(ref) => this.commitList = ref} 
          show={this.state.show}
          commits={this.state.commits} />
        {this.renderPagination()}
      </div>
    );
  }
}

如何对表格上的结果进行分页?

reactjs react-bootstrap create-react-app
2个回答
0
投票

有 2 种方法来处理这个问题:

  1. 仅使用前端分页:获取整个结果并将结果除以列表大小(例如 10),假设您总共有 50 个列表项,所以您有 50/10 == 5 页。对于每个分页项目,添加一个“onClick”并更改显示的页面。

  2. 同时使用前端和后端分页:仅获取结果的特定部分,并在前端处理它。 (搜索 Spring JPA 分页了解更多详细信息。)


0
投票

使用 Antd 设计实现响应式表格和分页 链接如下:https://ant.design/components/pagination/

© www.soinside.com 2019 - 2024. All rights reserved.