Списки: фильтры, сортировка, выгрузка и загрузка CSV
This commit is contained in:
1 parent
9075c817dc
commit
c4e9f6fc37
31 files changed
+1734
-184
No files matched your search
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Форма фильтров списка. Рисуется по описанию (`Support\Filter`), поэтому
|
||||
* новый фильтр появляется в разметке сам — правится только контроллер.
|
||||
*
|
||||
* @var \Rsgrinko\Proton\Support\Filters $filters
|
||||
* @var string $route имя маршрута раздела
|
||||
* @var int $total сколько всего нашлось
|
||||
* @var string $submit подпись кнопки
|
||||
* @var string $export имя маршрута выгрузки, если она есть
|
||||
*/
|
||||
|
||||
use Rsgrinko\Proton\Support\Filter;
|
||||
use Rsgrinko\Proton\View\View;
|
||||
|
||||
$total = $total ?? -1;
|
||||
$submit = $submit ?? 'Показать';
|
||||
$export = $export ?? '';
|
||||
?>
|
||||
<div class="card">
|
||||
<form method="get" action="<?= View::e(View::route($route)) ?>">
|
||||
<?php if ($filters->sort() !== '') { ?>
|
||||
<input type="hidden" name="sort" value="<?= View::e($filters->sort()) ?>">
|
||||
<input type="hidden" name="dir" value="<?= View::e($filters->direction()) ?>">
|
||||
<?php } ?>
|
||||
|
||||
<div class="filters">
|
||||
<?php foreach ($filters->fields() as $field) { ?>
|
||||
<label>
|
||||
<span><?= View::e($field->label) ?></span>
|
||||
|
||||
<?php if ($field->type === Filter::SEARCH) { ?>
|
||||
<input type="search" name="<?= View::e($field->key) ?>" value="<?= View::e($filters->value($field->key)) ?>" placeholder="<?= View::e($field->placeholder) ?>">
|
||||
<?php } elseif ($field->type === Filter::TEXT) { ?>
|
||||
<input type="text" name="<?= View::e($field->key) ?>" value="<?= View::e($filters->value($field->key)) ?>" placeholder="<?= View::e($field->placeholder) ?>">
|
||||
<?php } elseif ($field->type === Filter::DATES) { ?>
|
||||
<span class="range">
|
||||
<input type="date" name="<?= View::e($field->key) ?>_from" value="<?= View::e($filters->value($field->key . '_from')) ?>">
|
||||
<input type="date" name="<?= View::e($field->key) ?>_to" value="<?= View::e($filters->value($field->key . '_to')) ?>">
|
||||
</span>
|
||||
<?php } else { ?>
|
||||
<select name="<?= View::e($field->key) ?>">
|
||||
<option value="">неважно</option>
|
||||
<?php foreach ($field->options as $value => $title) { ?>
|
||||
<option value="<?= View::e($value) ?>" <?= $filters->value($field->key) === (string) $value ? 'selected' : '' ?>><?= View::e($title) ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
<?php } ?>
|
||||
</label>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<div class="filter-actions row">
|
||||
<button type="submit" class="primary"><?= View::e($submit) ?></button>
|
||||
|
||||
<?php if ($filters->active()) { ?>
|
||||
<a class="btn" href="<?= View::e(View::route($route)) ?>">Сбросить</a>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($export !== '') { ?>
|
||||
<a class="btn" href="<?= View::e(View::route($export, $filters->params())) ?>">Выгрузить CSV</a>
|
||||
<?php } ?>
|
||||
|
||||
<span class="spacer"></span>
|
||||
|
||||
<?php if ($total >= 0) { ?>
|
||||
<span class="muted small">Всего: <?= (int) $total ?></span>
|
||||
<?php } ?>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@@ -210,8 +210,20 @@ $unread = $viewer->isGuest() ? 0 : UserNotification::unreadFor($viewer->id());
|
||||
|
||||
.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; align-items: end; }
|
||||
.filters label { margin: 0; }
|
||||
.filters .range { display: flex; gap: 6px; }
|
||||
.filters .range input { min-width: 0; }
|
||||
.filter-actions { margin-top: 12px; }
|
||||
|
||||
.chart { display: flex; align-items: flex-end; gap: 2px; height: 120px; margin: 12px 0 6px; }
|
||||
.chart .col { flex: 1 1 0; min-width: 2px; min-height: 2px; border-radius: 3px 3px 0 0; background: var(--accent); opacity: .85; }
|
||||
.chart .col:hover { opacity: 1; }
|
||||
.chart .col.empty { background: var(--border); }
|
||||
.chart-legend { display: flex; justify-content: space-between; }
|
||||
|
||||
a.sort { color: inherit; text-decoration: none; white-space: nowrap; }
|
||||
a.sort:hover { text-decoration: underline; }
|
||||
a.sort .arrow { margin-left: 4px; color: var(--accent); }
|
||||
|
||||
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; word-break: break-word; }
|
||||
.flash.ok { background: var(--ok-bg); color: var(--ok); }
|
||||
.flash.error { background: var(--err-bg); color: var(--err); }
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Заголовок колонки, по которой можно сортировать. Ссылка тащит за собой
|
||||
* текущие фильтры, иначе клик по стрелке сбрасывал бы отбор.
|
||||
*
|
||||
* @var \Rsgrinko\Proton\Support\Filters $filters
|
||||
* @var string $route имя маршрута раздела
|
||||
* @var string $column колонка из белого списка сортировки
|
||||
* @var string $label подпись
|
||||
*/
|
||||
|
||||
use Rsgrinko\Proton\View\View;
|
||||
|
||||
if (!$filters->sortableBy($column)) {
|
||||
echo View::e($label);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$current = $filters->sort() === $column;
|
||||
// По той же колонке — переворачиваем, по новой — начинаем с убывания:
|
||||
// свежее и большее чаще всего и нужно первым
|
||||
$direction = $current && $filters->direction() === 'desc' ? 'asc' : 'desc';
|
||||
$params = array_merge($filters->params(), ['sort' => $column, 'dir' => $direction]);
|
||||
?>
|
||||
<a class="sort<?= $current ? ' current' : '' ?>" href="<?= View::e(View::route($route, $params)) ?>"><?= View::e($label) ?><?php if ($current) { ?><span class="arrow"><?= $filters->direction() === 'asc' ? '↑' : '↓' ?></span><?php } ?></a>
|
||||
Reference in new issue
Block a user