ReactJS如何在分页中一次显示5个页码

时间:2018-10-12 07:58:50

标签: arrays reactjs

所以我有此分页代码,

class Pagination extends Component{

    render() {


        var { meta } = this.props.items2
        var numbers = !meta ? [] : Array.from({length: meta.pagination.total_pages },(v,k)=>k+1)
        console.log("data pagination", meta)
        return (
            <nav aria-label="Page navigation example">
              { !meta ? <span></span>
                : <ul className="pagination justify-content-center">
                            {meta.pagination.links.length === 0 ? <span></span> 
                                      : <li className="page-item">
                                          <button className="page-link" aria-label="Previous">
                                            <span aria-hidden="true">&laquo;</span>
                                            <span className="sr-only">Previous</span>
                                          </button>
                                        </li>
                              }
                            {
                                numbers.map((item) => <li className={meta.pagination.current_page === item ? "page-item active" : "page-item"}><button className="page-link" onClick={() => this.props.pageNumber(item,"num")}>{item}</button></li>) 
                            }
                            {meta.pagination.links.length === 0 ? <span></span> 
                                      : <li className="page-item">
                                          <button className="page-link" aria-label="Next">
                                            <span aria-hidden="true">&raquo;</span>
                                            <span className="sr-only">Next</span>
                                          </button>
                                        </li>
                            }
                    </ul>
              }
            </nav>
            )

    }
}

其中显示api中指示的所有页面。 看起来像这样: enter image description here

下一个和上一个按钮尚不起作用。并显示所有页面。我该如何确保仅显示5页,并且当单击“ nexy”按钮时,接下来的5页将出现?

编辑:根据@santosh答案,我收到一个错误Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

class Pagination extends Component{

    state = {
        startIndex : 0, 
        endIndex : 4
    }

  prevPage = () => {
        this.setState(prevState => ({
            startIndex : prevState.startIndex - 5,
            endIndex : prevState.endIndex - 5
        }));
    }

    nextPage = () => {
        this.setState(prevState => ({
            startIndex : prevState.startIndex + 5,
            endIndex : prevState.endIndex + 5
        }));
    }

    render() {
        var startIndex = this.state.startIndex
        var endIndex = this.state.endIndex
        var { meta } = this.props.items2
        var numbers = !meta ? [] : Array.from({length: meta.pagination.total_pages },(v,k)=>k+1)
        console.log("data pagination", meta)
        return (
            <nav aria-label="Page navigation example">
              { !meta ? <span></span>
                : <ul className="pagination justify-content-center">
                            {meta.pagination.links.length === 0 ? <span></span> 
                                      : <li className="page-item">
                                          <button className="page-link" onClick={
                                                        this.prevPage()
                                                    } aria-label="Previous">
                                            <span aria-hidden="true">&laquo;</span>
                                            <span className="sr-only">Previous</span>
                                          </button>
                                        </li>

                              }
                            {numbers.slice(startIndex, endIndex).map(item => (
                                <li className={meta.pagination.current_page === item ? 'page-item active' : 'page-item'}>
                                <button className="page-link" onClick={() => this.props.pageNumber(item, 'num')}>
                                {item}
                                </button>
                                </li>
                                ))
                                    }
                            {meta.pagination.links.length === 0 ? <span></span> 
                                      : <li className="page-item">
                                          <button className="page-link" onClick={
                                                        this.nextPage()
                                                    } aria-label="Next">
                                            <span aria-hidden="true">&raquo;</span>
                                            <span className="sr-only">Next</span>
                                          </button>
                                        </li>
                            }
                    </ul>
              }
            </nav>
            )

    }
}

这是元

enter image description here

我正在从索引文件访问此分页组件。被这样称呼

<Pagination 
pageNumber={this.pageNumber}
items2={items2}/>

1 个答案:

答案 0 :(得分:1)

可以通过以下方法完成。

1)如下定义两个状态变量。

state = {..., startIndex : 0, endIndex : 4}

2)然后用这两个索引对 numbers 数组进行切片。

{
    numbers.slice(startIndex, endIndex).map(item => (
        <li className={meta.pagination.current_page === item ? 'page-item active' : 'page-item'}>
            <button className="page-link" onClick={() => this.props.pageNumber(item, 'num')}>
                {item}
            </button>
        </li>
    ));
}

3)单击下一步按钮时,应使用 setState

更新状态值

上次点击

this.setState((prevstate)=>{
    startIndex : prevstate.startIndex - 5,
    endIndex : prevstate.endIndex - 5
})

下一步单击

this.setState((prevstate)=>{
    startIndex : prevstate.startIndex + 5,
    endIndex : prevstate.endIndex + 5
})

可以根据要求添加检查数组限制的附加条件。 希望有帮助:)

代码与onClick绑定有关。

onClick={this.prevPage()}
onClick={this.nextPage()}

应该是

onClick={this.prevPage}
onClick={this.nextPage}

请找到有效的代码笔:https://codepen.io/imsontosh/pen/zmdGdj?editors=0010