Skip to content
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

Fix #183: Add classes to data column #217

Merged
merged 5 commits into from
Sep 29, 2024
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
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)
samdark marked this conversation as resolved.
Show resolved Hide resolved
->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)
samdark marked this conversation as resolved.
Show resolved Hide resolved
->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()
);
}
}
Loading