React分页

原文地址

分页组件:

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';

import './style.css';
import nextBtn from './images/nextBtn.png';
import preBtnDisabled from './images/preBtn-disabled.png';

export default class Pagination extends Component {
    constructor(props) {
        super(props);
        this.state = {
            currentPageNum: 1,
        }
    }

    componentDidMount() {
        this.handleAnchor() //页面刷新时回到刷新前的page
    }

    handleAnchor() {
        let index = window.location.hash.slice(1) || 1;
        this.goToPage(parseInt(index, 10));
    }

    //翻页
    goToPage(num) {
        this.setState({currentPageNum: num});
        this.props.goToPage(num);
    }

    //页码输入框
    changePageNumber(event) {
        this.setState({
            currentPageNum: event.target.value
        });
    }

    //键盘按下事件
    handleKeyUp(e, pageTotal) {
        let value = e.target.value;
        let event = e || window.event;
        let code = event.keyCode || event.which || event.charCode;
        if (code === 13) { //回车键
            if (isNaN(value)) { //isNaN() 不是一个数字返回true
                alert('请输入数字!')
            } else if(value > pageTotal || value === null || value.trim() === '') { //使用trim()去掉空格,全为空格的字符串不为''也不为null
                alert('请输入合法的页码')
            } else {
                window.location.hash = `#${value}`;
                this.goToPage(parseInt(value, 10)) //这里一定要将value类型转换为数字,用户手动输入页码很有可能是string。而最好在这里转换是因为已经过滤了不是数字和空的值,不会出现NaN的情况
            }
        }
    }


    render() {
        let pageTotal = Math.ceil(this.props.total / this.props.pageSize); //页码总数
        return (
            
{this.state.currentPageNum === 1 ? : this.goToPage(this.state.currentPageNum-1)} > }

this.handleKeyUp(event, pageTotal)} /> / {pageTotal}

{this.state.currentPageNum === pageTotal ? : this.goToPage(this.state.currentPageNum+1)} > }
) } } Pagination.defaultProps = { pageSize: 5, currentPageNum: 1, }; Pagination.propTypes = { total: PropTypes.number.isRequired, //数据总数 pageSize: PropTypes.number, //一页显示几条数据 currentPageNum: PropTypes.number, //当前页码 };

调用方法:

import React, { Component } from 'react';
import Pagination from '../components/pagination';

class Test extends Component {
    constructor(props) {
        super(props);
        this.state = {
            total: 0, //数据总数
            data: [],
        };
        this.pageSize = 10; //一页显示的数据条数
    }

    //获取数据
    requestData() {
        fetch('http://localhost:3100/test.json').then(response => response.json())
            .then(res => {
              this.setState({
                total: res.total,
                data: res.data
              })
            })
            .catch(err => console.log('err', err))
    }

    //翻页
    goToPage(pageNum) {
        this.requestData();
    }

    render() {
        let blogItem = this.state.data && this.state.data.map((item, index) => {
            return (
                

{item.categoryName}

{item.title}

) }); return (
{blogItem} this.goToPage(pageNum)} />
) } } export default Test;

你可能感兴趣的:(React分页)