Skip to content

Commit

Permalink
add support for sub request
Browse files Browse the repository at this point in the history
  • Loading branch information
JCrombez committed Jul 24, 2024
1 parent 707e2f3 commit cd7a454
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/Grid/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Kibatic\DatagridBundle\Twig\HtmlExtension;
use Knp\Component\Pager\Pagination\PaginationInterface;
use Symfony\Component\HttpFoundation\Request;

class Grid
{
Expand All @@ -16,17 +17,20 @@ class Grid
private string $theme;
private $rowAttributesCallback = null;

private Request $request;
private PaginationInterface $pagination;

public function __construct(
array $columns,
Request $request,
PaginationInterface $pagination,
string $theme,
array $batchActions = [],
string $batchMethod = 'POST',
?callable $rowAttributesCallback = null,
) {
$this->columns = $columns;
$this->request = $request;
$this->pagination = $pagination;
$this->batchActions = $batchActions;
$this->batchMethod = $batchMethod;
Expand All @@ -39,6 +43,11 @@ public function getColumns(): array
return $this->columns;
}

public function getRequest(): Request
{
return $this->request;
}

public function getPagination(): PaginationInterface
{
return $this->pagination;
Expand Down
1 change: 1 addition & 0 deletions src/Grid/GridBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public function getGrid(bool $forceRecreate = false): Grid

$this->grid = new Grid(
$this->columns,
$this->request,
$pagination,
$this->theme,
$this->batchActions,
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/views/theme/bootstrap4/datagrid-table.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
<th {% if th_class is not empty %}class="{{ th_class }}"{% endif %}>
{% if column.sortable is not null %}
{% set sortUrl = path(
app.request.attributes.get('_route'),
app.request.attributes.get('_route_params')|merge(app.request.query.all)|merge({
grid.request.attributes.get('_route'),
grid.request.attributes.get('_route_params')|merge(grid.request.query.all)|merge({
'sort_by': column.sortable,
'sort_order': app.request.get('sort_order') == 'ASC' ? 'DESC' : 'ASC',
'sort_order': grid.request.get('sort_order') == 'ASC' ? 'DESC' : 'ASC',
'page': null
})
) %}
Expand Down Expand Up @@ -92,4 +92,4 @@
{% if grid.hasBatchActions %}</form>{% endif %}
{% else %}
<p class="alert alert-info">{{ 'No result'|trans({}, 'KibaticDatagridBundle') }}</p>
{% endif %}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
{% set cleared_url = '' %}
{% if form.vars.method == 'GET' %}
{% set cleared_url = path(
app.request.attributes.get('_route'),
app.request.attributes.get('_route_params')|merge(app.request.query.all|filter((v, k) => k != form.vars.name))
grid.request.attributes.get('_route'),
grid.request.attributes.get('_route_params')|merge(grid.request.query.all|filter((v, k) => k != form.vars.name))
) %}
{% endif %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
grid.pagination.route,
grid.pagination.params|merge({
'sort_by': column.sortable,
'sort_order': app.request.get('sort_order') == 'ASC' ? 'DESC' : 'ASC'
'sort_order': grid.request.get('sort_order') == 'ASC' ? 'DESC' : 'ASC'
})
) %}
<a href="{{ sortUrl }}" title="{{ 'Sort'|trans({}, 'KibaticDatagridBundle') }}">{{ column.name }}</a>
Expand Down

0 comments on commit cd7a454

Please sign in to comment.