From 819415d8c1d5df2710d4ff9545dde0981f462e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20CROMBEZ?= <777666+jcrombez@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:23:53 +0000 Subject: [PATCH] batch checkbox using stimulus (bt5 only) --- assets/dist/checker_controller.js | 17 ++++++++++ assets/package.json | 27 ++++++++++++++++ .../KibaticDatagridExtension.php | 32 ++++++++++++++++++- .../theme/bootstrap5/datagrid-table.html.twig | 20 ++---------- 4 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 assets/dist/checker_controller.js create mode 100644 assets/package.json diff --git a/assets/dist/checker_controller.js b/assets/dist/checker_controller.js new file mode 100644 index 0000000..a5be21a --- /dev/null +++ b/assets/dist/checker_controller.js @@ -0,0 +1,17 @@ +import { Controller } from '@hotwired/stimulus' + +export default class extends Controller { + static targets = ["master", "checkbox"] + + masterTargetConnected(element) { + element.addEventListener("click", this.toggleAll.bind(this)) + } + + toggleAll() { + console.log(this.checkboxTargets) + + for (let checkbox of this.checkboxTargets) { + checkbox.checked = this.masterTarget.checked + } + } +} diff --git a/assets/package.json b/assets/package.json new file mode 100644 index 0000000..20a1788 --- /dev/null +++ b/assets/package.json @@ -0,0 +1,27 @@ +{ + "name": "@kibatic/datagrid-bundle", + "description": "", + "license": "MIT", + "version": "0.0.1", + "main": "dist/checker_controller.js", + "symfony": { + "controllers": { + "checker": { + "main": "dist/checker_controller.js", + "name": "checker", + "webpackMode": "eager", + "fetch": "eager", + "enabled": true + } + }, + "importmap": { + "@hotwired/stimulus": "^3.0.0" + } + }, + "peerDependencies": { + "@hotwired/stimulus": "^3.0.0" + }, + "devDependencies": { + "@hotwired/stimulus": "^3.0.0" + } +} diff --git a/src/DependencyInjection/KibaticDatagridExtension.php b/src/DependencyInjection/KibaticDatagridExtension.php index 4916010..2b6350e 100644 --- a/src/DependencyInjection/KibaticDatagridExtension.php +++ b/src/DependencyInjection/KibaticDatagridExtension.php @@ -1,14 +1,16 @@ load('services.yaml'); } + + public function prepend(ContainerBuilder $container): void + { + if ($this->isAssetMapperAvailable($container)) { + $container->prependExtensionConfig('framework', [ + 'asset_mapper' => [ + 'paths' => [ + __DIR__.'/../../assets/dist' => '@kibatic/datagrid-bundle', + ], + ], + ]); + } + } + + private function isAssetMapperAvailable(ContainerBuilder $container): bool + { + if (!interface_exists(AssetMapperInterface::class)) { + return false; + } + + // check that FrameworkBundle 6.3 or higher is installed + $bundlesMetadata = $container->getParameter('kernel.bundles_metadata'); + if (!isset($bundlesMetadata['FrameworkBundle'])) { + return false; + } + + return is_file($bundlesMetadata['FrameworkBundle']['path'].'/Resources/config/asset_mapper.php'); + } } diff --git a/src/Resources/views/theme/bootstrap5/datagrid-table.html.twig b/src/Resources/views/theme/bootstrap5/datagrid-table.html.twig index 66c030e..6ea0af5 100644 --- a/src/Resources/views/theme/bootstrap5/datagrid-table.html.twig +++ b/src/Resources/views/theme/bootstrap5/datagrid-table.html.twig @@ -6,26 +6,12 @@ {% endif %} - +
{% if grid.hasBatchActions %} {% endif %} {% for column in grid.columns %} @@ -57,7 +43,7 @@ {% if grid.hasBatchActions %} {% endif %} {% for column in grid.columns %}
- - + - +