Skip to content

Add offset pagination support to serial column #240

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 1 commit into from
Jan 24, 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
7 changes: 7 additions & 0 deletions src/Column/Base/DataContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@

namespace Yiisoft\Yii\DataView\Column\Base;

use Yiisoft\Data\Reader\ReadableDataInterface;
use Yiisoft\Yii\DataView\Column\ColumnInterface;

final class DataContext
{
/**
* @param ReadableDataInterface|null $preparedDataReader The prepared data reader with applied filters, paginating
* and sorting. `null` means that there is no data.
* @param int $index 0-based index of the data item among the items currently rendered.
*/
public function __construct(
public readonly ?ReadableDataInterface $preparedDataReader,
public readonly ColumnInterface $column,
public readonly array|object $data,
public readonly int|string $key,
Expand Down
7 changes: 6 additions & 1 deletion src/Column/SerialColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\DataView\Column;

use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Yii\DataView\Column\Base\Cell;
use Yiisoft\Yii\DataView\Column\Base\DataContext;
use Yiisoft\Yii\DataView\Column\Base\GlobalContext;
Expand All @@ -26,9 +27,13 @@ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext

public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell
{
$index = $context->preparedDataReader instanceof OffsetPaginator
? $context->preparedDataReader->getOffset() + $context->index + 1
: $context->index + 1;

return $cell
->addAttributes($column->bodyAttributes)
->content((string)($context->index + 1));
->content((string) $index);
}

public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
Expand Down
2 changes: 1 addition & 1 deletion src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ protected function renderItems(

$tags = [];
foreach ($columns as $i => $column) {
$context = new DataContext($column, $value, $key, $index);
$context = new DataContext($preparedDataReader, $column, $value, $key, $index);
$cell = $renderers[$i]->renderBody($column, new Cell($this->bodyCellAttributes), $context);
$tags[] = $cell->isEmptyContent()
? Html::td($this->emptyCell, $this->emptyCellAttributes)->encode(false)
Expand Down
Loading