Created
November 8, 2013 23:58
-
-
Save ceekz/7379560 to your computer and use it in GitHub Desktop.
Pagination Helper : Mojolicious + Data::Pageset + Bootstrap 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Data::Pageset; | |
$self->helper( | |
pagination => sub { | |
my ($self, $total_entries, $current_page) = @_; | |
my $dp = Data::Pageset->new({ | |
'total_entries' => $total_entries, | |
'entries_per_page' => 20, | |
'current_page' => (! $current_page || $current_page < 1) ? 1 : $current_page, | |
'pages_per_set' => 10, | |
'mode' => 'slide', | |
}); | |
my $html = '<ul class="pagination">'; | |
if ($dp->previous_page) { | |
$html .= sprintf qq!<li><a href="%s">««</a></li>!, $self->url_with->query([page => undef]); | |
$html .= sprintf qq!<li><a href="%s">«</a></li>!, $self->url_with->query([page =>$dp->previous_page]); | |
} else { | |
$html .= '<li><span>««</span></li>'; | |
$html .= '<li><span>«</span></li>'; | |
} | |
foreach my $p (@{$dp->pages_in_set()}) { | |
$html .= sprintf qq!<li%s><a href="%s">%d</a></li>!, ($p == $dp->current_page()) ? ' class="active"' : '', $self->url_with->query([page => $p]), $p; | |
} | |
if ($dp->next_page) { | |
$html .= sprintf qq!<li><a href="%s">»</a></li>!, $self->url_with->query([page => $dp->next_page]); | |
$html .= sprintf qq!<li><a href="%s">»»</a></li>!, $self->url_with->query([page => $dp->last_page]); | |
} else { | |
$html .= '<li><span>»</span></li>'; | |
$html .= '<li><span>»»</span></li>'; | |
} | |
$html .= '</ul>'; | |
return b($html); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment