Skip to content

Commit

Permalink
Add HTML attrs for empty cells in GridView (#209)
Browse files Browse the repository at this point in the history
* Add HTML attrs for empty cells in `GridView`

* fix

* improve

* improve

* improve
  • Loading branch information
vjik authored Sep 5, 2024
1 parent b35f810 commit 848da41
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
use Yiisoft\Yii\DataView\Column\OverrideOrderFieldsColumnInterface;
use Yiisoft\Yii\DataView\Filter\Factory\IncorrectValueException;

use function call_user_func;
use function is_callable;

/**
* The GridView widget displays data in a grid.
*
Expand Down Expand Up @@ -56,6 +59,7 @@ final class GridView extends BaseListView

private bool $columnGroupEnabled = false;
private string $emptyCell = ' ';
private array $emptyCellAttributes = [];
private bool $footerEnabled = false;
private array $footerRowAttributes = [];
private bool $headerTableEnabled = true;
Expand Down Expand Up @@ -207,14 +211,28 @@ public function columnGroupEnabled(bool $value = true): self
/**
* Return new instance with the HTML display when the content is empty.
*
* @param string $value The HTML display when the content of a cell is empty. This property is used to render cells
* that have no defined content, e.g., empty footer.
* @param string $content The HTML display when the content of a cell is empty. Defaults to ' '.
* @param array|null $attributes The HTML attributes for the empty cell.
*/
public function emptyCell(string $value): self
public function emptyCell(string $content, ?array $attributes = null): self
{
$new = clone $this;
$new->emptyCell = $value;
$new->emptyCell = $content;
if ($attributes !== null) {
$new->emptyCellAttributes = $attributes;
}
return $new;
}

/**
* Returns a new instance with the HTML attributes for the empty cell.
*
* @param array $attributes The HTML attributes for the empty cell.
*/
public function emptyCellAttributes(array $attributes): self
{
$new = clone $this;
$new->emptyCellAttributes = $attributes;
return $new;
}

Expand Down Expand Up @@ -612,7 +630,7 @@ protected function renderItems(array $items, ValidationResult $filterValidationR
$context = new DataContext($column, $value, $key, $index);
$cell = $renderers[$i]->renderBody($column, new Cell($this->bodyCellAttributes), $context);
$tags[] = $cell->isEmptyContent()
? Html::td()->content($this->emptyCell)->encode(false)
? Html::td($this->emptyCell, $this->emptyCellAttributes)->encode(false)
: Html::td(attributes: $this->prepareBodyAttributes($cell->getAttributes(), $context))
->content(...$cell->getContent())
->encode($cell->isEncode())
Expand Down

0 comments on commit 848da41

Please sign in to comment.