0

I'm very new to Magento 2.3, i created a Mass Action for Status Change, when i open action dropdown and click Change status a tree menu should open with 2 options, but when i click on Change status page just refresh, i write this code in /EC/Downloads/view/adminhtml/ui_component/ec_downloads_items.xml

    <listingToolbar name="listing_top">
        <settings>
            <sticky>true</sticky>
        </settings>
        <paging name="listing_paging"/>
        <filters name="listing_filters"/>

        <massaction name="listing_massaction">

        <action name="delete">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="type" xsi:type="string">delete</item>
                <item name="label" xsi:type="string" translate="true">Delete</item>
                <item name="url" xsi:type="url" path="*/items/MassDelete"/>
                <item name="confirm" xsi:type="array">
                    <item name="title" xsi:type="string" translate="true">Delete</item>
                    <item name="message" xsi:type="string" translate="true">Do you want to delete selected row record?</item>
                </item>
            </item>
        </argument>
        </action>

        <action name="status">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="type" xsi:type="string">status</item>
                <item name="label" xsi:type="string" translate="true">Change status</item>
            </item>
        </argument>
        <argument name="actions" xsi:type="array">
            <item name="0" xsi:type="array">
                <item name="type" xsi:type="string">enable</item>
                <item name="label" xsi:type="string" translate="true">Enable</item>
                <item name="url" xsi:type="url" path="*/items/MassStatus">
                    <param name="status">1</param>
                </item>
            </item>
            <item name="1" xsi:type="array">
                <item name="type" xsi:type="string">disable</item>
                <item name="label" xsi:type="string" translate="true">Disable</item>
                <item name="url" xsi:type="url" path="*/items/MassStatus">
                    <param name="status">2</param>
                </item>
            </item>
        </argument>
        </action>

        </massaction>
    </listingToolbar>

and this code in /EC/Downloads/Controller/Adminhtml/Items/MassStatus.php

<?php
namespace EC\Downloads\Controller\Adminhtml\Items;

use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use EC\Downloads\Model\ResourceModel\Items\CollectionFactory; // Define your collection here
use Magento\Framework\Controller\ResultFactory;

/**
 * Class MassDisable
 */
class MassStatus extends \Magento\Backend\App\Action
{
    /**
     * @var Filter
     */
    protected $filter;

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


    /**
     * @param Context $context
     * @param Filter $filter
     * @param CollectionFactory $collectionFactory
     */
    public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
    {
        $this->filter = $filter;
        $this->collectionFactory = $collectionFactory;
        parent::__construct($context);
    }
    /**
     * Execute action
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     * @throws \Magento\Framework\Exception\LocalizedException|\Exception
     */
    public function execute()
    {
    echo 'Hello';exit;
        $statusValue = $this->getRequest()->getParam('status');
        $collection = $this->filter->getCollection($this->collectionFactory->create());

        foreach ($collection as $item) {
            $item->setStatus($statusValue);
            $item->save();
        }

        $this->messageManager->addSuccess(__('A total of %1 record(s) have been modified.', $collection->getSize()));

        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        return $resultRedirect->setPath('*/*/');
    }
}

code in /EC/Downloads/Model/ResourceModel/Items.php

<?php

namespace EC\Downloads\Model\ResourceModel;
/**
 * Class Items
 * @package EC\Downloads\Model\ResourceModel
 */
class Items extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    /**
     *
     */
    protected function _construct()
    {
        $this->_init('downloads', 'downloads_id');
    }
}
0

I forgot to add the component in <massaction> tag:

<massaction name="listing_massaction" component="Magento_Ui/js/grid/tree-massactions">
     .........
</massaction>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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