Skip to content

Commit

Permalink
support batch actions method GET
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrombez committed Dec 8, 2023
1 parent 0bdd312 commit fc744ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/Grid/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Grid
*/
private array $columns;
private array $batchActions;
private string $batchMethod;
private string $theme;
private $rowAttributesCallback = null;

Expand All @@ -22,11 +23,13 @@ public function __construct(
PaginationInterface $pagination,
string $theme,
array $batchActions = [],
string $batchMethod = 'POST',
?callable $rowAttributesCallback = null,
) {
$this->columns = $columns;
$this->pagination = $pagination;
$this->batchActions = $batchActions;
$this->batchMethod = $batchMethod;
$this->theme = $theme;
$this->rowAttributesCallback = $rowAttributesCallback;
}
Expand All @@ -51,6 +54,16 @@ public function hasBatchActions(): bool
return !empty($this->batchActions);
}

public function getBatchMethod(): string
{
return $this->batchMethod;
}

public function getBatchActionsTokenId(): string
{
return json_encode($this->getBatchActions());
}

public function getTheme(): string
{
return $this->theme;
Expand Down
9 changes: 9 additions & 0 deletions src/Grid/GridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class GridBuilder
*/
private array $filters = [];
private array $batchActions = [];
private ?string $batchMethod = 'POST';
private ?string $theme = '@KibaticDatagrid/theme/bootstrap5';
private ?string $explicitRouteName = null;
private array $explicitRouteParams = [];
Expand Down Expand Up @@ -191,6 +192,13 @@ public function addBatchAction(string $id, string $label, string $url): self
return $this;
}

public function setBatchMethod(string $method): self
{
$this->batchMethod = $method;

return $this;
}

public function setItemsPerPage(?int $itemsPerPage): self
{
$this->itemsPerPage = $itemsPerPage;
Expand Down Expand Up @@ -238,6 +246,7 @@ public function getGrid(): Grid
$pagination,
$this->theme,
$this->batchActions,
$this->batchMethod,
$this->rowAttributesCallback
);
}
Expand Down
24 changes: 14 additions & 10 deletions src/Resources/views/theme/bootstrap5/datagrid-table.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

{% if grid.pagination.totalItemCount > 0 %}
{% if grid.hasBatchActions %}
<form name="datagrid-batch-action" method="POST" action="{{ grid.batchActions[0].url }}">
<form name="datagrid-batch-action" method="{{ grid.batchMethod }}" action="{{ grid.batchActions[0].url }}">
<input type="hidden" name="_token" value="{{ csrf_token(grid.batchActionsTokenId) }}">
{% endif %}

<table class="{% block grid_table_class %}table table-bordered table-striped table-hover{% endblock %}">
Expand All @@ -12,6 +13,7 @@
<th>
<input type="checkbox" data-check-all title="{{ 'Select all'|trans({}, 'KibaticDatagridBundle') }}" />
<script>
{# TODO: convertir en controller stimulus pour que ça soit compatible avec Turbo Drive #}
document.addEventListener("DOMContentLoaded", () => {
const batchForm = document.querySelector('form[name="datagrid-batch-action"]')
const checkboxes = batchForm.querySelectorAll('input[type="checkbox"]')
Expand Down Expand Up @@ -71,15 +73,17 @@
<div class="clearfix">
{% if grid.hasBatchActions %}
<div class="float-start">
{{ 'Batch action'|trans({}, 'KibaticDatagridBundle') }}

<select name="batch_action" onchange="this.closest('form').action = this.value" style="width: auto; height: auto">
{% for batchAction in grid.batchActions %}
<option value="{{ batchAction.url }}">{{ batchAction.label }}</option>
{% endfor %}
</select>

<input type="submit" class="btn btn-small btn-primary" value="OK">
<div class="input-group">
<label class="input-group-text" for="batch-select">
{{ 'Batch action'|trans({}, 'KibaticDatagridBundle') }}
</label>
<select onchange="this.closest('form').action = this.value" class="form-select" id="batch-select">
{% for batchAction in grid.batchActions %}
<option value="{{ batchAction.url }}">{{ batchAction.label }}</option>
{% endfor %}
</select>
<input type="submit" class="btn btn-small btn-primary" value="OK">
</div>
</div>
{% endif %}

Expand Down

0 comments on commit fc744ce

Please sign in to comment.