Skip to content

Commit

Permalink
Pagination Links
Browse files Browse the repository at this point in the history
  • Loading branch information
douglas committed Feb 7, 2020
1 parent 73cfec9 commit 5674fa2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function index(){
$this->view("home",
[
"users" => $this->users->select()->paginate(2)->get(),
"title" => "Listando Usuários"
"title" => "Listando Usuários",
"links" => $this->users->links()
]
);

Expand Down
2 changes: 1 addition & 1 deletion app/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class User extends Model
{
protected $table = "users";
protected $table = "user";
}
55 changes: 52 additions & 3 deletions app/traits/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,68 @@

trait Links
{
protected $maxLinks;
protected $maxLinks = 4;

private function previous(){

if($this->page > 1){


$previous = $this->page - 1;

$links = "<li class=\"page-item\"><a class=\"page-link\" href=\"?page=1\">[1]</a></li>";
$links .= "<li class=\"page-item\">
<a class=\"page-link\" href=\"?page={$previous}\" aria-label=\"Previous\">
<span aria-hidden=\"true\">&laquo;</span>
<span class=\"sr-only\">Previous</span>
</a>
</li>";

return $links;
}

}

private function next(){

if($this->page < $this->pages){

$next = $this->page + 1;

$links = "<li class=\"page-item\">
<a class=\"page-link\" href=\"?page={$next}\" aria-label=\"Next\">
<span aria-hidden=\"true\">&raquo;</span>
<span class=\"sr-only\">Next</span>
</a>
</li>";
$links .= "<li class=\"page-item\"><a class=\"page-link\" href=\"?page={$this->pages}\">[{$this->pages}]</a></li>";

return $links;
}

}

public function links(){
public function links()
{
if($this->page > 0){

$links = "<nav aria-label=\"Page navigation example\">
<ul class=\"pagination\">";

$links.= $this->previous();

for($i = $this->page - $this->maxLinks;$i <= $this->page + $this->maxLinks;$i++){
$active = ($this->page == $i) ? "active" : "";
if($i > 0 && $i <= $this->pages){
$links .= "<li class=\"page-item {$active}\"><a class=\"page-link \" href=\"?page={$i}\">{$i}</a></li>";

}
}

$links.= $this->next();
$links.= "</ul>
</nav>";

return $links;
}
}
}
6 changes: 5 additions & 1 deletion app/traits/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

trait Read
{
use Links;

/**
* Método responsável por
* @param string $fields
Expand Down Expand Up @@ -103,4 +103,8 @@ public function paginate($perPage){

return $this;
}

public function links(){
return $this->paginate->links();
}
}
4 changes: 4 additions & 0 deletions src/Paginate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace Core;


use App\traits\Links;

class Paginate
{
private $page;
Expand All @@ -12,6 +14,8 @@ class Paginate
private $pages;
private $registers;

use Links;

private function current(){
$this->page = filter_input(INPUT_GET,"page",FILTER_SANITIZE_NUMBER_INT) ?? 1;
}
Expand Down
1 change: 1 addition & 0 deletions views/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ <h1>Usuários</h1>
{% endfor %}
</tbody>
</table>
{{ links | raw }}
</div>
</div>
{% endblock %}

0 comments on commit 5674fa2

Please sign in to comment.