Commit b3565c27 authored by Elias Nahum's avatar Elias Nahum

Pagination with first, prev, next and last

parent 4505d6e4
...@@ -4,7 +4,25 @@ import {browserHistory} from 'react-router'; ...@@ -4,7 +4,25 @@ import {browserHistory} from 'react-router';
export default class Pagination extends React.Component { export default class Pagination extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.handleFirst = this.handleFirst.bind(this);
this.handlePrev = this.handlePrev.bind(this);
this.handleChange = this.handleChange.bind(this); this.handleChange = this.handleChange.bind(this);
this.handleNext = this.handleNext.bind(this);
this.handleLast = this.handleLast.bind(this);
}
handleFirst(e) {
e.preventDefault();
browserHistory.push(`/${this.props.url}`);
}
handlePrev(e) {
e.preventDefault();
const prevPage = this.props.currentPage - 1;
if (prevPage > 1) {
browserHistory.push(`/${this.props.url}?page=${prevPage}`);
} else {
browserHistory.push(`/${this.props.url}`);
}
} }
handleChange(e) { handleChange(e) {
e.preventDefault(); e.preventDefault();
...@@ -17,20 +35,60 @@ export default class Pagination extends React.Component { ...@@ -17,20 +35,60 @@ export default class Pagination extends React.Component {
browserHistory.push(`/${this.props.url}${pageUrl}`); browserHistory.push(`/${this.props.url}${pageUrl}`);
} }
handleNext(e) {
e.preventDefault();
browserHistory.push(`/${this.props.url}?page=${this.props.currentPage + 1}`);
}
handleLast(e) {
e.preventDefault();
browserHistory.push(`/${this.props.url}?page=${this.props.totalPages}`);
}
render() { render() {
const total = this.props.totalPages; const total = this.props.totalPages;
const current = this.props.currentPage; const current = this.props.currentPage;
const pages = []; const pages = [];
// let first; let first;
// let prev; let prev;
// let next; let next;
// let last; let last;
let i = 1; let i = 1;
// if (current > 1 && current < total) { if (current > 1 && current <= total) {
// first = (
// } <li key='first-page'>
<a
onClick={this.handleFirst}
>{'Primera'}</a>
</li>
);
prev = (
<li key='prev-page'>
<a
onClick={this.handlePrev}
>{'Ant.'}</a>
</li>
);
}
if (current < total) {
next = (
<li key='next-page'>
<a
onClick={this.handleNext}
>{'Sig.'}</a>
</li>
);
last = (
<li key='last-page'>
<a
onClick={this.handleLast}
>{'Última'}</a>
</li>
);
}
for (; i <= total; i++) { for (; i <= total; i++) {
if (current === i) { if (current === i) {
...@@ -47,7 +105,9 @@ export default class Pagination extends React.Component { ...@@ -47,7 +105,9 @@ export default class Pagination extends React.Component {
<li key={`page-${i}`}> <li key={`page-${i}`}>
<a <a
onClick={this.handleChange} onClick={this.handleChange}
>{i.toString()}</a> >
{i.toString()}
</a>
</li> </li>
); );
} }
...@@ -56,7 +116,11 @@ export default class Pagination extends React.Component { ...@@ -56,7 +116,11 @@ export default class Pagination extends React.Component {
return ( return (
<div className='pagination'> <div className='pagination'>
<ul className='pagination'> <ul className='pagination'>
{first}
{prev}
{pages} {pages}
{next}
{last}
</ul> </ul>
</div> </div>
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment