3

I have created Admingrid and associate UI form. I need to change the Status for all the items using Massaction. Any one know Please suggest me.. Uigrid.xml

 <massaction name="listing_massaction">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="selectProvider" xsi:type="string">ox_homeslider_post_listing.ox_homeslider_post_listing.ox_homeslider_post_columns.ids</item>
                <item name="indexField" xsi:type="string">id</item>
            </item>
        </argument>
        <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="homeslider/post/massdelete"/>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Delete Slides</item>
                        <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected Slides?</item>
                    </item>
                </item>
            </argument>
        </action>
         <action name="status">
            <settings>
                <type>status</type>
                <label translate="true">Change status</label>
                <actions>
                    <action name="0">
                        <type>enable</type>
                        <label translate="true">Enable</label>
                        <url path="homeslider/post/massStatus">
                            <param name="status">1</param>
                        </url>
                    </action>
                    <action name="1">
                        <type>disable</type>
                        <label translate="true">Disable</label>
                        <url path="homeslider/post/massStatus">
                            <param name="status">2</param>
                        </url>
                    </action>
                </actions>
            </settings>
        </action>
    </massaction>
5

You can define your massaction in your uicomponent grid.

Inside listingToolbar> tag you can define <massaction> as many mass action you want.

<listingToolbar 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">listing_name.listing_name.listing_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="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="*/*/massDelete"/>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Delete items</item>
                        <item name="message" xsi:type="string" translate="true">Are you sure to delete selected entries?</item>
                    </item>
                </item>
            </argument>
        </action>
    </massaction>
</listingToolbar>

<item name="indexField" xsi:type="string">entity_id</item> defines the primary key which you will get in your controller.

<item name="url" xsi:type="url" path="*/*/massDelete"/> defines the path of your controller where you can call your model and perform mass action on it. You need to create controller for same.

Update For Mass Status Update

You need to define your mass controller

<?php
namespace Namespace\Modulename\Controller\Adminhtml\Post;

use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Namespace\Modulename\Model\ResourceModel\Post\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()
    {
        $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('*/*/');
    }
}
| improve this answer | |
  • I have already done MassDelete function. Now need to do change status which will be updated for all items listing in grid. I – Jaisa Nov 16 '17 at 13:06
  • I have updated my question .Please refer that – Jaisa Nov 16 '17 at 13:11
  • I need grid format for mass status. But in above, you have given grid code for mass delete – Jaisa Nov 17 '17 at 6:42
3

I have added tree-massactions and display area in grid structure. After that Mass actions works fine. Here I have given grid structure for mass actions. Hope it helps.

grid.xml for Massactions

<massaction name="listing_massaction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">ox_homeslider_post_listing.ox_homeslider_post_listing.ox_homeslider_post_columns.ids</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
                    <item name="displayArea" xsi:type="string">bottom</item>
                    <item name="indexField" xsi:type="string">id</item>
                </item>
            </argument>
            <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="homeslider/post/massdelete"/>
                        <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">Delete Slides</item>
                            <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected Slides?</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="homeslider/post/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="homeslider/post/massstatus">
                            <param name="status">0</param>
                        </item>
                    </item>
                </argument>
            </action>
        </massaction>
| improve this answer | |

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.