feat(crud/index): allow to define row attributes

This commit is contained in:
Simon Vieille 2025-12-22 17:36:08 +01:00
commit 19a78b9359
Signed by: deblan
GPG key ID: 579388D585F70417
3 changed files with 28 additions and 1 deletions

View file

@ -11,6 +11,7 @@
* feat(builder): allow to add block between children
* feat(builder): improve UI to add new block
* feat(settings): allow to edit a setting in plain page
* feat(crud/index): allow to define row attributes
## [v1.26.0] - 2025-03-17
### Added

View file

@ -2,6 +2,8 @@
namespace App\Core\Crud;
use App\Core\Entity\EntityInterface;
/**
* class CrudConfiguration.
*
@ -28,6 +30,7 @@ class CrudConfiguration
protected string $sortableCollectionProperty = 'sortOrder';
protected ?string $defaultLocale = null;
protected array $showActions = [];
protected array $listRowAttributes = [];
protected static $self;
@ -247,6 +250,26 @@ class CrudConfiguration
return $this->viewDatas[$context][$name] ?? $defaultValue;
}
public function setListRowAttributes(string $context, array $attributes): self
{
$this->listRowAttributes[$context] = $attributes;
return $this;
}
public function getListRowAttributes(string $context, EntityInterface $entity): array
{
$attributes = $this->listRowAttributes[$context] ?? [];
foreach ($attributes as $key => $attribute) {
if (is_callable($attribute)) {
$attributes[$key] = $attribute($entity);
}
}
return $attributes;
}
// --
public function setField(string $context, string $label, string $field, array $options): self

View file

@ -193,7 +193,10 @@
{%- endif -%}
{%- endset -%}
<tr {{ dataSortableItem|raw }} {% if configuration.doubleClick(context) %}data-dblclick="{{- dbClick -}}"{% endif %} class="{{ loop.index is odd ? 'is-odd' : 'is-even' }}">
{% set rowAttributes = configuration.listRowAttributes(context, item) %}
{% set rowClasses = rowAttributes['class']|default('') %}
<tr {{ dataSortableItem|raw }} {% if configuration.doubleClick(context) %}data-dblclick="{{- dbClick -}}"{% endif %} class="{{ loop.index is odd ? 'is-odd' : 'is-even' }} {{ rowClasses }}" {% for attr, attrValue in rowAttributes %}{% if attr != 'class' %}{{ attr }}={{ attrValue }} {% endif %}{% endfor %}>
{% if configuration.hasBatchAction(context) %}
<td class="crud-batch-column">
<input type="checkbox" class="batch_form" name="batch[items][{{ loop.index }}]" form="form-batch" value="{{ loop.index }}">