How to Add Dynamic Mass Action in Admin Grid in Magento 2
The Mass Actions component allows performing specific actions with multiple selected items. Admin can perform a mass operation on the selected items quickly. It saves the admin from the tedious process of completing an operation on a one-by-one record.
Having a grid in Magento 2 means loads of records maintained in that admin grid, and need often arrives to perform mass action on the various records. No individual or separate record operation is required if the admin grid has a mass action facility. In addition, the admin grid provides the flexibility to remove columns from admin grid dynamically from the table, allowing for a more tailored and efficient view of the data.
Magento 2 comes with a UI component that assists store admins in adding more items such as columns, filter argument, paging, mass action, etc., for the admin grid.
Earlier, I did post a solution to add custom mass action in order grid in Magento 2.
However, sometimes you may have to dynamically add mass action in the admin grid according to your requirements. For example you can dynamically add link to customer account navigation in Magento 2 allows you to customize and enhance the customer’s account dashboard by adding custom links or sections based on specific business needs and requirements.
For instance, the admin grid has a column named ‘status’ with three records- pending, confirmed, and unconfirmed. Now admin wants to change all the unconfirmed status with confirmed one and pending status with confirmed. Also, if a new status “cancelled” is created, the cancel status must be available as an option dynamically while using this custom mass action.
Or, if you are using the custom mass action to upgrade the customer group of your selected customers from the grid and you create a custom customer group, using the below solution to add dynamic mass action in admin grid in Magento 2, your custom group will be available as an option dynamically!
Method to Add Dynamic Mass Action in Admin Grid in Magento 2
- Use the below code in the data_listing.xml at Vendor/Extension/view/adminhtml/ui_component
123456789101112131415<massaction name="listing_massaction"><argument name="data" xsi:type="array"><item name="config" xsi:type="array"><item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item><item name="indexField" xsi:type="string">id</item></item></argument><action name="change_status"><settings><type>change_status</type><label translate="true">Change Status</label><actions class="Vendore\Extension\Ui\Component\MassAction\Status\Options"/></settings></action></massaction> - Use the below code in the di.xml file at Vendor/Extension/etc
123456789101112<type name="Vendore\Extension\Ui\Component\MassAction\Status\Options"><arguments><argument name="data" xsi:type="array"><item name="urlPath" xsi:type="string">extenssion/index/masschangestatus</item><item name="paramName" xsi:type="string">status</item><item name="confirm" xsi:type="array"><item name="title" xsi:type="string" translatable="true">Change Status</item><item name="message" xsi:type="string" translatable="true">Are you sure to change?</item></item></argument></arguments></type> - Paste the below code in the Options.php file at Vendor/Extension/UI/Component/MassAction/Status
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687<?phpnamespace Vendore\Extension\Ui\Component\MassAction\Status;use Magento\Framework\Phrase;use Magento\Framework\UrlInterface;class Options implements \JsonSerializable{protected $options;protected $data;protected $urlBuilder;protected $urlPath;protected $paramName;protected $additionalData = [];public function __construct(UrlInterface $urlBuilder,array $data = []){$this->data = $data;$this->urlBuilder = $urlBuilder;}public function jsonSerialize(){if ($this->options === null) {$options = [['value' => 'pending','label' => 'Pending approval'], ['value' => 'active','label' => 'Active'], ['value' => 'inactive','label' => 'Inactive']];$this->prepareData();foreach ($options as $optionCode) {$this->options[$optionCode['value']] = ['type' => 'change_status_' . $optionCode['value'],'label' => __($optionCode['label']),'__disableTmpl' => true];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;}protected function prepareData(){foreach ($this->data as $key => $value) {switch ($key) {case 'urlPath':$this->urlPath = $value;break;case 'paramName':$this->paramName = $value;break;case 'confirm':foreach ($value as $messageName => $message) {$this->additionalData[$key][$messageName] = (string)new Phrase($message);}break;default:$this->additionalData[$key] = $value;break;}}}} - Use the below code in MassChangeStatus.php file at Vendor/Extension/Controller/Adminhtml/Index
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667<?phpnamespace Vendore\Extension\Controller\Adminhtml\Index;use Magento\Backend\App\Action;use Magento\Backend\App\Action\Context;use Magento\Framework\Controller\ResultFactory;use Magento\Framework\View\Result\PageFactory;use Magento\Ui\Component\MassAction\Filter;use Vendore\Extension\Model\ExtensionModelFactory;use Vendore\Extension\Model\ResourceModel\ExtensionModel\CollectionFactory;;class MassChangeStatus extends Action{protected $filter;protected $resultPageFactory;protected $collectionFactory;protected $extensionModelFactory;private $scopeConfig;public function __construct(Context $context,PageFactory $resultPageFactory,Filter $filter,ScopeConfigInterface $scopeConfig,ExtensionModelFactory $extensionModelFactory,CollectionFactory $collectionFactory){parent::__construct($context, $resultPageFactory);$this->resultPageFactory = $resultPageFactory;$this->filter = $filter;$this->scopeConfig = $scopeConfig;$this->extensionModelFactory = $extensionModelFactory;$this->collectionFactory = $collectionFactory;}public function execute(){try {$collection = $this->filter->getCollection($this->collectionFactory->create());$updated = 0;foreach ($collection as $item) {$model = $this->extensionModelFactory->create()->load($item['id']);$model->setData('status', $this->getRequest()->getParam('status'));$model->save();$updated++;}if ($updated) {$this->messageManager->addSuccess(__('A total of %1 record(s) were updated.', $updated));}} catch (\Exception $e) {\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($e->getMessage());}$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);$resultRedirect->setUrl($this->_redirect->getRefererUrl());return $resultRedirect;}protected function _isAllowed(){return true;}}
After applying the above method, the mass action displays in the admin grid as shown below:
Quite lengthy but worth applying. Right?
If you have queries regarding this blog, feel free to ask in the Comments section below.
I would be happy to answer your question.
Do consider sharing this post with Magento Community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Get Product Quantity Information in Magento 2
How to Override Category View Page in Magento 2
Next