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 2 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,14 +58,20 @@ 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
->addClass($column->columnClass)
->addAttributes($column->columnAttributes);
samdark marked this conversation as resolved.
Show resolved Hide resolved
}

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

$cell = $cell
->addClass($column->headerClass)
->addAttributes($column->headerAttributes)
samdark marked this conversation as resolved.
Show resolved Hide resolved
->encode(false);

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 @@ -198,6 +207,7 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con
}

return $cell
->addClass($column->bodyClass)
->addAttributes($attributes)
samdark marked this conversation as resolved.
Show resolved Hide resolved
->content($content)
->encode(false);
Expand All @@ -206,6 +216,7 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con
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
38 changes: 38 additions & 0 deletions tests/Column/DataColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,42 @@ public function testValueClosure(): void
->render()
);
}

public function testColumnClasses(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="w1-grid">
<table>
<thead>
<tr>
<th class="headerClass">Name</th>
</tr>
</thead>
<tbody>
<tr>
<td class="bodyClass">John</td>
</tr>
<tr>
<td class="bodyClass">Mary</td>
</tr>
</tbody>
</table>
<div>Page <b>1</b> of <b>1</b></div>
</div>
HTML,
GridView::widget()
->columns(
new DataColumn(
'name',
columnClass: 'columnClass',
headerClass: 'headerClass',
bodyClass: 'bodyClass'
),
)
->id('w1-grid')
->dataReader($this->createOffsetPaginator($this->data, 10))
->render()
);
}
}
Loading