how to implement Pagination in reactJs

后端 未结 10 1535
抹茶落季
抹茶落季 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条回答
  •  萌比男神i
    2020-11-28 03:02

       Sample pagination react js working code 
        import React, { Component } from 'react';
        import {
        Pagination,
        PaginationItem,
        PaginationLink
        } from "reactstrap";
    
    
        let prev  = 0;
        let next  = 0;
        let last  = 0;
        let first = 0;
        export default class SamplePagination extends 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','v','u','w','x','y','z'],
               currentPage: 1,
               todosPerPage: 3,
    
             };
             this.handleClick = this.handleClick.bind(this);
             this.handleLastClick = this.handleLastClick.bind(this);
             this.handleFirstClick = this.handleFirstClick.bind(this);
           }
    
           handleClick(event) {
             event.preventDefault();
             this.setState({
               currentPage: Number(event.target.id)
             });
           }
    
           handleLastClick(event) {
             event.preventDefault();
             this.setState({
               currentPage:last
             });
           }
           handleFirstClick(event) {
             event.preventDefault();
             this.setState({
               currentPage:1
             });
           }
           render() {
             let { todos, currentPage, todosPerPage } = this.state;
    
             // Logic for displaying current todos
             let indexOfLastTodo = currentPage * todosPerPage;
             let indexOfFirstTodo = indexOfLastTodo - todosPerPage;
             let currentTodos = todos.slice(indexOfFirstTodo, indexOfLastTodo);
              prev  = currentPage > 0 ? (currentPage -1) :0;
              last = Math.ceil(todos.length/todosPerPage);
              next  = (last === currentPage) ?currentPage: currentPage +1;
    
             // Logic for displaying page numbers
             let pageNumbers = [];
             for (let i = 1; i <=last; i++) {
               pageNumbers.push(i);
             }
    
              return (
               
      { currentTodos.map((todo,index) =>{ return
    • {todo}
    • ; }) }
    ); } } ReactDOM.render( , document.getElementById('root') );

提交回复
热议问题