Skip to content

Commit

Permalink
Add example of yii-dataview (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Feb 14, 2022
1 parent 5664d81 commit 1e1c3d2
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 70 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -77,6 +77,7 @@
"yiisoft/yii-cycle": "2.0.x-dev",
"yiisoft/yii-debug": "^3.0@dev",
"yiisoft/yii-debug-api": "^3.0@dev",
"yiisoft/yii-dataview": "^3.0@dev",
"yiisoft/yii-event": "^1.0",
"yiisoft/yii-http": "^1.0",
"yiisoft/yii-middleware": "dev-master",
Expand Down
22 changes: 22 additions & 0 deletions resources/asset/css/site.css
Expand Up @@ -2,3 +2,25 @@ label.required:not(:empty):after {
color: #dc3545;
content: '\a0*';
}

/* add sorting icons to gridview sort links */
a.asc:after, a.desc:after {
content: '';
left: 3px;
display: inline-block;
width: 0;
height: 0;
border: solid 5px transparent;
margin: 4px 4px 2px 4px;
background: transparent;
}

a.asc:after {
border-bottom: solid 7px #212529;
border-top-width: 0;
}

a.desc:after {
border-top: solid 7px #212529;
border-bottom-width: 0;
}
44 changes: 29 additions & 15 deletions src/User/Controller/UserController.php
Expand Up @@ -7,6 +7,7 @@
use App\User\UserRepository;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Router\CurrentRoute;
Expand All @@ -16,27 +17,40 @@ final class UserController
{
private const PAGINATION_INDEX = 5;

private ViewRenderer $viewRenderer;

public function __construct(ViewRenderer $viewRenderer)
public function __construct(private ViewRenderer $viewRenderer)
{
$this->viewRenderer = $viewRenderer->withControllerName('user');
}

public function index(UserRepository $userRepository, CurrentRoute $currentRoute): Response
{
$pageNum = (int)$currentRoute->getArgument('page', '1');

$dataReader = $userRepository->findAll()->withSort(Sort::only(['login'])->withOrderString('login'));
$paginator = (new OffsetPaginator($dataReader))
->withPageSize(self::PAGINATION_INDEX)
->withCurrentPage($pageNum);

return $this->viewRenderer->render('index', ['paginator' => $paginator]);
public function index(
CurrentRoute $currentRoute,
ServerRequestInterface $request,
UserRepository $userRepository
): Response {
$page = (int)$currentRoute->getArgument('page', '1');
$sortOrderString = $request->getQueryParams();

$dataReader = $userRepository
->findAll()
->withSort(Sort::only(['id', 'login'])->withOrderString($sortOrderString['sort'] ?? ''));

$paginator = (new OffsetPaginator($dataReader))->withPageSize(self::PAGINATION_INDEX);

return $this->viewRenderer->render(
'index',
[
'page' => $page,
'paginator' => $paginator,
'sortOrder' => $sortOrderString['sort'] ?? '',
]
);
}

public function profile(CurrentRoute $currentRoute, UserRepository $userRepository, ResponseFactoryInterface $responseFactory): Response
{
public function profile(
CurrentRoute $currentRoute,
ResponseFactoryInterface $responseFactory,
UserRepository $userRepository
): Response {
$login = $currentRoute->getArgument('login');
$item = $userRepository->findByLogin($login);
if ($item === null) {
Expand Down
122 changes: 67 additions & 55 deletions views/user/index.php
Expand Up @@ -7,66 +7,78 @@
* @var \Yiisoft\Translator\TranslatorInterface $translator
* @var \Yiisoft\Router\UrlGeneratorInterface $urlGenerator
* @var \Yiisoft\View\WebView $this
* @var int $currentPage
*/

use App\Widget\OffsetPagination;
use Yiisoft\Html\Html;
use Yiisoft\Yii\DataView\GridView;

$this->setTitle($translator->translate('menu.users'));
?>

$pagination = OffsetPagination::widget()
->paginator($paginator)
->urlGenerator(fn ($page) => $urlGenerator->generate('user/index', ['page' => $page]));
<div class="container">
<div class="text-end">
<?= Html::a('API v1 Info', $urlGenerator->generate('api/info/v1'), ['class' => 'btn btn-link']) ?>
<?= Html::a('API v2 Info', $urlGenerator->generate('api/info/v2'), ['class' => 'btn btn-link']) ?>
<?= Html::a('API Users List Data', $urlGenerator->generate('api/user/index'), ['class' => 'btn btn-link'])?>
</div>

echo Html::tag('h1', $this->getTitle());
echo Html::p('Total users: ' . $paginator->getTotalItems(), ['class' => 'text-muted']);
echo Html::a(
'API v1 Info',
$urlGenerator->generate('api/info/v1'),
['class' => 'btn btn-link']
), '<br>';
echo Html::a(
'API v2 Info',
$urlGenerator->generate('api/info/v2'),
['class' => 'btn btn-link']
), '<br>';
echo Html::a(
'API Users List Data',
$urlGenerator->generate('api/user/index'),
['class' => 'btn btn-link']
), '<br>';
?>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Created at</th>
</tr>
</thead>
<tbody>
<?php
/** @var \App\User\User $item */
foreach ($paginator->read() as $item) {
echo Html::openTag('tr');
echo Html::openTag('td');
echo Html::a(
Html::encode($item->getLogin()),
$urlGenerator->generate('user/profile', ['login' => $item->getLogin()]),
['class' => 'btn btn-link']
);
echo Html::a(
Html::encode('API User Data'),
$urlGenerator->generate('api/user/profile', ['login' => $item->getLogin()]),
['class' => 'btn btn-link']
);
echo Html::closeTag('td');
echo Html::tag('td', $item->getCreatedAt()->format('r'));
echo Html::closeTag('tr');
}
?>
</tbody>
</table>
<?php
if ($pagination->isRequired()) {
echo $pagination;
}
<div class="card shadow">
<h5 class="card-header bg-primary text-white">
<i class="bi bi-people"></i> List users
</h5>
<?= GridView::widget()->columns(
[
[
'attribute()' => ['id'],
'value()' => [static function ($model): string {
return $model->getId();
}],
],
[
'attribute()' => ['login'],
'value()' => [static fn ($model): string => $model->getLogin()],
],
[
'header()' => ['create_at'],
'value()' => [static fn ($model): string => $model->getCreatedAt()->format('r')],
],
[
'header()' => ['api'],
'value()' => [
static function ($model) use ($urlGenerator): string {
return Html::a(
'API User Data',
$urlGenerator->generate('api/user/profile', ['login' => $model->getLogin()]),
['class' => 'btn btn-link', 'target' => '_blank'],
)->render();
},
],
],
[
'header()' => ['profile'],
'value()' => [
static function ($model) use ($urlGenerator): string {
return Html::a(
Html::tag('i', '', [
'class' => 'bi bi-person-fill ms-1',
'style' => 'font-size: 1.5em;',
]),
$urlGenerator->generate('user/profile', ['login' => $model->getLogin()]),
['class' => 'btn btn-link'],
)->render();
},
],
],
]
)
->currentPage($page)
->pageArgument(true)
->paginator($paginator)
->requestArguments(['sort' => $sortOrder, 'page' => $page])
->rowOptions(['class' => 'align-middle'])
->summaryOptions(['class' => 'mt-3 me-3 summary text-end'])
->tableOptions(['class' => 'table table-striped text-center h-75']) ?>
</div>
</div>

0 comments on commit 1e1c3d2

Please sign in to comment.