Last active
August 29, 2015 14:27
-
-
Save HugoPresents/e315f15b1ea891ca63bb to your computer and use it in GitHub Desktop.
Phalcon simple pagination use bootstrap style
This file contains 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
{% for message in paginator.items %} | |
{{ message.title }} | |
{% endfor %} | |
{{ paginator.output() }} |
This file contains 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
$current_page = (int) $_GET["page"]; | |
$pagination = new PaginatorModel([ | |
'data' => Message::find($options), | |
'limit' => 20, | |
'page' => $current_page | |
]); | |
$paginator = new Paginator($pagination, $this->request->getQuery()); | |
$this->view->setVars(compact('paginator', 'keyword')); |
This file contains 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
<?php | |
class Paginator | |
{ | |
public $current; | |
public $total_pages; | |
public $next; | |
public $before; | |
public $items; | |
public $query = []; | |
public $limit = 1; | |
public function __construct(Phalcon\Paginator\AdapterInterface $paginate, array $query) | |
{ | |
$this->limit = $paginate->getLimit(); | |
$paginate = $paginate->getPaginate(); | |
$this->current = $paginate->current; | |
$this->total_pages = $paginate->total_pages; | |
$this->next = $paginate->next; | |
$this->before = $paginate->before; | |
$this->items = $paginate->items; | |
unset($query['_url']); | |
unset($query['page']); | |
$this->query = $query; | |
} | |
public function output() | |
{ | |
$layout = <<<EOF | |
<div class="row"> | |
<div class="col-md-6 text-left"> | |
<span>总共 %d 页,当前第 %d 页</span> | |
</div> | |
<div class="col-md-6 text-right"> | |
<ul class="pagination"> | |
%s | |
</ul> | |
</div> | |
</div> | |
EOF; | |
$links = ""; | |
if ($this->before < $this->current) { | |
$links .= sprintf('<li><a href="%s">«</a></li>', $this->pageUrl($this->before)); | |
if ($this->current - 3 > 0) { | |
$links .= sprintf('<li><a href="%s">%d</a></li>', $this->pageUrl($this->current - 3), $this->current - 3); | |
} | |
if ($this->current - 2 > 0) { | |
$links .= sprintf('<li><a href="%s">%d</a></li>', $this->pageUrl($this->current - 2), $this->current - 2); | |
} | |
if ($this->current - 1 > 0) { | |
$links .= sprintf('<li><a href="%s">%d</a></li>', $this->pageUrl($this->current - 1), $this->current - 1); | |
} | |
} else { | |
$links .= '<li class="disabled"><a href="#">«</a></li>'; | |
} | |
$links .= sprintf('<li class="active"><a href="#">%d</a></li>', $this->current); | |
if ($this->next > $this->current) { | |
if ($this->current + 1 <= $this->total_pages) { | |
$links .= sprintf('<li><a href="%s">%d</a></li>', $this->pageUrl($this->current + 1), $this->current + 1); | |
if ($this->current + 2 <= $this->total_pages) { | |
$links .= sprintf('<li><a href="%s">%d</a></li>', $this->pageUrl($this->current + 2), $this->current + 2); | |
if ($this->current + 3 <= $this->total_pages) { | |
$links .= sprintf('<li><a href="%s">%d</a></li>', $this->pageUrl($this->current + 3), $this->current + 3); | |
} | |
} | |
} | |
$links .= sprintf('<li><a href="%s">»</a></li>', $this->pageUrl($this->next)); | |
} else { | |
$links .= '<li class="disabled"><a href="#">»</a></li>'; | |
} | |
$html = sprintf($layout, $this->total_pages, $this->current, $links); | |
return $html; | |
} | |
private function pageUrl($page) | |
{ | |
$args = $this->query; | |
$args['page'] = $page; | |
return '?' . http_build_query($args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment