Skip to content

Add OffsetPagination::addLinkAttributes() and GridView::filterFormAttributes(). Fix sortableLinkAttributes usage. #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Column/Base/GlobalContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function translate(string|Stringable $id): string
* @param Cell $cell The header cell to prepare.
* @param string $property The property name for sorting.
*
* @psalm-return list{Cell,?A,string,string}
* @psalm-return list{Cell, ?A, string, string}
*
* @return array Array containing:
* - Modified cell
Expand All @@ -122,7 +122,7 @@ public function prepareSortable(Cell $cell, string $property): array
return [$cell, null, '', ''];
}

$linkAttributes = [];
$linkAttributes = $this->sortableLinkAttributes;
$propertyOrder = $this->sort->getOrder()[$property] ?? null;
if ($propertyOrder === null) {
$cell = $cell->addClass($this->sortableHeaderClass);
Expand Down
16 changes: 14 additions & 2 deletions src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use Yiisoft\Translator\TranslatorInterface;
use Yiisoft\Validator\Result as ValidationResult;
use Yiisoft\Yii\DataView\Column\Base\Cell;
use Yiisoft\Yii\DataView\Column\Base\DataContext;
use Yiisoft\Yii\DataView\Column\Base\FilterContext;
use Yiisoft\Yii\DataView\Column\Base\GlobalContext;
use Yiisoft\Yii\DataView\Column\Base\DataContext;
use Yiisoft\Yii\DataView\Column\Base\MakeFilterContext;
use Yiisoft\Yii\DataView\Column\Base\RendererContainer;
use Yiisoft\Yii\DataView\Column\ColumnInterface;
Expand Down Expand Up @@ -207,6 +207,8 @@
*/
private array $filterErrorsContainerAttributes = [];

private array $filterFormAttributes = [];

/**
* @var RendererContainer Container for column renderers.
*/
Expand Down Expand Up @@ -289,6 +291,13 @@
return $new;
}

public function filterFormAttributes(array $attributes): self

Check warning on line 294 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L294

Added line #L294 was not covered by tests
{
$new = clone $this;
$new->filterFormAttributes = $attributes;
return $new;

Check warning on line 298 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L296-L298

Added lines #L296 - L298 were not covered by tests
}

/**
* Whether to keep the current page when sorting is changed.
*
Expand Down Expand Up @@ -820,7 +829,10 @@
if (!empty($sort) && $this->urlConfig->getSortParameterType() === UrlParameterType::QUERY) {
$content[] = Html::hiddenInput($this->urlConfig->getSortParameterName(), $sort);
}
$filtersForm = Html::form($url, 'GET', ['id' => $filterContext->formId, 'style' => 'display:none'])
$formAttributes = $this->filterFormAttributes;
$formAttributes['id'] = $filterContext->formId;
Html::addCssStyle($formAttributes, 'display:none');
$filtersForm = Html::form($url, 'GET', $formAttributes)
->content(...$content)
->render();
$filterRow = Html::tr()->cells(...$tags);
Expand Down
7 changes: 7 additions & 0 deletions src/Pagination/OffsetPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@
return $new;
}

public function addLinkAttributes(array $attributes): self

Check warning on line 142 in src/Pagination/OffsetPagination.php

View check run for this annotation

Codecov / codecov/patch

src/Pagination/OffsetPagination.php#L142

Added line #L142 was not covered by tests
{
$new = clone $this;
$new->linkAttributes = array_merge($new->linkAttributes, $attributes);
return $new;

Check warning on line 146 in src/Pagination/OffsetPagination.php

View check run for this annotation

Codecov / codecov/patch

src/Pagination/OffsetPagination.php#L144-L146

Added lines #L144 - L146 were not covered by tests
}

/**
* Set new link classes.
*
Expand Down
Loading