Checkboxes and Bookmarks
I think you saw the “Bookmarks” button (With the eye icon and the text “Default view”)
You can choose, filters, sort and columns to display, and save the current view with this button.
At this moment, we have got the button, but the view creation does not work.
So we will modify the XML.
We will also add checkboxes on every lines of our grid.
You can add this XML just before the first “column” node :
<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>
You will have a XML like this :
<?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> </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> </columns> </listing>
Now you can change the columns to display, and save the view with the name you want.
With the checkboxes, we will add mass actions on the next lessons. For example “Delete all selected lines”.
Actions Columns
Our grid is very beautiful, but we can do nothing with it, so we will add 2 actions for a line : Edit / Delete
Add the XML after the last “column” node :
<!-- 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>
Here is the full XML :
<?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> </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>
We declare a new object Maxime\Jobs\Ui\Component\Listing\Column\DepartmentActions, so let’s create it :
app/code/Maxime/Jobs/Ui/Component/Listing/Column/DepartmentActions.php
With the following content :
<?php /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Maxime\Jobs\Ui\Component\Listing\Column; use Magento\Framework\View\Element\UiComponent\ContextInterface; use Magento\Framework\View\Element\UiComponentFactory; use Magento\Ui\Component\Listing\Columns\Column; use Magento\Framework\UrlInterface; /** * Class DepartmentActions */ class DepartmentActions extends Column { /** * @var UrlInterface */ protected $urlBuilder; /** * @param ContextInterface $context * @param UiComponentFactory $uiComponentFactory * @param UrlInterface $urlBuilder * @param array $components * @param array $data */ public function __construct( ContextInterface $context, UiComponentFactory $uiComponentFactory, UrlInterface $urlBuilder, array $components = [], array $data = [] ) { $this->urlBuilder = $urlBuilder; parent::__construct($context, $uiComponentFactory, $components, $data); } /** * Prepare Data Source * * @param array $dataSource * @return array */ public function prepareDataSource(array $dataSource) { if (isset($dataSource['data']['items'])) { foreach ($dataSource['data']['items'] as &$item) { $item[$this->getData('name')]['edit'] = [ 'href' => $this->urlBuilder->getUrl( 'jobs/department/edit', ['id' => $item['entity_id']] ), 'label' => __('Edit'), 'hidden' => false, ]; $item[$this->getData('name')]['delete'] = [ 'href' => $this->urlBuilder->getUrl( 'jobs/department/delete', ['id' => $item['entity_id']] ), 'label' => __('Delete'), 'hidden' => false, ]; } } return $dataSource; } }
The most important part is “prepareDataSource”. We define 2 actions : Edit and Delete, with the url on click (and the ID parameter)
You will see something like this :
If we have only one action, we will have a single link instead of a dropdown :
We can do a lot of things with grids ! Next part : filters !
If you have problem with columns sort order after adding new column, try this:
TRUNCATEui_bookmark; in scope of your MYSQL db.
Thank you, it helped me