【问题标题】:ReactStrap pagination element exceeds available widthReactStrap 分页元素超出可用宽度
【发布时间】:2020-09-22 01:22:53
【问题描述】:

我在 Card 元素中呈现了一个 Pagination 元素:

这是代码:

 <Col col="6" sm="4" md="2" xl className="mb-3 mb-xl-0">
        <TablePagination
          pagesCount={this.props.pagesCount}
          currentPage={this.state.currentPage}
          handlePageClick={this.handlePageClick}
          handlePreviousClick={this.handlePreviousClick}
          handleNextClick={this.handleNextClick}
        />
      </Col>
    </Card>

这是 TablePagination React 功能组件:

import React, { Component } from "react";
import { Pagination, PaginationItem, PaginationLink } from "reactstrap";

import PropTypes from "prop-types";
/** I used object destructuring of the props object, to pass down the properties as variables. This improves readability by getting rid of props. */
const TablePagination = ({
  pagesCount,
  currentPage,
  handlePageClick,
  handlePreviousClick,
  handleNextClick,
}) => {
  return (
    /**The reactstrap Pagination component encapsulates the reactstrap PaginationItem which in turn encapsulates reactstrap PaginationLink. */
    /**The first PaginationItem inside the Pagination is the previous button. This is disabled when the current page is zero or less
     *  than zero “disabled={currentPage <= 0}”. */
    <div>
      <Pagination size="sm">
        <PaginationItem disabled={currentPage <= 0}>
          <PaginationLink onClick={handlePreviousClick} previous href="#" />
        </PaginationItem>
        {/* The next PaginationItem after the previous PaginationItem button is the dynamic PaginationItem. This is the one that generates the page number buttons. */}
        {/* “Array(pagesCount)”: creates and initializes a new array object of length equal to pagesCount. */}
        {/* “[…Array(pagesCount)].map( fn)”: using the spread operator I expand the array. After expanding, the map() method then creates a new array of PaginationItems. */}

        {[...Array(pagesCount)].map((page, i) => (
          <PaginationItem active={i === currentPage} key={i}>
            <PaginationLink onClick={(e) => handlePageClick(e, i)} href="#">
              {i + 1}
            </PaginationLink>
          </PaginationItem>
        ))}

        <PaginationItem disabled={currentPage >= pagesCount - 1}>
          <PaginationLink onClick={handleNextClick} next href="#" />
        </PaginationItem>
      </Pagination>
    </div>
  );
};

TablePagination.propTypes = {
  //pageCount: the total number of records in our dataset.
  pagesCount: PropTypes.number.isRequired,
  //currentPage: the current page navigated to
  currentPage: PropTypes.number.isRequired,
  /**handlePageClick: a function that handles the click event when a page number is clicked.
   * This function will pass the current page number which will be saved in state. */
  handlePageClick: PropTypes.func.isRequired,
  /**handlePreviousClick: a function that handles the click event when the previous button is clicked. This enables navigating to the previous(<<) page. */
  handlePreviousClick: PropTypes.func.isRequired,
  /**handleNextClick: a function that handles the click event when the next (>>) button is clicked. This enables navigating to the next page. */
  handleNextClick: PropTypes.func.isRequired,
};
export default TablePagination;

我在documentation 中进行了搜索,我找不到一种方法可以让分页编号在下一行中呈现,以防它们超出宽度限制。

【问题讨论】:

    标签: html css reactjs twitter-bootstrap reactstrap


    【解决方案1】:

    您可以为“.pagination”元素添加样式规则

    flex-wrap: wrap;
    

    但它看起来有点蹩脚的几行。 我认为最好对超出的页面使用点,例如MaterialUI pagination

    【讨论】:

    • 谢谢你。那完成了工作!
    【解决方案2】:

    我遇到了同样的问题,我想让它对用户更友好。 所以我想显示 10 个分页链接并动态更改开始和结束页面,就像在谷歌页面结果分页中一样。

    let pageLimit = 10; // number of page links in pagination
    let start = 0; // starting page
    let end = pageLimit; // ending page
    
    if (pagesCount <= pageLimit) {
      pageLimit = pagesCount;
    }
    
    // increment start page when current page is greater than 5
    if (currentPage - 5 >= 0) {
      start = currentPage - 4;
    }
    
    // if reaching end of pagination stop increment 
    if (start + pageLimit >= pagesCount) {
      start = pagesCount - pageLimit;
    }
    
    // increment end page when current + 5 exceeds page limit
    if (currentPage + 5 >= pageLimit) {
      end = currentPage + 6;
      pageLimit = end;
      if (pagesCount <= pageLimit) {
        pageLimit = pagesCount;
      }
    }
    

    这些数字可能与您的数据集不同。 然后在起点和终点之间渲染 PaginationItem

    {[...Array(pageLimit)].map((page, i) => {
      if (i >= start && i < end) {
        return (
          <PaginationItem active={i === currentPage} key={i}>
            <PaginationLink onClick={e => handleClick(e, i)} href="#">
              {i + 1}
            </PaginationLink>
          </PaginationItem>
         );
      }
    })}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      • 2021-01-22
      • 2014-10-15
      • 1970-01-01
      相关资源
      最近更新 更多