How to Add Mass Action In UI Component Grid In Magento 2

Hello Everyone,

In this blog, we will learn about how to Add Mass Action in Magento 2.

In our previous blog we created a UI Component Grid and UI Component Form and performed CRUD Operation.

Now We Add code for Mass Action in UI Component Grid.

Without wasting your time, let us guide you straight away. Follow the easy step given below to Add Mass Action in UI Component Grid in Magento 2.

STEPS FOR ADD MASS ACTION IN UI COMPONENT GRID IN MAGENTO 2

Step 1: Open ui_listing.xml file

app/code/Vendor/Extension/view/adminhtml/ui_component/ui_listing.xml
Add below code under <listingToolbar>...</listingToolbar> tag.
<massaction name="listing_massaction" component="Magento_Ui/js/grid/tree-massactions" class="\Magento\Catalog\Ui\Component\Product\MassAction">
                <action name="delete">
                    <settings>
                        <confirm>
                            <message translate="true">Delete  Items?</message>
                            <title translate="true">Delete items</title>
                        </confirm>
                        <url path="*/*/massDelete" />
                        <type>delete</type>
                        <label translate="true">Delete</label>
                    </settings>
                </action>
            </massaction>

Step 2: Create Mass Delete.php file.

app/code/Vendor/Extension/Controller/Adminhtml/Index/MassDelete.php

<?php

namespace Vendor\Extension\Controller\Adminhtml\Index;

use Vendor\Extension\Model\ResourceModel\BookInfo\CollectionFactory;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;
use Magento\Ui\Component\MassAction\Filter;

class MassDelete extends Action
{
    protected $_coreRegistry = null;
    protected $resultPageFactory;
    protected $BookInfoFactory;
    protected $filter;
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        Registry $registry,
        Filter $filter,
        CollectionFactory $BookInfo
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->_coreRegistry = $registry;
        $this->BookInfoFactory = $BookInfo;
        $this->filter = $filter;
        parent::__construct($context);
    }
    public function execute()
    {
        $collection = $this->filter->getCollection($this->BookInfoFactory->create());

        $count = 0;
        foreach ($collection as $child) {
            $child->delete();
            $count++;
        }

        $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $count));
        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect->setPath('*/*/index');
    }
}

Step 3: Finally run the below commands

$ php bin/magento cache:clean
$ php bin/magento cache:flush

Step 4: Output:

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Add Mass Action in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

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