【问题标题】:Pagination with React使用 React 进行分页
【发布时间】:2019-09-12 14:18:37
【问题描述】:

我的分页做了一些奇怪的事情。我用react-pager

当我点击第一页时 - 我没有任何项目(但我注意到了),当我点击第二页时,我有 4 个项目,当我点击第三页时,我的项目刚刚添加到来自第二页。我的意思是在第三页上我有 8 个旧 + 新项目,而不是 4 个像我注意到的新项目。 我认为切片有问题。

组件如下:

import React, {Component} from 'react';
import {connect} from 'react-redux';
import Pager from 'react-pager';

class AlbumsShow extends Component {
    constructor(props) {
        super(props);
        this.renderImage = this.renderImage.bind(this);
        this.handlePageChanged = this.handlePageChanged.bind(this);

        this.state = {
            total:       3,
            current:     1,
            visiblePage: 2,
        };
    }

    handlePageChanged(newPage) {
        this.setState({ current : newPage });
    }

    renderImage() {
        return this.props.images.slice((this.state.current-1), this.state.current * 4).map(image => (
            <li key={image.id}>
                <img className="album_img" alt="job" src={image.img} />
                <p className="album_title">Test</p>
            </li>
        ));
    }

    render() {
        return (
            <div>
                <div>
                    {this.renderImage()}
                </div>
                     <Pager
                        total={this.state.total}
                        current={this.state.current}
                        visiblePages={this.state.visiblePage}
                        titles={{ first: '<|', last: '>|' }}
                        className="pagination-sm pull-right"
                        onPageChanged={this.handlePageChanged}
                    />
            </div>
        );
    }
}

function mapStateToProps(state, ownProps) {
    const currentAlbumId = parseInt(ownProps.params.id, 10);

    return {
        images: state.main.albums.filter(album => album.id === currentAlbumId)[0].images,
    };
}
export default connect(mapStateToProps)(AlbumsShow);

此外,如果我将有更多 10 个项目,我的分页应该是可见的,如果少于 10 - null,但我的条件也不起作用。例如:

            {this.renderImage() > 10 ?

             <Pager
                total={this.state.total}
                current={this.state.current}
                visiblePages={this.state.visiblePage}
                titles={{ first: '<|', last: '>|' }}
                className="pagination-sm pull-right"
                onPageChanged={this.handlePageChanged} 
            />: null }

【问题讨论】:

    标签: reactjs pagination


    【解决方案1】:

    我猜this.state.current 是页码。所以你的切片可能是这样的:

    slice(this.state.current * 4, (this.state.current + 1) * 4 - 1)

    编辑: 我没有看到你的第二个问题。如果您不希望分页超过 10 个项目,您可以这样做:

        {this.props.images.length > 10 ?
    
             <Pager
                total={this.state.total}
                current={this.state.current}
                visiblePages={this.state.visiblePage}
                titles={{ first: '<|', last: '>|' }}
                className="pagination-sm pull-right"
                onPageChanged={this.handlePageChanged} 
            />: null }
    

    编辑 2: 糟糕,电流从 0 而不是 1 开始。我编辑了上面的切片。

    编辑:3: 如果您想在少于 10 个项目时显示所有图像,您可以执行以下操作:

      renderImage() {
        const imgs = this.props.images.length > 10 ?
          this.props.images.slice(this.state.current * 4, (this.state.current + 1) * 4 - 1) :
          this.props.images;
        return imgs.map(image => (
          <li key={image.id}>
            <img className="album_img" alt="job" src={image.img} />
            <p className="album_title">Test</p>
          </li>
        ));
      }
    
      render() {
        return (
          <div>
          <ul>
            {this.renderImage()}
          </ul>
    
          {this.props.images.length > 10 ?
    
            <Pager
              total={this.state.total}
              current={this.state.current}
              visiblePages={this.state.visiblePage}
              titles={{ first: '<|', last: '>|' }}
              className="pagination-sm pull-right"
              onPageChanged={this.handlePageChanged} 
            /> : null
          }
          </div>
        );
      }
    

    【讨论】:

    • 它有帮助,我的项目不会相互添加,但我的第一页仍然是空的。
    • 分页工作,谢谢。我还有一个关于条件的问题:当我将您的编辑粘贴到我的代码中时,我的项目消失了,但控制台很清晰。
    • 你的意思是当你有少于 10 张图片时你显示 0 张图片?如果是这样,这是正常的,因为如果条件为假,则返回 null。我将编辑我的答案,向您展示如何做到这一点。
    • 看,当我的图片少于 10 张时 - 我需要让我的分页不可见并且我的所有项目都应该显示,但是当项目超过 10 时 - 我在一页上有 10 个项目并且我的分页可见.
    • 检查我的 EDIT 3,这应该可以。我刚刚重新编辑了它,所以请务必使用我的最后一个 EDIT 3。刷新此 stackoverflow 页面以显示最新版本。
    【解决方案2】:

    另一个处理分页的组件是react-pagination-custom

    <TinyPagination
        total = {...}
        selectedPageId = {...}
        itemPerPage = {...}
        renderBtnNumber = {...}
        maxBtnNumbers = {...}
        preKey = '...'
        nextKey = '...'
        {...someOptionalProperties}
    />
    

    它允许您自定义所有内容(数字按钮、包装容器、展开等)。它的文档很好,demo也很清晰。

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 2021-03-31
      • 2017-10-12
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      相关资源
      最近更新 更多