Skip to content

Commit

Permalink
add a way to autodetect column types
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrombez committed Oct 24, 2023
1 parent f66a2ad commit d1a01b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/Grid/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Column
{
public string $name;
public $value;
public string $template;
private ?string $template;
public array $templateParameters;
public ?string $sortable;
public $sortableQuery;
Expand All @@ -19,17 +19,30 @@ public function __construct(
string|callable $value = null,
string $template = null,
array $templateParameters = [],
string|array $sortable = null,
string|array $sortable = null,
callable|string|null $sortableQuery = null
) {
$this->name = $name;
$this->value = $value ?? fn($item) => $item;
$this->template = $template ?? Template::TEXT;
$this->template = $template;
$this->templateParameters = $templateParameters;
$this->sortable = $sortable;
$this->sortableQuery = $sortableQuery;
}

public function getTemplate(?object $entity = null): string
{
if ($this->template !== null) {
return $this->template;
}

if ($entity !== null && is_array($this->getValue($entity))) {
return Template::ARRAY;
}

return Template::TEXT;
}

public function getValue(object $entity)
{
if (is_callable($this->value)) {
Expand Down
6 changes: 4 additions & 2 deletions src/Resources/views/_column_value.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% include [grid.theme ~ '/column_type/' ~ column.template, '@KibaticDatagrid/column_type/' ~ column.template, column.template] with {
{% set template = column.template(item) %}

{% include [grid.theme ~ '/column_type/' ~ template, '@KibaticDatagrid/column_type/' ~ template, template] with {
'column': column,
'parameters': column.templateParameters,
'item': item,
'value': column.value(item)
} %}
} %}

0 comments on commit d1a01b3

Please sign in to comment.