如何在Reactjs应用程序中使用分页 [英] How to use pagination in Reactjs application

查看:75
本文介绍了如何在Reactjs应用程序中使用分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

解决方案

这是react-js-pagination的默认样式.您必须自己设置样式.但是据我所知,您正在应用程序中使用引导程序,因此可以通过以下方式使用它们的样式:

 <分页activePage = {this.state.activePage}itemsCountPerPage = {1}totalItemsCount = {2}pageRangeDisplayed = {1}onChange = {this.handlePageChange.bind(this)}itemClass ='页面项目'linkClass ='页面链接'/> 

I am implementing pagination for my blog page by using react-js-pagination.

  1. I am unable to fix this pagination ui and its content per page
  2. Bind my data accordingly per page

I have tried to import less from bootstrap but it is not rendering for this.

Any suggestion on this to solve this pagination issue?

Updated code: it is working now //blog.js

import React, {Component} from 'react';
import {Card, Grid, Cell, Dialog, CardMenu, Button, CardTitle, 
    CardText, CardActions, FABButton, Icon} from'react-mdl';
import { Container} from 'reactstrap';
import { connect } from 'react-redux';
import { getBlog, deleteBlog } from '../../actions/resumeActions';
import PropTypes from 'prop-types';
import Loading from './Loading';
import Moment from 'moment';
import BlogModal from "./BlogModal";
import Pagination from "react-js-pagination";

class Blogs extends Component{
    constructor(props) {
        super(props);
            this.state = {
                modal: false,
                justClicked: null,
                activePage: 1
            };
        
            this.handleOpenDialog = this.handleOpenDialog.bind(this);
            this.handleCloseDialog = this.handleCloseDialog.bind(this);
        }
    
    static propTypes = {
        getContact: PropTypes.func.isRequired,
        deleteContact: PropTypes.func.isRequired,
        resume: PropTypes.object.isRequired,
        isAuthenticated: PropTypes.bool,
        auth: PropTypes.object.isRequired,
        loading: PropTypes.object.isRequired
    }

    toggle = () => {
        this.setState({
            modal: !this.state.modal
        });
    }

    handleOpenDialog(id) {
        this.setState({
          openDialog: true,
          justClicked: id
    
        });
    }
    
    handleCloseDialog() {
    this.setState({
        openDialog: false
    });
    }
    
    componentDidMount() {
        this.props.getBlog();
    }

    onDeleteBlogClick = (id) => {
        this.props.deleteBlog(id);
    };

    handlePageChange(pageNumber) {
        console.log(`active page is ${pageNumber}`);
        this.setState({activePage: pageNumber});
      }

  cardDialog(blogs, user){
    return(
        <Grid style={{padding: 0, display: 'contents'}}>
            
            {blogs.map(({ _id, blog_name, blog_desc, blog_image_link, blog_by }) => (
            <Cell col={12}>
                <Dialog open={this.state.openDialog && this.state.justClicked === _id} className="open-dialog">
                
                    <CardTitle style={{color: '#fff', height: '176px', backgroundImage: `url(${blog_image_link})`, backgroundPosition: 'center',
                    backgroundSize: 'cover',
                    backgroundRepeat: 'no-repeat'}}>{blog_name}</CardTitle>
                    <CardText>
                        {blog_desc}
                    </CardText>
                    <CardActions border>
                       <p style={{float:'right', fontWeight:'bold'}}>Author: {blog_by}</p>
                    </CardActions>
                    <CardMenu style={{color: '#fff'}}>
                        <FABButton onClick={this.handleCloseDialog} className="close-button" >
                            <Icon name="close" />
                        </FABButton>
                    </CardMenu>
                </Dialog>
            </Cell>
            ))}
        </Grid>
    )
    
  }



    render(){
        const { blogs, loading} = this.props.resume;
        const { isAuthenticated, user } = this.props.auth;
        const itemsPerPage = 1; 
        let activeBlogs = blogs.slice (itemsPerPage * this.state.activePage - itemsPerPage, itemsPerPage * this.state.activePage)
        return(
            <Container>
             
            {loading ? (
            <div><Loading/></div>
            ) : (
                <div>
                    {/* blog modal */}
                    <BlogModal />

                    {/* card dialog */}
                    {this.cardDialog(blogs, user)}

                    <Grid style={{padding: 0}} id="todo">
                        {activeBlogs.map(({ _id, blog_name, blog_by, date }) => (
                            <Cell key={_id} data-id={_id}>   
                            { this.props.isAuthenticated && (user.is_admin === true) ? 
                                <Button className="remove-btn"
                                color="danger"
                                size="sm"
                                onClick= {this.onDeleteBlogClick.bind(this, _id)}>
                                    &times;
                                </Button> : null }
                                <Card shadow={5} className="cards-grid">
                                    <CardTitle className="card-title-image"></CardTitle>
                                    <CardText>
                                        <b>{blog_name}</b>
                                    </CardText>
                                    
                                    <CardActions border>
                                        <Button className="blog-read-me-button" onClick={this.handleOpenDialog.bind(this, _id)}>Read </Button>
                                        <p style={{ fontStyle:'italic', fontWeight:'bold'}}>By-{blog_by} <span style={{float:'right',}}>{Moment(date).format('Do MMMM YYYY')}</span></p> 
                                    </CardActions>
                                </Card>  
                            </Cell>  
                        ))} 
                    </Grid>
                </div> 
                )}
                <Pagination
                activePage={this.state.activePage}
                itemsCountPerPage={1}
                totalItemsCount={2}
                pageRangeDisplayed={1}
                onChange={this.handlePageChange.bind(this)}
                itemClass='page-item'
                linkClass='page-link'
/>
            </Container>
        ) 
   }
}

const mapStateToProps = (state) => ({
    resume: state.resume,
    isAuthenticated : state.auth.isAuthenticated,
    auth: state.auth,
    loading: state.apiCallsInProgress > 0
});

export default connect(mapStateToProps, {getBlog, deleteBlog }) (Blogs);

//current ui

解决方案

This is the default style of react-js-pagination. You have to style it yourself. However as far as I see, you are using bootstrap in your application, so you could use their styles in the following way:

<Pagination
                activePage={this.state.activePage}
                itemsCountPerPage={1}
                totalItemsCount={2}
                pageRangeDisplayed={1}
                onChange={this.handlePageChange.bind(this)}
                itemClass='page-item'
                linkClass='page-link'
/>

这篇关于如何在Reactjs应用程序中使用分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆