How To Add Mass Action in Admin Grid In Magento 2?

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

Step 1: Adminhtml layout file

Step 2: Add a MassAction tag in the component XML file

Step 3: Form options for selection

Step 4: Controller file

Build your Amazon-like multi-vendor marketplace website @ $249/- now!!!
            Get your free live demo now: Click here

How To Add Mass Action in Admin Grid In Magento 2?

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

Step 1: Html layout file from the Admin grid

Adminhtml Layout:

<body>

   <referenceContainer name="content">

       <uiComponent name="rewardpoints_earningrates_listing"/>

   </referenceContainer>

</body>

Step 2: Include a Mass Action tag in the Magento 2 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 With Mass Action

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');

   }



}

Contact us to develop a highly scaling eCommerce store with Magento platform today!!!

How To Add Mass Action in Admin Grid In Magento 2 ?


Posted

in

,

by

Comments

Leave a Reply

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