0

In my custom module I am showing the records in the admin panel grid using ui_component and it's showing good. When I am selecting count()-1 records the selected parameter is passing to controller to delete the records as shown below pic.

enter image description here

When I am selecting the all records from Mass Action the selected parameter is not passing to controller see below pic.

enter image description here

for this I have written the ui_component xml file as shown below.

<?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">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">abc_company_grid.abc_company_grid_data_source</item>
            <item name="deps" xsi:type="string">abc_company_grid.abc_company_grid_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">abc_company_columns</item>
    </argument>
    <dataSource name="abc_company_grid_data_source" >
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
            <argument name="name" xsi:type="string">abc_company_grid_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">abclogger_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">abclogger_id</item>
                    </item>
                </item>
            </argument>
        </argument>
    </dataSource>
    <listingToolbar name="listing_top">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sticky" xsi:type="boolean">true</item>
            </item>
        </argument>
        <bookmark name="bookmarks"/>
        <columnsControls name="columns_controls"/>
        <exportButton name="export_button"/>
        <filterSearch name="fulltext"/>
        <filters name="listing_filters"/>
        <massaction name="listing_massaction" component="Magento_Ui/js/grid/tree-massactions">
            <!--<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="selectProvider" xsi:type="string">abc_company_grid.abc_company_grid.abc_company_columns.ids</item>
                    <item name="indexField" xsi:type="string">abclogger_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="dynamicsnav/index/massDelete"/>
                        <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">Delete Logger</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>
        <paging name="listing_paging"/>
    </listingToolbar>
    <columns name="abc_company_columns">
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="indexField" xsi:type="string">abclogger_id</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="abclogger_id">
            <settings>
                <filter>text</filter>
                <sorting>asc</sorting>
                <label translate="true">ID</label>
            </settings>
        </column>
        <column name="message">
            <settings>
                <filter>text</filter>
                <label translate="true">Message</label>
                <editor>
                    <editorType>text</editorType>
                    <validation>
                        <rule name="required-entry" xsi:type="boolean">false</rule>
                    </validation>
                </editor>
            </settings>
        </column>
        <column name="type_of_message">
            <settings>
                <filter>text</filter>
                <label translate="true">Type of Request</label>
                <editor>
                    <editorType>text</editorType>
                    <validation>
                        <rule name="required-entry" xsi:type="boolean">false</rule>
                    </validation>
                </editor>
            </settings>
        </column>
        <column name="created_at">
            <settings>
                <filter>text</filter>
                <label translate="true">Created at</label>
                <editor>
                    <editorType>text</editorType>
                    <validation>
                        <rule name="required-entry" xsi:type="boolean">false</rule>
                    </validation>
                </editor>
            </settings>
        </column>
        <actionsColumn class="ABCSolutions\Company\Ui\Component\Listing\Column\abcLoggerActions" name="actions">
            <settings>
                <indexField>abclogger_id</indexField>
                <resizeEnabled>false</resizeEnabled>
                <resizeDefaultWidth>107</resizeDefaultWidth>
            </settings>
        </actionsColumn>
    </columns>
</listing>

enter image description here

And the Controller method

public function execute()
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        $params = $this->getRequest()->getParams();
        if (array_key_exists('selected', $params)) {
            try {
                foreach ($params['selected'] as $key => $id) {                         
                    $model = $this->_objectManager->create(ABCLogger::class);
                    $model->load($id);
                    $model->delete();
                }
                $message=__("Logs deleted successfully.");
                $this->messageManager->addSuccess($message);
            } catch (\Exception $e) {
                $this->messageManager->addError(__('Something went wrong. Please try again.'));
            }
        } else {
            $this->messageManager->addError(__('Something went wrong. Please try again.'));
        }
        return $resultRedirect->setPath('dynamicsnav/index/index');
    }

Can you please suggest me where I went wrong?

1
  • @AbdulSamadAbbasi Updated my thread.
    – Bojjaiah
    Nov 10, 2020 at 11:34

1 Answer 1

0

In your ui_listing :

<massaction name="listing_massaction" component="Magento_Ui/js/grid/tree-massactions">
            <action name="delete">
                <settings>
                    <confirm>
                        <message translate="true">Are you sure you want to delete selected items?</message>
                        <title translate="true">Delete items</title>
                    </confirm>
                    <url path="module/controller/MassDelete"/>
                    <type>delete</type>
                    <label translate="true">Delete</label>
                </settings>
            </action>

In your MassDelete controller class:

<?php

namespace Vendor\Module\Controller\Adminhtml\Controllername;

use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Vendor\Module\Model\ResourceModel\Module\CollectionFactory;

/**
 * Class MassDelete
 */
class MassDelete 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);
    }
    
    /**
     * Authorization level
     *
     * @see _isAllowed()
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Vendor_Module::delete');
    }

    /**
     * Execute action
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     * @throws \Magento\Framework\Exception\LocalizedException|\Exception
     */
    public function execute()
    {
        $collection = $this->filter->getCollection($this->collectionFactory->create());
                
        $collectionSize = $collection->getSize();

        foreach ($collection as $bar) {
            $bar->delete();
        }

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

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

Hope, that works.

5
  • Not working for me.
    – Bojjaiah
    Nov 10, 2020 at 11:38
  • Did you try my controller? and is ABClogger your resource model? Nov 10, 2020 at 11:41
  • See problem is not the controller the selected record/ids $params['selected'] are not coming in parameters i.e $this->getRequest()->getParams(). See my thread. Thank you.
    – Bojjaiah
    Nov 10, 2020 at 11:44
  • 1
    I think that has to do something with this dependency use Magento\Ui\Component\MassAction\Filter; that you're not using in your controller. This controller is working perfectly at my side. I'd suggest you to try it once and see what happens. Nov 10, 2020 at 11:47
  • let me try @Abdul
    – Bojjaiah
    Nov 10, 2020 at 12:06

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.