Skip to content

Commit

Permalink
Add offset pagination support to serial column
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jan 24, 2025
1 parent 4def342 commit 577d82c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
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

0 comments on commit 577d82c

Please sign in to comment.