how to implement Pagination in reactJs

后端 未结 10 1558
抹茶落季
抹茶落季 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 
  • {todo}
  • ; }); // 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(
  • {number}
  • ) } else if((number < upperPageBound + 1) && number > lowerPageBound){ return(
  • {number}
  • ) } }); let pageIncrementBtn = null; if(pageNumbers.length > upperPageBound){ pageIncrementBtn =
  • } let pageDecrementBtn = null; if(lowerPageBound >= 1){ pageDecrementBtn =
  • } let renderPrevBtn = null; if(isPrevBtnActive === 'disabled') { renderPrevBtn =
  • Prev
  • } else{ renderPrevBtn =
  • Prev
  • } let renderNextBtn = null; if(isNextBtnActive === 'disabled') { renderNextBtn =
  • Next
  • } else{ renderNextBtn =
  • Next
  • } return (
      {renderTodos}
      {renderPrevBtn} {pageDecrementBtn} {renderPageNumbers} {pageIncrementBtn} {renderNextBtn}
    ); } } ReactDOM.render( , document.getElementById('app') );

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

    Image : simple react pagination

提交回复
热议问题