Magento 2: Add Mass Action in Admin Grid

In this blog, the following steps illustrate you how to attach Mass action in Admin Grid in Magento 2:

Step 1: Adminhtml layout file

Step 2: Add a MassAction tag in component xml file

Step 3: Form options for selection

Step 4: Controller file

UI component to utilize Magento 2 to include more items in the admin grid: column, filter argument, paging … and mass action.

Step 1: Adminhtml layout file

Adminhtml Layout:

<body>

   <referenceContainer name="content">

       <uiComponent name="rewardpoints_earningrates_listing"/>

   </referenceContainer>

</body>

Step 2: Include a MassAction tag in component xml file

Include a massAction tag in UI component xml file:

app/code/Webnexs/Rewardpoints/view/adminhtml/ui_component/rewardpoints_earningrates_listing.xml

<massaction name="listing_massaction">

   <action name="change_status">

       <argument name="data" xsi:type="array">

           <item name="config" xsi:type="array">

               <item name="type" xsi:type="string">change_status</item>

               <item name="label" xsi:type="string" translate="true">Change status</item>

           </item>

       </argument>

       <argument name="actions" xsi:type="configurableObject">

           <argument name="class" xsi:type="string">Webnexs\Rewardpoints\Ui\Component\MassAction\Status\Options</argument>

           <argument name="data" xsi:type="array">

               <item name="urlPath" xsi:type="string">rewardpoints/earningrates/massStatus</item>

               <item name="paramName" xsi:type="string">status</item>

           </argument>

       </argument>

   </action>

</massaction>

<columns>

 <column name="status">

    <argument name="data" xsi:type="array">

       <item name="options" xsi:type="object">Webnexs\Rewardpoints\Ui\Component\Listing\Column\Status</item>

       <item name="config" xsi:type="array">

           <item name="editor" xsi:type="string">select</item>

           <item name="filter" xsi:type="string">select</item>

           <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>

           <item name="dataType" xsi:type="string">select</item>

           <item name="label" xsi:type="string" translate="true">Status</item>

       </item>

    </argument>

  </column>

</columns>

Step 3: Form options for selection

Form option for selection:

app/code/Webnexs/Rewardpoints/Ui/Component/MassAction/Status/Options.php

namespace Webnexs\Rewardpoints\Ui\Component\MassAction\Status;

use Magento\Framework\UrlInterface;

use Zend\Stdlib\JsonSerializable;



/**

* Class Options

*/

class Options implements JsonSerializable

{

   /**

    * @var array

    */

   protected $options;



   /**

    * Additional options params

    *

    * @var array

    */

   protected $data;



   /**

    * @var UrlInterface

    */

   protected $urlBuilder;



   /**

    * Base URL for subactions

    *

    * @var string

    */

   protected $urlPath;



   /**

    * Param name for subactions

    *

    * @var string

    */

   protected $paramName;



   /**

    * Additional params for subactions

    *

    * @var array

    */

   protected $additionalData = [];



   /**

    * Constructor

    *

    * @param CollectionFactory $collectionFactory

    * @param UrlInterface $urlBuilder

    * @param array $data

    */

   public function __construct(

       UrlInterface $urlBuilder,

       array $data = []

   ) {

       $this->data = $data;

       $this->urlBuilder = $urlBuilder;

   }



   /**

    * Get action options

    *

    * @return array

    */

   public function jsonSerialize()

   {

       if ($this->options === null) {

           $options = array(

               array(

                   "value" => "1",

                   "label" => ('Active'),

               ),

               array(

                   "value" => "2",

                   "label" => ('Inactive'),

               )

           );

           $this->prepareData();

           foreach ($options as $optionCode) {

               $this->options[$optionCode['value']] = [

                   'type' => 'status_' . $optionCode['value'],

                   'label' => $optionCode['label'],

               ];



               if ($this->urlPath && $this->paramName) {

                   $this->options[$optionCode['value']]['url'] = $this->urlBuilder->getUrl(

                       $this->urlPath,

                       [$this->paramName => $optionCode['value']]

                   );

               }



               $this->options[$optionCode['value']] = array_merge_recursive(

                   $this->options[$optionCode['value']],

                   $this->additionalData

               );

           }

           $this->options = array_values($this->options);

       }

       return $this->options;

   }



   /**

    * Prepare addition data for subactions

    *

    * @return void

    */

   protected function prepareData()

   {

       foreach ($this->data as $key => $value) {

           switch ($key) {

               case 'urlPath':

                   $this->urlPath = $value;

                   break;

               case 'paramName':

                   $this->paramName = $value;

                   break;

               default:

                   $this->additionalData[$key] = $value;

                   break;

           }

       }

   }

}

Step 4: Controller file

Controller file:

app/code/Webnexs/Rewardpoints/Controller/Adminhtml/Earningrates/MassStatus.php

namespace Webnexs\Rewardpoints\Controller\Adminhtml\Earningrates;

use Magento\Backend\App\Action\Context;

use Webnexs\Rewardpoints\Model\ResourceModel\Rate\CollectionFactory;

use Magento\Ui\Component\MassAction\Filter;

use Magento\Framework\Controller\ResultFactory;

use Webnexs\Rewardpoints\Model\ResourceModel\Rate\Collection;



/**

* Class MassDelete

*/

class MassStatus extends AbstractMassAction

{

   /**

    * @param Context $context

    * @param Filter $filter

    * @param CollectionFactory $collectionFactory

    */

   public function __construct(

       Context $context,

       Filter $filter,

       CollectionFactory $collectionFactory

   ) {

       parent::__construct($context, $filter, $collectionFactory);

   }



   /**

    * @param AbstractCollection $collection

    * @return \Magento\Backend\Model\View\Result\Redirect

    */

   protected function massAction(Collection $collection)

   {

       $rateChangeStatus = 0;

       foreach ($collection as $rate) {

           $rate->setStatus($this->getRequest()->getParam('status'))->save();

           $rateChangeStatus++;

       }



       if ($rateChangeStatus) {

           $this->messageManager->addSuccess(__('A total of %1 record(s) were updated.', $rateChangeStatus));

       }

       /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */

       $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

       $resultRedirect->setPath($this->getComponentRefererUrl());



       return $resultRedirect;

   }



   /**

    * @return bool

    */

   protected function _isAllowed()

   {

       return $this->_authorization->isAllowed('Webnexs_Rewardpoints::Earning_Rates');

   }



}

Last Update: December 21, 2018  

March 16, 2018   275   Nandini R    Operations    
Total 1 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn