Redeem Now
Read More
Read now
Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    Magento Development 15: MassAction

    The MassAction allows us to perform an operation on multiple items of the grid. You may have seen mass actions on the product grid, which allows us to delete multiple product at once.

    In this blog we will create some mass actions. First we will create a MassAction to delete multiple blogs at once. We need to edit the ui component file view/adminhtml/ui_component/blogmanager_blog_listing.xml

    Here we have added selectionsColumn tag inside the columns tag. It will show the checkbox column to select the rows.

    And inside the listingToolbar tag we have added massaction tag which will be used to manage the mass-action dropdown. To add the mass-action action, we have used the action tag, where we have specified label, url for the mass-action, confirmation message, etc.

    Now let’s create the action file for the mass-delete url, Controller/Adminhtml/Manage/MassDelete.php

    Let’s see what’s this line is doing,
    With $this->collectionFactory->create(), we are getting the whole blogs collection. And we are passing it in getCollection method of mass-action’s filter class. So it will return the collection of all the rows which we selected while performing this action. And we are iterating through each model and deleting them one by one.

    There are few other things new here, which are not limited to mass-action.
    __(‘A total of %1 blog(s) have been deleted.’, $count) – the %1 will get replaced with $count
    Similarly we can pass multiple variables and format string.

    ->setPath(‘*/*/index’) – here * means replace this component with current value. So it will replace frontName and controllerName with current values which will be blog and manage respectively.

    Please ignore _isAllowed method we will discuss about it when we create ACL as mentioned earlier.

    Now you should get similar result as below,

    PS: The selection column might come at the end as last column. That’s because bookmark has been saved in magento. So we can delete that bookmark by running
    delete from ui_bookmark where namespace=”blogmanager_blog_listing”;
    sql query.

    MassActions Tree

    Now let’s another mass-action to allow the admin to change the status of the blogs. Once again we have to edit the ui component file view/adminhtml/ui_component/blogmanager_blog_listing.xml

    We have added one more action tag in the massaction tag. And in that action tag we have added two sub-actions whose URLs are same but we have passed status param with different values.

    Since it has sub-action structure, so we need to add the component attribute in massaction tag to use tree-massactions component.

    Now let’s create the action file as Controller/Adminhtml/Manage/MassStatus.php

    Here we are doing similar things as we did earlier. We are iterating through each selected blogs and setting the status as the value passed in the param. We set this param-value in the ui component.

    Now you will see the change status mass-action as below,

    Folder Structure,

    Next Blog -> Magento Development 16: Action Column

    Previous Blog -> Magento Development 14: More aboutAdmin Grid

    . . .

    Leave a Comment

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

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again