Skip to content

分页器 #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<!--
Expand Down
8 changes: 8 additions & 0 deletions src/actions/PlayersActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ export function starPlayer(id) {
id,
};
}

export function currentPlayer(currentPage, pageSize) {
return {
type: types.CURRENT_PLAYER,
currentPage,
pageSize
};
}
36 changes: 36 additions & 0 deletions src/components/Pagination.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
:local(.pagination) {
overflow: hidden;
margin: 0;
padding: 0;
}
.pagination > div {
float: left;
margin-right: 5px;
}
.pagination .totals{
overflow: hidden;
}
.pagination .totals > span{
float: left;
}
.total-num{
margin-right: 5px;
}
.pagination .bd{
overflow: hidden;
}
.pagination .bd > ul > li{
list-style: none outside none;
float: left;
width: 30px;
height: 30px;
line-height: 30px;
text-align: center;
cursor: pointer;
margin-right: 1px;
border: 1px solid #eaedf1;
}
.pagination .bd > ul > li.current-page{
background: #f00;
color: #fff;
}
131 changes: 131 additions & 0 deletions src/components/Pagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { currentPlayer } from '../actions/PlayersActions';
// import PropTypes from 'prop-types';
import './Pagination.css';

class Pagination extends Component {
state = {
totalNum: 0,
currentPage: 1,
totalPage: 0,
pageBtnList: [],
pageSize: 5,
sizeList: [5, 6, 7]
}
previousPage = () => {
this.setState(state => {
let currentPage = state.currentPage > 1 ? state.currentPage - 1 : state.currentPage;
this.props.dispatch(currentPlayer(currentPage, state.pageSize));
return { currentPage };
});
}
nextPage = () => {
this.setState(state => {
let currentPage = state.currentPage < state.totalPage ? state.currentPage + 1 : state.currentPage;
this.props.dispatch(currentPlayer(currentPage, state.pageSize));
return { currentPage }
});
}
pageBtnClick = (btnNum) => {
this.props.dispatch(currentPlayer(btnNum, this.state.pageSize));
this.setState({currentPage: btnNum});
}
pageSizeChangeHandle = (e) => {
let pageSize = +e.target.value;
this.props.dispatch(currentPlayer(this.state.currentPage, pageSize));
this.setState({pageSize});
}
setPagination = (playerlist = { playersById: [] }) => {
const { playersById, init_currentPage, init_pageSize} = playerlist;
let currentPage = init_currentPage || this.state.currentPage;
let pageSize = init_pageSize || this.state.pageSize;
let totalNum = playersById.length;
let totalPage = Math.ceil(playersById.length / pageSize);
let pageBtnList = [];
if (totalPage > 5) {
if (currentPage < 4) {
pageBtnList = [1, 2, 3, 4, 5];
} else {
pageBtnList = [currentPage -2, currentPage - 1, currentPage, currentPage + 1, currentPage + 2];
}
} else {
for (let i = 1; i <= totalPage; i++) {
pageBtnList.push(i);
}
}
this.setState({
currentPage,
pageSize,
totalNum,
totalPage,
pageBtnList
});
}
// calculateCurrentPlayers = () => {
// console.log(666);
// let {currentPage, pageSize} = this.state;
// let startIndex = (currentPage - 1) * pageSize;
// let len = this.props.playerlist.playersById.length;
// let endIndex = len > startIndex + pageSize ? startIndex + pageSize - 1 : len - 1;
// console.log(currentPlayer(startIndex, endIndex));
// this.props.dispatch(currentPlayer(startIndex, endIndex));
// }
componentDidMount () {
this.setPagination(this.props.playerlist);
}
componentWillReceiveProps (props) {
console.log(666, props);
this.setPagination(props.playerlist);
}
render() {
const { totalNum, totalPage, pageBtnList, sizeList, currentPage } = this.state;
return (
<div className="pagination" style={{display: totalNum > 5 ? 'block' : 'none'}}>
<div className="totals">
<span className="total-num">{`total ${totalNum} nums,`}</span>
<span className="total-page">{`total ${totalPage} pages`}</span>
</div>
<div className="pre">
<button type="button" onClick={this.previousPage}>previous page</button>
</div>
<div className="bd">
<ul>
{
pageBtnList.map((item, index) => (
<li key={index} className={currentPage - 1 === index ? 'current-page' : ''} onClick={() => this.pageBtnClick(item)}>{item}</li>
))
}
</ul>
</div>
<div className="next">
<button type="button" onClick={this.nextPage}>next page</button>
</div>
<div className="page-size">
<select name="pageSize" onChange={this.pageSizeChangeHandle}>
{
sizeList.map((sizeItem, sizeIndex) => (
<option value={sizeItem} key={sizeIndex}>{sizeItem}</option>
))
}
</select>
</div>
</div>
);
}
}

// Pagination.propTypes = {
// currentPage: PropTypes.number.isRequired,
// totalNum: PropTypes.number.isRequired
// };

const mapStateToProps = (state) => {
return state;
}

const mapDispatchToProps = (dispatch) => {
return dispatch;
}

export default connect(mapStateToProps, null)(Pagination);
1 change: 1 addition & 0 deletions src/components/PlayerList.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PlayerList extends Component {
team={player.team}
position={player.position}
starred={player.starred}
current={player.current}
{...this.props.actions}
/>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/PlayerListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import styles from './PlayerListItem.css';
class PlayerListItem extends Component {
render() {
return (
<li className={styles.playerListItem}>
<li className={styles.playerListItem} style={{display: this.props.current ? 'block' : 'none'}}>
<div className={styles.playerInfos}>
<div>
<span>{this.props.name}</span>
Expand Down Expand Up @@ -47,6 +47,7 @@ PlayerListItem.propTypes = {
team: PropTypes.string.isRequired,
position: PropTypes.string.isRequired,
starred: PropTypes.bool,
current: PropTypes.bool,
starPlayer: PropTypes.func.isRequired,
};

Expand Down
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as AddPlayerInput } from './AddPlayerInput';
export { default as PlayerList } from './PlayerList';
export { default as PlayerListItem } from './PlayerListItem';
export { default as Pagination } from './Pagination';
1 change: 1 addition & 0 deletions src/constants/ActionTypes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const ADD_PLAYER = 'ADD_PLAYER';
export const STAR_PLAYER = 'STAR_PLAYER';
export const DELETE_PLAYER = 'DELETE_PLAYER';
export const CURRENT_PLAYER = 'CURRENT_PLAYER';
3 changes: 2 additions & 1 deletion src/containers/PlayerListApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styles from './PlayerListApp.css';
import { connect } from 'react-redux';

import { addPlayer, deletePlayer, starPlayer } from '../actions/PlayersActions';
import { PlayerList, AddPlayerInput } from '../components';
import { PlayerList, AddPlayerInput, Pagination } from '../components';

class PlayerListApp extends Component {
render() {
Expand All @@ -22,6 +22,7 @@ class PlayerListApp extends Component {
<h1>NBA Players</h1>
<AddPlayerInput addPlayer={actions.addPlayer} />
<PlayerList players={playersById} actions={actions} />
<Pagination />
</div>
);
}
Expand Down
Loading