2

I trying to implement dynamic status in admin sales grid under mass action,but unable to get this.

I was able to see the single mass action but not sub mass action under the sales mass action.

I tried many blogs but it didn't work.

I have used below two blogs but it is not coming as expected

http://www.ibnab.com/en/blog/magento-2/magento-2-ui-components-new-dynamic-ui-treemassaction-technique

http://webkul.com/blog/create-dynamic-mass-action-magento-2-grid/

I have added below snippit in view/adminhtml/ui_component/sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Ui/etc/ui_configuration.xsd">
  <container name="listing_top">
    <massaction name="listing_massaction">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="actions" xsi:type="array">
                    <item name="delete" xsi:type="array">
                        <item name="type" xsi:type="string">Status</item>
                        <item name="label" xsi:type="string" translate="true">Status</item>
                        <item name="url" xsi:type="string">sales/order/massStatus</item>
                    </item>
                </item>
            </item>
        </argument>
    </massaction>
</container>

Please help me to solve above problem.

enter image description here

1

3 Answers 3

2
+50

To add the mass action, create the ui component and specify Magento_Ui/js/grid/tree-massactions massaction renderer

app\code\Magentoins\Pdfgenerator\view\adminhtml\ui_component\sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <container name="listing_top">
        <massaction name="listing_massaction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">sales_order_grid.sales_order_grid.sales_order_columns.ids</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
                    <item name="indexField" xsi:type="string">entity_id</item>
                </item>
            </argument>
            <action name="select_template">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">pdfgenerator_pdftemplates</item>
                        <item name="label" xsi:type="string" translate="true">PDF Orders</item>
                    </item>
                </argument>
                <argument name="actions" xsi:type="configurableObject">
                    <argument name="class" xsi:type="string">Magentoins\Pdfgenerator\Ui\Component\Sales\Order\MassAction\Pdftemplates</argument>
                    <argument name="data" xsi:type="array">
                        <item name="urlPath" xsi:type="string">pdfgenerator/pdfgenerator/sales_massorderpdf</item>
                        <item name="paramName" xsi:type="string">template_id</item>
                        <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">PDF Template</item>
                            <item name="message" xsi:type="string" translate="true">Are you sure to use selected template for PDF?</item>
                        </item>
                    </argument>
                </argument>
            </action>
        </massaction>
    </container>
</listing>

Under the massaction class provide the action options in jsonSerialize

app\code\Magentoins\Pdfgenerator\Ui\Component\Sales\Order\MassAction\Pdftemplates.php

<?php
namespace Magentoins\Pdfgenerator\Ui\Component\Sales\Order\MassAction;
use Magento\Framework\UrlInterface;
use Zend\Stdlib\JsonSerializable;
use Magentoins\Pdfgenerator\Model\ResourceModel\Pdfgenerator\CollectionFactory;

/**
 * Class Options
 */
class Pdftemplates implements JsonSerializable
{
    /**
     * @var array
     */
    protected $options;

    /**
     * @var CollectionFactory
     */
    protected $collectionFactory;

    /**
     * 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(
        CollectionFactory $collectionFactory,
        UrlInterface $urlBuilder,
        array $data = []
    ) {
        $this->collectionFactory = $collectionFactory;
        $this->data = $data;
        $this->urlBuilder = $urlBuilder;
    }

    /**
     * Get action options
     *
     * @return array
     */
    public function jsonSerialize()
    {
        $i=0;
        if ($this->options === null) {
            // get the massaction data from the database table
            $templateCollection = $this->collectionFactory->create()->addFieldToFilter('is_active',['eq'=>1]);

            if(!count($templateCollection)){
                return $this->options;
            }
            //make a array of massaction
            foreach ($templateCollection as $key => $badge) {
                $options[$i]['value']=$badge->getEntityId();
                $options[$i]['label']=$badge->getTitle();
                $i++;
            }
            $this->prepareData();
            foreach ($options as $optionCode) {
                $this->options[$optionCode['value']] = [
                    'type' => 'template_' . $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
                );
            }

            // return the massaction data
            $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;
            }
        }
    }
}
5
  • Hello Ajay, can you tell me how we can the sort order of the mass action. Commented Jun 19, 2016 at 7:28
  • Ajay can you tell me how we can add mention image structure in m2 i.e adding select box on click of mass action and even sort order. Commented Jun 19, 2016 at 7:32
  • Ajay,,can you guide me on this post as well magento.stackexchange.com/questions/120197/… Commented Jun 19, 2016 at 7:35
  • It gives class argument is invalid error. Commented Apr 17, 2020 at 7:36
  • This is not compatible on m2.4 Commented Jun 10, 2022 at 11:13
1

Reference: https://devdocs.magento.com/guides/v2.4/ui_comp_guide/components/ui-treemassactions.html

Add below snippet at view/adminhtml/ui_component/sales_order_grid.xml

Output: https://prnt.sc/1rbil23

<listingToolbar name="listing_top">
    <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="info_delete">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">info_delete</item>
                    <item name="label" xsi:type="string" translate="true">Delete</item>
                    <item name="url" xsi:type="url" path="vendormod/index/massdelete"/>
                    <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">Delete Blogs?</item>
                            <item name="message" xsi:type="string" translate="true">Are you sure you want to delete the selected blogs?</item>
                    </item>
                </item>
            </argument>
        </action>
        <action name="status2">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">status2</item>
                    <item name="label" xsi:type="string" translate="true">Change Status 2</item>
                </item>
            </argument>
            <argument name="actions" xsi:type="array">
                <item name="0" xsi:type="array">
                    <item name="type" xsi:type="string">sub_action1</item>
                    <item name="label" xsi:type="string" translate="true">#1 Inactive</item>
                    <item name="url" xsi:type="url" path="vendormod/index/massstatus">
                        <param name="status">1</param>
                    </item>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Delete Blogs?</item>
                        <item name="message" xsi:type="string" translate="true">Are you sure you want to delete the selected blogs?</item>
                    </item>
                </item>
                <item name="1" xsi:type="array">
                    <item name="type" xsi:type="string">sub_action2</item>
                    <item name="label" xsi:type="string" translate="true">#2 Pending approval</item>
                    <item name="url" xsi:type="url" path="vendormod/index/massstatus">
                        <param name="status">2</param>
                    </item>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Delete Blogs?</item>
                        <item name="message" xsi:type="string" translate="true">Are you sure you want to delete the selected blogs?</item>
                    </item>
                </item>
                <item name="2" xsi:type="array">
                    <item name="type" xsi:type="string">sub_action3</item>
                    <item name="label" xsi:type="string" translate="true">#3 Active</item>
                    <item name="url" xsi:type="url" path="vendormod/index/massstatus">
                        <param name="status">3</param>
                    </item>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Delete Blogs?</item>
                        <item name="message" xsi:type="string" translate="true">Are you sure you want to delete the selected blogs?</item>
                    </item>
                </item>
            </argument>
        </action>
    </massaction>
</listingToolbar>
0

To specify new nested/dynamic massaction for grid you need explicitly set Magento_Ui/js/grid/tree-massactions as a renderer for actions in grid.

Also you need specify provider of options for your massaction, this class should implement Zend\Stdlib\JsonSerializable

See example below, how to customize sales order grid with nested massactions. (ui component configuration ExtensoinModule/view/adminhtml/ui_component/sales_order_grid.xml)

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <listingToolbar name="listing_top">
    <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>
    </argument>
        <action name="set_status">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">set_status</item>
                    <item name="label" xsi:type="string" translate="true">Set Status</item>
                </item>
            </argument>
            <argument name="actions" xsi:type="configurableObject">
                <argument name="class" xsi:type="string">ExtensoinModule\Ui\Component\MassAction\Status\Options</argument>
                <argument name="data" xsi:type="array">
                    <item name="urlPath" xsi:type="string">extension/index/massSetStatus</item>
                    <item name="paramName" xsi:type="string">group</item>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Set Order Status</item>
                        <item name="message" xsi:type="string" translate="true">Are you sure?</item>
                    </item>
                </argument>
            </argument>
        </action>
    </massaction>
    </listingToolbar>
</listing>
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.