how to implement Pagination in reactJs

后端 未结 10 1412
抹茶落季
抹茶落季 2020-11-28 02:05

I am new to ReactJS and am creating a simple TODO application in it. Actually, it is a very basic app with no db connection, tasks are stored in an array. I added Edit and D

相关标签:
10条回答
  • 2020-11-28 03:08

    I have tried to recreate the simple pagination example given by piotr-berebecki which was great. But when there will be a lot of pages then the pagination will overflow in the screen. So, I used previous and back button along with forward and backward button to stream back and forth between the pages. And for design part I have used bootstrap 3.

    You can customize no of pages to display in pagination using the pagebound values. Make sure to use same value for upperPageBound and pageBound.

        class TodoApp extends React.Component {
              constructor() {
                super();
                this.state = {
                  todos: ['a','b','c','d','e','f','g','h','i','j','k','l','m',
    'n','o','p','q','r','s','t','u','v','w','x','y','z'],
                  currentPage: 1,
                  todosPerPage: 3,
                  upperPageBound: 3,
                  lowerPageBound: 0,
                  isPrevBtnActive: 'disabled',
                  isNextBtnActive: '',
                  pageBound: 3
                };
                this.handleClick = this.handleClick.bind(this);
                this.btnDecrementClick = this.btnDecrementClick.bind(this);
                this.btnIncrementClick = this.btnIncrementClick.bind(this);
                this.btnNextClick = this.btnNextClick.bind(this);
                this.btnPrevClick = this.btnPrevClick.bind(this);
                // this.componentDidMount = this.componentDidMount.bind(this);
                this.setPrevAndNextBtnClass = this.setPrevAndNextBtnClass.bind(this);
              }
              componentDidUpdate() {
                    $("ul li.active").removeClass('active');
                    $('ul li#'+this.state.currentPage).addClass('active');
              }
              handleClick(event) {
                let listid = Number(event.target.id);
                this.setState({
                  currentPage: listid
                });
                $("ul li.active").removeClass('active');
                $('ul li#'+listid).addClass('active');
                this.setPrevAndNextBtnClass(listid);
              }
              setPrevAndNextBtnClass(listid) {
                let totalPage = Math.ceil(this.state.todos.length / this.state.todosPerPage);
                this.setState({isNextBtnActive: 'disabled'});
                this.setState({isPrevBtnActive: 'disabled'});
                if(totalPage === listid && totalPage > 1){
                    this.setState({isPrevBtnActive: ''});
                }
                else if(listid === 1 && totalPage > 1){
                    this.setState({isNextBtnActive: ''});
                }
                else if(totalPage > 1){
                    this.setState({isNextBtnActive: ''});
                    this.setState({isPrevBtnActive: ''});
                }
            }
              btnIncrementClick() {
                  this.setState({upperPageBound: this.state.upperPageBound + this.state.pageBound});
                  this.setState({lowerPageBound: this.state.lowerPageBound + this.state.pageBound});
                  let listid = this.state.upperPageBound + 1;
                  this.setState({ currentPage: listid});
                  this.setPrevAndNextBtnClass(listid);
            }
              btnDecrementClick() {
                this.setState({upperPageBound: this.state.upperPageBound - this.state.pageBound});
                this.setState({lowerPageBound: this.state.lowerPageBound - this.state.pageBound});
                let listid = this.state.upperPageBound - this.state.pageBound;
                this.setState({ currentPage: listid});
                this.setPrevAndNextBtnClass(listid);
            }
            btnPrevClick() {
                if((this.state.currentPage -1)%this.state.pageBound === 0 ){
                    this.setState({upperPageBound: this.state.upperPageBound - this.state.pageBound});
                    this.setState({lowerPageBound: this.state.lowerPageBound - this.state.pageBound});
                }
                let listid = this.state.currentPage - 1;
                this.setState({ currentPage : listid});
                this.setPrevAndNextBtnClass(listid);
            }
            btnNextClick() {
                if((this.state.currentPage +1) > this.state.upperPageBound ){
                    this.setState({upperPageBound: this.state.upperPageBound + this.state.pageBound});
                    this.setState({lowerPageBound: this.state.lowerPageBound + this.state.pageBound});
                }
                let listid = this.state.currentPage + 1;
                this.setState({ currentPage : listid});
                this.setPrevAndNextBtnClass(listid);
            }
              render() {
                const { todos, currentPage, todosPerPage,upperPageBound,lowerPageBound,isPrevBtnActive,isNextBtnActive } = this.state;
                // Logic for displaying current todos
                const indexOfLastTodo = currentPage * todosPerPage;
                const indexOfFirstTodo = indexOfLastTodo - todosPerPage;
                const currentTodos = todos.slice(indexOfFirstTodo, indexOfLastTodo);
    
                const renderTodos = currentTodos.map((todo, index) => {
                  return <li key={index}>{todo}</li>;
                });
    
                // Logic for displaying page numbers
                const pageNumbers = [];
                for (let i = 1; i <= Math.ceil(todos.length / todosPerPage); i++) {
                  pageNumbers.push(i);
                }
    
                const renderPageNumbers = pageNumbers.map(number => {
                    if(number === 1 && currentPage === 1){
                        return(
                            <li key={number} className='active' id={number}><a href='#' id={number} onClick={this.handleClick}>{number}</a></li>
                        )
                    }
                    else if((number < upperPageBound + 1) && number > lowerPageBound){
                        return(
                            <li key={number} id={number}><a href='#' id={number} onClick={this.handleClick}>{number}</a></li>
                        )
                    }
                });
                let pageIncrementBtn = null;
                if(pageNumbers.length > upperPageBound){
                    pageIncrementBtn = <li className=''><a href='#' onClick={this.btnIncrementClick}> &hellip; </a></li>
                }
                let pageDecrementBtn = null;
                if(lowerPageBound >= 1){
                    pageDecrementBtn = <li className=''><a href='#' onClick={this.btnDecrementClick}> &hellip; </a></li>
                }
                let renderPrevBtn = null;
                if(isPrevBtnActive === 'disabled') {
                    renderPrevBtn = <li className={isPrevBtnActive}><span id="btnPrev"> Prev </span></li>
                }
                else{
                    renderPrevBtn = <li className={isPrevBtnActive}><a href='#' id="btnPrev" onClick={this.btnPrevClick}> Prev </a></li>
                }
                let renderNextBtn = null;
                if(isNextBtnActive === 'disabled') {
                    renderNextBtn = <li className={isNextBtnActive}><span id="btnNext"> Next </span></li>
                }
                else{
                    renderNextBtn = <li className={isNextBtnActive}><a href='#' id="btnNext" onClick={this.btnNextClick}> Next </a></li>
                }
                return (
                  <div>
                      <ul>
                      {renderTodos}
                    </ul>
                    <ul id="page-numbers" className="pagination">
                      {renderPrevBtn}
                      {pageDecrementBtn}
                      {renderPageNumbers}
                      {pageIncrementBtn}
                      {renderNextBtn}
                    </ul>
                  </div>
                );
              }
            }
    
    
            ReactDOM.render(
              <TodoApp />,
              document.getElementById('app')
            );
    

    Working demo link : https://codepen.io/mhmanandhar/pen/oEWBqx

    Image : simple react pagination

    0 讨论(0)
  • 2020-11-28 03:08

    A ReactJS dumb component to render pagination. You can also use this Library:

    https://www.npmjs.com/package/react-js-pagination
    

    Some Examples are Here:

    http://vayser.github.io/react-js-pagination/

    OR

    You can also Use this https://codepen.io/PiotrBerebecki/pen/pEYPbY

    0 讨论(0)
  • 2020-11-28 03:08

    I have implemented pagination + search in ReactJs, see the output: Pagination in React

    View complete code on GitHub: https://github.com/navanathjadhav/generic-pagination

    Also visit this article for step by step implementation of pagination: https://everblogs.com/react/3-simple-steps-to-add-pagination-in-react/

    0 讨论(0)
  • 2020-11-28 03:09

    Give you a pagination component, which is maybe a little difficult to understand for newbie to react:

    https://www.npmjs.com/package/pagination

    0 讨论(0)
提交回复
热议问题