diff --git a/src/components/AddPlayerInput.js b/src/components/AddPlayerInput.js index 5d914d8..5cba264 100755 --- a/src/components/AddPlayerInput.js +++ b/src/components/AddPlayerInput.js @@ -6,15 +6,29 @@ import styles from './AddPlayerInput.css'; class AddPlayerInput extends Component { render() { return ( - +
+ +
+ +
+
+ +
+
); } @@ -28,13 +42,23 @@ class AddPlayerInput extends Component { handleChange(e) { this.setState({ name: e.target.value }); } + handleChangePosition(e) { + this.setState({ position: e.target.value }); + } handleSubmit(e) { - const name = e.target.value.trim(); - if (e.which === 13) { - this.props.addPlayer(name); - this.setState({ name: '' }); + const name = this.state.name; + const position = this.state.position + if(!name || !position){ + alert("name or position don't empty") + return false; } + + this.props.addPlayer({ + name,position + }); + this.setState({ name: ''}); + } } diff --git a/src/components/PlayerList.js b/src/components/PlayerList.js index 7b40246..8671663 100755 --- a/src/components/PlayerList.js +++ b/src/components/PlayerList.js @@ -2,25 +2,114 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import styles from './PlayerList.css'; import PlayerListItem from './PlayerListItem'; +import { connect } from 'react-redux'; +import Pagination from './pagination'; +import './style.css' + +//import {addPlayer, deletePlayer, starPlayer} from "../actions/PlayersActions"; class PlayerList extends Component { + constructor(props){ + super(props) + + + this.state = { + indexList : this.props.players.slice(0,5), //初始化第一页,只取5条 + totalNum:this.props.players.length,//总记录数 + totalData:[], + current: 1, //当前页码 + pageSize:5, //每页显示的条数5条 + totalPage:Math.ceil(this.props.players.length/5),//总页数 + } + } + componentWillReceiveProps(nextProps, nextContext) { + + this.setState({ + totalPage:Math.ceil(nextProps.players.length/5), + indexList: nextProps.players.slice(0,5) + },()=>{ + if(this.state.totalPage >= this.state.current){ + this.pageClick(this.state.current) + } + }) + + } + + componentWillMount(){ + + } + splitPlayersArr(data){ + let arr = []; + for(var i=0;i 0){ + this.pageClick(cur); + } + } + //下一步 + goNext(){ + + let cur = this.state.current; + if(cur < this.state.totalPage){ + this.pageClick(cur + 1); + } + } render() { return ( - +
+ + +
); } } @@ -31,3 +120,18 @@ PlayerList.propTypes = { }; export default PlayerList; + + +// function mapStateToProps(state) { +// return state; +// } +// function mapDispatchToProps(dispatch) { +// return { +// changePlayerId: (state) => dispatch(state) +// } +// } +// +// export default connect( +// mapStateToProps, +// mapDispatchToProps, +// )(PlayerList); diff --git a/src/components/PlayerListItem.js b/src/components/PlayerListItem.js index ec9758c..5a01303 100755 --- a/src/components/PlayerListItem.js +++ b/src/components/PlayerListItem.js @@ -20,7 +20,8 @@ class PlayerListItem extends Component {
diff --git a/src/components/pagination.js b/src/components/pagination.js new file mode 100644 index 0000000..db261e0 --- /dev/null +++ b/src/components/pagination.js @@ -0,0 +1,61 @@ +import React, { Component } from 'react'; + +class Pagination extends Component{ + render(){ + let _this = this; + //当前页码 + let cur = this.props.current; + + //显示分页按钮 + let pageNum = []; + let begin; + let len; + if(_this.props.totalPage > 5){ + len = 5; + if(cur >= (_this.props.totalPage-2)){ + begin = _this.props.totalPage - 4; + }else if(cur <= 3){ + begin = 1; + }else{ + begin = cur - 2; + } + }else{ + len = _this.props.totalPage; + begin = 1; + } +//根据返回的总记录数计算当前页显示的数据 + for(let i = 0; i < len; i ++){ + let cur = this.props.current; + let showI = begin + i; + if(cur === showI){ + pageNum.push({num : showI, cur : true}); + }else{ + pageNum.push({num : showI, cur : false}); + } + } + return( +
+
+
+
+ +
+ +
+ prev page + + { + pageNum.map(function(curPageNum){ + return({curPageNum.num}) }) + } + + + next page +
+ +
+
+ ) + } +} +export default Pagination; diff --git a/src/components/style.css b/src/components/style.css new file mode 100644 index 0000000..a72f698 --- /dev/null +++ b/src/components/style.css @@ -0,0 +1,11 @@ +.paginationDiv a{ + border: 1px solid #ddd; + padding: 5px 10px; +} +.paginationDiv a.current{ + color: red; + border: 1px solid red; +} +.paginationDiv a.disable{ + background: #ccc; +} diff --git a/src/index.js b/src/index.js index 95c69b7..6a6eb8e 100755 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,6 @@ import React from 'react'; import ReactDOM from 'react-dom'; import App from './containers/App'; import './index.css'; - ReactDOM.render( , document.getElementById('root') diff --git a/src/reducers/playerlist.js b/src/reducers/playerlist.js index 1bc7457..38a0e33 100755 --- a/src/reducers/playerlist.js +++ b/src/reducers/playerlist.js @@ -44,14 +44,15 @@ const initialState = { export default function players(state = initialState, action) { switch (action.type) { case types.ADD_PLAYER: + return { ...state, playersById: [ ...state.playersById, { - name: action.name, + name: action.name.name, team: 'LOS ANGELES LAKERS', - position: 'SF', + position: action.name.position, }, ], }; @@ -63,9 +64,11 @@ export default function players(state = initialState, action) { ), }; case types.STAR_PLAYER: + let players = [...state.playersById]; let player = players.find((item, index) => index === action.id); player.starred = !player.starred; + return { ...state, playersById: players,