Skip to content

Commit

Permalink
Fix #183: Add classes to data column (#217)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergei Predvoditelev <[email protected]>
  • Loading branch information
samdark and vjik authored Sep 29, 2024
1 parent c26babd commit 490c1c9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Column/DataColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function __construct(
public readonly array|RuleInterface|null $filterValidation = null,
bool|callable|null $filterEmpty = null,
private readonly bool $visible = true,
public readonly ?string $columnClass = null,
public readonly ?string $headerClass = null,
public readonly ?string $bodyClass = null,
) {
$this->filterEmpty = $filterEmpty;
}
Expand Down
14 changes: 13 additions & 1 deletion src/Column/DataColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,21 @@ public function __construct(
public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
$this->checkColumn($column);
return $cell->addAttributes($column->columnAttributes);
/** @var DataColumn $column This annotation is for IDE only */

return $cell
->addAttributes($column->columnAttributes)
->addClass($column->columnClass);
}

public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): Cell
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

$cell = $cell
->addAttributes($column->headerAttributes)
->addClass($column->headerClass)
->encode(false);

if ($column->header === null) {
Expand All @@ -91,6 +97,7 @@ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext
public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext $context): ?Cell
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

if ($column->property === null || $column->filter === false) {
return null;
Expand Down Expand Up @@ -127,6 +134,8 @@ public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext
public function makeFilter(ColumnInterface $column, MakeFilterContext $context): ?FilterInterface
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

if ($column->property === null) {
return null;
}
Expand Down Expand Up @@ -199,13 +208,15 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con

return $cell
->addAttributes($attributes)
->addClass($column->bodyClass)
->content($content)
->encode(false);
}

public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

if ($column->footer !== null) {
$cell = $cell->content($column->footer);
Expand Down Expand Up @@ -263,6 +274,7 @@ private function checkColumn(ColumnInterface $column): void
public function getOverrideOrderFields(ColumnInterface $column): array
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

if ($column->property === null
|| $column->field === null
Expand Down
54 changes: 54 additions & 0 deletions tests/Column/DataColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Yii\DataView\Tests\Column;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Iterable\IterableDataReader;
use Yiisoft\Definitions\Exception\CircularReferenceException;
use Yiisoft\Definitions\Exception\InvalidConfigException;
use Yiisoft\Definitions\Exception\NotInstantiableException;
Expand Down Expand Up @@ -494,4 +495,57 @@ public function testValueClosure(): void
->render()
);
}

public function testColumnClasses(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div>
<table>
<colgroup>
<col class="columnClassAttr columnClass" custom="columnAttributes">
</colgroup>
<thead>
<tr>
<th class="headerClassAttr headerClass" custom="headerAttributes">Name</th>
</tr>
</thead>
<tbody>
<tr>
<td class="bodyClassAttr bodyClass" custom="bodyAttributes">John</td>
</tr>
<tr>
<td class="bodyClassAttr bodyClass" custom="bodyAttributes">Mary</td>
</tr>
</tbody>
</table>
<div>Page <b>1</b> of <b>1</b></div>
</div>
HTML,
GridView::widget()
->columns(
new DataColumn(
'name',
columnAttributes: [
'custom' => 'columnAttributes',
'class' => 'columnClassAttr',
],
headerAttributes: [
'custom' => 'headerAttributes',
'class' => 'headerClassAttr',
],
bodyAttributes: [
'custom' => 'bodyAttributes',
'class' => ['bodyClassAttr'],
],
columnClass: 'columnClass',
headerClass: 'headerClass',
bodyClass: 'bodyClass'
),
)
->columnGroupEnabled()
->dataReader(new IterableDataReader($this->data))
->render()
);
}
}

0 comments on commit 490c1c9

Please sign in to comment.