bd 3.0
This commit is contained in:
69
src/widgets/PaginationWidget.php
Normal file
69
src/widgets/PaginationWidget.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace itguild\forms\widgets;
|
||||
|
||||
use itguild\forms\debug\Debug;
|
||||
|
||||
class PaginationWidget extends BaseWidget
|
||||
{
|
||||
|
||||
private string $html = "";
|
||||
private int $perPage = 10;
|
||||
private int $countItem;
|
||||
private int $currentPage = 1;
|
||||
private int $countPages;
|
||||
|
||||
private string $baseUrl;
|
||||
|
||||
public function __construct($countItem, $perPage, $currentPage, $baseUrl)
|
||||
{
|
||||
$this->countItem = $countItem;
|
||||
$this->perPage = $perPage;
|
||||
$this->currentPage = $currentPage;
|
||||
$this->baseUrl = $baseUrl;
|
||||
|
||||
$this->countPages = ceil($this->countItem / $perPage);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null;
|
||||
$next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null;
|
||||
$btns = $prev ? "<li class='page-item'><a class='page-link' href='$this->baseUrl/$prev'>$prev</a></li>" : "";
|
||||
$btns .= "<li class='page-item'><a class='page-link active' href='#'>$this->currentPage</a></li>";
|
||||
$btns .= $next ? "<li class='page-item'><a class='page-link' href='$this->baseUrl/$next'>$next</a></li>" : "";
|
||||
|
||||
$this->html = str_replace('{btns}', $btns, $this->getTemplate());
|
||||
$this->html = str_replace('{previous_link}', $this->baseUrl . "/1", $this->html);
|
||||
$this->html = str_replace('{next_link}', $this->baseUrl . "/" . $this->countPages, $this->html);
|
||||
}
|
||||
|
||||
public function render(): void
|
||||
{
|
||||
echo $this->html;
|
||||
}
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
return $this->html;
|
||||
}
|
||||
|
||||
private function getTemplate()
|
||||
{
|
||||
return '<nav aria-label="Page navigation example">
|
||||
<ul class="pagination">
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="{previous_link}" aria-label="Previous">
|
||||
<span aria-hidden="true">«</span>
|
||||
</a>
|
||||
</li>
|
||||
{btns}
|
||||
<li class="page-item">
|
||||
<a class="page-link" href="{next_link}" aria-label="Next">
|
||||
<span aria-hidden="true">»</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>';
|
||||
}
|
||||
}
|
@ -12,10 +12,14 @@ class TableJsonWidget extends BaseWidget
|
||||
private string $html = '';
|
||||
private string $json;
|
||||
|
||||
private int $count = 0;
|
||||
private \Closure|false $beforePrintCell;
|
||||
|
||||
private array $data;
|
||||
|
||||
public function __construct(string $json)
|
||||
{
|
||||
$this->beforePrintCell = false;
|
||||
$this->json = $json;
|
||||
$this->data = json_decode($this->json, true);
|
||||
}
|
||||
@ -33,6 +37,7 @@ class TableJsonWidget extends BaseWidget
|
||||
}
|
||||
|
||||
$this->html .= "<thead><tr>";
|
||||
$this->html .= "<th>" . "ID" . "</th>";
|
||||
foreach ($this->data['meta']['columns'] as $key => $column){
|
||||
$this->html .= "<th>" . $column . "</th>";
|
||||
}
|
||||
@ -44,8 +49,15 @@ class TableJsonWidget extends BaseWidget
|
||||
if ($this->data['data']){
|
||||
foreach ($this->data['data'] as $col){
|
||||
$this->html .= "<tr>";
|
||||
$this->count += 1;
|
||||
$this->html .= "<td>" . $this->count . "</td>";
|
||||
foreach ($col as $key => $row){
|
||||
if ($this->issetColumn($key)){
|
||||
if ($this->beforePrintCell){
|
||||
$hook = $this->beforePrintCell;
|
||||
$row = $hook($key, $row);
|
||||
}
|
||||
|
||||
$this->html .= "<td>" . $row . "</td>";
|
||||
}
|
||||
}
|
||||
@ -97,4 +109,9 @@ class TableJsonWidget extends BaseWidget
|
||||
{
|
||||
echo $this->html;
|
||||
}
|
||||
|
||||
public function setBeforePrintCell(\Closure $closure): void
|
||||
{
|
||||
$this->beforePrintCell = $closure;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user