2

How to Implement massactions in grid using ui_component magento2. I have created my ui_component and need to implement massactions. I have also created controllers for the required paths. But, somehow it is just not working. I'm using magento 2.1 C.E.

ui_component/test_grid.xml

<massaction name="listing_massaction">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="selectProvider" xsi:type="string">test_grid.test_grid.test_grid_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="accept">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="type" xsi:type="string">accepted</item>
                <item name="label" xsi:type="string" translate="true">Accept</item>
                <item name="url" xsi:type="string">test/Index/Massaccept</item>
            </item>
        </argument>
    </action>
</massaction>

Index/Massaccept.php

<?php
    namespace Vendor\Module\Controller\Adminhtml\Index;
    use \Vendor\Module\Model\TestFactory;

    class Massaccept extends \Magento\Backend\App\Action {

        protected $resultPageFactory = false;
        protected $_fasctcureFactory;

        public function __construct(
            TestFactory $testFactory,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory,
            \Magento\Backend\App\Action\Context $context
            ) {
            $this->_testFactory = $testFactory;
            $this->resultPageFactory = $resultPageFactory;
            parent::__construct($context);
        }

        public function execute() {
            die("working");
            $ids = $this->getRequest()->getParam('id');
            $model = $this->_testFactory->create();

            foreach($ids as $id) {
                $model->load($id);
                $model->getStatus();
                $model->setStatus('Processing');
                $model->save();         
            }
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setRefererOrBaseUrl();
            return $resultRedirect;
        }
    }

My controllers are in the path:

Vendor/Module/Controller/Adminhtml/Index/Massaccept.php Vendor/Module/Controller/Adminhtml/Index/Massreject.php

The mass actions are visible on the grid, but are not redirecting anywhere.Redirect is not working for using above code.

All I'm getting is this error:

Uncaught TypeError: Cannot read property 'total' of undefined in massactions.js:52

2

2 Answers 2

7

Please try this. It's working fine for me.

<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="test/index/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 you wan't to delete selected items?</item>
                    </item>
                </item>
            </argument>
        </action>
    </massaction>

Your controller name like MassDelete.php

This is my MassActive.php

Please refer this and change your massaccept.php file and then check after that.

namespace Dedicated\Grid\Controller\Adminhtml\Item;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Dedicated\Grid\Model\ResourceModel\Grid\CollectionFactory;

class MassActive extends \Dedicated\Grid\Controller\Adminhtml\Item
{

/**
 * @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);
}
/**
 * Delete action
 *
 * @return \Magento\Backend\Model\View\Result\Redirect
 */
public function execute()
{
    $collection = $this->filter->getCollection($this->collectionFactory->create());
    $collectionSize = $collection->getSize();

    foreach ($collection as $template) {
        $template->setIsActive(true);
        $template->save();
    }

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

    /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    return $resultRedirect->setPath('*/*/');
}
}
9
  • my controller name is Massdelete.php which is inside Index directory. Now, what shall be done? Commented Oct 12, 2016 at 11:00
  • You can change the file name as well as change url here like this <item name="url" xsi:type="url" path="test/index/massdelete"/> Commented Oct 12, 2016 at 11:02
  • I keep getting this error : massactions.js:52 Uncaught TypeError: Cannot read property 'total' of undefined Commented Oct 12, 2016 at 11:15
  • Can you add you massdelete.php file ? Commented Oct 12, 2016 at 11:17
  • Now, I have kept only one action that is accept and I have put Index/Massaccept.php file right there. Commented Oct 12, 2016 at 11:22
0

You have to pass path like test/index/massdelete and test/index/massaccept in below arguments.

 <action name="reject">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="type" xsi:type="string">rejected</item>
                <item name="label" xsi:type="string" translate="true">Reject</item>
                <item name="url" xsi:type="url" path="*/*/MassDelete"><item/>
                <item name="confirm" xsi:type="array">
                    <item name="title" xsi:type="string" translate="true">Reject items</item>
                    <item name="message" xsi:type="string" translate="true">Are you sure to reject selected Requests?</item>
                </item>
            </item>
        </argument>
    </action>
    <action name="accept">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="type" xsi:type="string">accepted</item>
                <item name="label" xsi:type="string" translate="true">Accept</item>
                <item name="url" xsi:type="string">test/Index/Massaccept</item>                        
            </item>
        </argument>
    </action>

You have to replace with below code,

 <action name="reject">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="type" xsi:type="string">rejected</item>
                <item name="label" xsi:type="string" translate="true">Reject</item>
                <item name="url" xsi:type="url" path="test/index/massDelete"><item/>
                <item name="confirm" xsi:type="array">
                    <item name="title" xsi:type="string" translate="true">Reject items</item>
                    <item name="message" xsi:type="string" translate="true">Are you sure to reject selected Requests?</item>
                </item>
            </item>
        </argument>
    </action>
    <action name="accept">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="type" xsi:type="string">accepted</item>
                <item name="label" xsi:type="string" translate="true">Accept</item>
                <item name="url" xsi:type="string">test/index/massAccept</item>                        
            </item>
        </argument>
    </action>

Your controller file path should be MassDelete.php and MassAccept.php

4
  • massactions.js:52 Uncaught TypeError: Cannot read property 'total' of undefined Commented Oct 12, 2016 at 9:56
  • clear var and remove cache Commented Oct 12, 2016 at 11:29
  • Did that already, I have even compiled the source, cache is disabled Commented Oct 12, 2016 at 11:29
  • have you changed filename and update your xml code based on above post? Commented Oct 12, 2016 at 11:58

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.