Ajout des actions de masse (massActions) dans un grid
Nous allons ajouter les actions de masses à notre liste. Ce sont des actions que l’on peut exécuter après avoir coché les éléments qui nous intéressent dans la grid.
Ce code est à insérer dans le fichier
app/code/Maxime/Jobs/view/adminhtml/ui_component/jobs_department_listing.xml
On le met après la fermeture de la balise « filterSearch » :
<!-- Mass action --> <massaction name="listing_massaction"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="selectProvider" xsi:type="string">jobs_department_listing.jobs_department_listing.jobs_department_columns.ids</item> <item name="indexField" xsi:type="string">entity_id</item> </item> </argument> <action name="delete"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="type" xsi:type="string">delete</item> <item name="label" xsi:type="string" translate="true">Delete</item> <item name="url" xsi:type="url" path="jobs/department/massDelete"/> <item name="confirm" xsi:type="array"> <item name="title" xsi:type="string" translate="true">Delete items</item> <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item> </item> </item> </argument> </action> </massaction>
Nous déclarons dedans le type d’action, l’url de l’action, ainsi que les libellés qui seront affichés.
Voyons maintenant comment mettre en place la pagination, et ensuite je vous fournirai le XML complet.
Pagination
Cette pagination est à mettre en place dans le même fichier, juste après la fermeture de notre balise précécente, la balise « massAction » :
<!-- Paging --> <paging name="listing_paging"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="storageConfig" xsi:type="array"> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing.listing_top.bookmarks</item> <item name="namespace" xsi:type="string">current.paging</item> </item> <item name="selectProvider" xsi:type="string">jobs_department_listing.jobs_department_listing.jobs_department_columns.ids</item> <item name="displayArea" xsi:type="string">bottom</item> </item> </argument> </paging>
Il est également possible de mettre une valeur au choix du nombre d’éléments par page.
Voici le XML final pour les plus pressés :
<?xml version="1.0" encoding="UTF-8"?> <!-- /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ --> <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <!-- Integration --> <argument name="data" xsi:type="array"> <item name="js_config" xsi:type="array"> <!-- we define a provider --> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing_data_source</item> <!-- same string as above --> <item name="deps" xsi:type="string">jobs_department_listing.jobs_department_listing_data_source</item> </item> <!-- define column type --> <item name="spinner" xsi:type="string">jobs_department_columns</item> <!-- Button to add new item --> <item name="buttons" xsi:type="array"> <item name="add" xsi:type="array"> <item name="name" xsi:type="string">add</item> <item name="label" xsi:type="string" translate="true">Add New Department</item> <item name="class" xsi:type="string">primary</item> <item name="url" xsi:type="string">*/*/new</item> <!-- magento will translate it by jobs/department/new --> </item> </item> </argument> <!-- Data source --> <dataSource name="jobs_department_listing_data_source"> <argument name="dataProvider" xsi:type="configurableObject"> <argument name="class" xsi:type="string">DepartmentGridDataProvider</argument> <!-- Data provider class --> <argument name="name" xsi:type="string">jobs_department_listing_data_source</argument> <!-- provider defined above --> <argument name="primaryFieldName" xsi:type="string">entity_id</argument> <!-- Primary key --> <argument name="requestFieldName" xsi:type="string">id</argument> <!-- URL name parameter --> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item> <item name="update_url" xsi:type="url" path="mui/index/render"/> <item name="storageConfig" xsi:type="array"> <!-- Primary key column name --> <item name="indexField" xsi:type="string">entity_id</item> </item> </item> </argument> </argument> </dataSource> <!-- Container Listing Top --> <container name="listing_top"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="template" xsi:type="string">ui/grid/toolbar</item> </item> </argument> <!-- Button to manage views --> <bookmark name="bookmarks"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="component" xsi:type="string">Magento_Ui/js/grid/controls/bookmarks/bookmarks</item> <item name="displayArea" xsi:type="string">dataGridActions</item> <item name="storageConfig" xsi:type="array"> <item name="saveUrl" xsi:type="url" path="mui/bookmark/save"/> <item name="deleteUrl" xsi:type="url" path="mui/bookmark/delete"/> <item name="namespace" xsi:type="string">jobs_department_listing</item> </item> </item> </argument> </bookmark> <!-- Button to manage columns --> <container name="columns_controls"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="columnsData" xsi:type="array"> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing.jobs_department_columns</item> </item> <item name="component" xsi:type="string">Magento_Ui/js/grid/controls/columns</item> <item name="displayArea" xsi:type="string">dataGridActions</item> </item> </argument> </container> <!-- Filters --> <filters name="listing_filters"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="storageConfig" xsi:type="array"> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing.listing_top.bookmarks</item> <item name="namespace" xsi:type="string">current.filters</item> </item> <item name="childDefaults" xsi:type="array"> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing.listing_top.listing_filters</item> <item name="imports" xsi:type="array"> <item name="visible" xsi:type="string">jobs_department_listing.jobs_department_listing.listing_top.bookmarks:current.columns.${ $.index }.visible</item> </item> </item> </item> </argument> <!-- Department ID Filter --> <filterRange name="department_id"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="dataScope" xsi:type="string">entity_id</item> <!-- Column name in DB --> <item name="label" xsi:type="string" translate="true">ID</item> <!-- Label on grid --> <item name="childDefaults" xsi:type="array"> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing.listing_top.listing_filters</item> </item> </item> </argument> <filterInput name="from"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="dataScope" xsi:type="string">from</item> <item name="label" xsi:type="string" translate="true">from</item> <item name="placeholder" xsi:type="string" translate="true">From</item> </item> </argument> </filterInput> <filterInput name="to"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="dataScope" xsi:type="string">to</item> <item name="label" xsi:type="string" translate="true">to</item> <item name="placeholder" xsi:type="string" translate="true">To</item> </item> </argument> </filterInput> </filterRange> <!-- Department name Filter --> <filterInput name="department_name"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="dataScope" xsi:type="string">name</item> <!-- Column name in DB --> <item name="label" xsi:type="string" translate="true">Name</item> <!-- Label on grid --> </item> </argument> </filterInput> </filters> <!-- Filter Search --> <filterSearch name="fulltext"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing_data_source</item> <item name="chipsProvider" xsi:type="string">jobs_department_listing.jobs_department_listing.listing_top.listing_filters_chips</item> <item name="storageConfig" xsi:type="array"> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing.listing_top.bookmarks</item> <item name="namespace" xsi:type="string">current.search</item> </item> </item> </argument> </filterSearch> <!-- Mass action --> <massaction name="listing_massaction"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="selectProvider" xsi:type="string">jobs_department_listing.jobs_department_listing.jobs_department_columns.ids</item> <item name="indexField" xsi:type="string">entity_id</item> </item> </argument> <action name="delete"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="type" xsi:type="string">delete</item> <item name="label" xsi:type="string" translate="true">Delete</item> <item name="url" xsi:type="url" path="jobs/department/massDelete"/> <item name="confirm" xsi:type="array"> <item name="title" xsi:type="string" translate="true">Delete items</item> <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item> </item> </item> </argument> </action> </massaction> <!-- Paging --> <paging name="listing_paging"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="storageConfig" xsi:type="array"> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing.listing_top.bookmarks</item> <item name="namespace" xsi:type="string">current.paging</item> </item> <item name="selectProvider" xsi:type="string">jobs_department_listing.jobs_department_listing.jobs_department_columns.ids</item> <item name="displayArea" xsi:type="string">bottom</item> </item> </argument> </paging> </container> <!-- Columns --> <columns name="jobs_department_columns"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <!-- Bookmarks behaviour --> <item name="storageConfig" xsi:type="array"> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing.listing_top.bookmarks</item> <item name="namespace" xsi:type="string">current</item> </item> <item name="childDefaults" xsi:type="array"> <item name="controlVisibility" xsi:type="boolean">true</item> <!-- Bookmarks behaviour --> <item name="storageConfig" xsi:type="array"> <item name="provider" xsi:type="string">jobs_department_listing.jobs_department_listing.listing_top.bookmarks</item> <item name="root" xsi:type="string">columns.${ $.index }</item> <item name="namespace" xsi:type="string">current.${ $.storageConfig.root}</item> </item> </item> </item> </argument> <!-- Add columns with checkboxes --> <selectionsColumn name="ids"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="resizeEnabled" xsi:type="boolean">false</item> <item name="resizeDefaultWidth" xsi:type="string">55</item> <item name="indexField" xsi:type="string">entity_id</item> </item> </argument> </selectionsColumn> <!-- ID Column --> <column name="entity_id"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="filter" xsi:type="string">textRange</item> <item name="sorting" xsi:type="string">asc</item> <item name="label" xsi:type="string" translate="true">ID</item> </item> </argument> </column> <!-- Name Column --> <column name="name"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="filter" xsi:type="string">text</item> <item name="editor" xsi:type="array"> <item name="editorType" xsi:type="string">text</item> <item name="validation" xsi:type="array"> <item name="required-entry" xsi:type="boolean">true</item> </item> </item> <item name="label" xsi:type="string" translate="true">Name</item> </item> </argument> </column> <!-- Action columns --> <actionsColumn name="actions" class="Maxime\Jobs\Ui\Component\Listing\Column\DepartmentActions"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="resizeEnabled" xsi:type="boolean">false</item> <item name="resizeDefaultWidth" xsi:type="string">107</item> <item name="indexField" xsi:type="string">entity_id</item> </item> </argument> </actionsColumn> </columns> </listing>
Notre partie sur les grid de l’admin est désormais terminée.
Nous avons défini tout un tas d’actions, que nous allons désormais mettre en place grâce aux controllers !
Notez que nous avons fait uniquement la partie « departments », nous allons continuer ainsi.
Une fois celle-ci terminée complètement, un article « Travaux pratiques » vous permettra d’ajouter par vous même la gestion des « jobs » avec un correctif complet 😉
L’ajout des éléments mass action et paging dans le layout n’affichait plus les éléments.
Après un coup d’oeil dans la console, des librairies JS étaient manquantes. Il a donc fallu refaire un magento setup:static:deploy