0

I'm having some trouble getting "Select All" working properly for a custom grid.

I have a massDelete action working and deleting multiple items. I have a multiselect column which, after I run a "Select All", displays the fact that the full count is selected (It says that the full count of 150 oo so items are selected). The problem is that when I run the massDelete action after a "Select All", only the 20 or so items on the current page are deleted.

Here is my Mass Action:

<massaction name="listing_massaction">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="selectProvider" xsi:type="string">rule_listing.rule_listing.rule_listing_columns.ids</item>
            <item name="indexField" xsi:type="string">rule_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 you wan't to delete selected items?</item>
                </item>
            </item>
        </argument>
    </action>
</massaction>

Here is the selectionsColumn

<selectionsColumn name="ids">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="indexField" xsi:type="string">rule_id</item>
        </item>
    </argument>
</selectionsColumn>

My controller is using the following code:

$this->filter->getCollection($this->collectionFactory->create())
foreach ($collection as $model) {
    $model->delete();
}

I have checked the values that are being POSTed to the controller. There is no difference between what is POSTed after doing a "Select All" compared to selecting the first page.

Am I missing something?

3

3 Answers 3

2

This is an upstream bug:

https://github.com/magento/magento2/issues/23110

Solution: https://github.com/magento/magento2/pull/22704/commits/595a7a8f56571a5bd2753096bef9db27d46020ff

app/code/Magento/Ui/view/base/web/js/grid/massactions.js - remove the following lines (156-160):

if (itemsType === 'excluded' && data.selected && data.selected.length) {
  itemsType = 'selected';
  data[itemsType] = _.difference(data.selected, data.excluded);
}
0

This has been fixed in Magento 2.3.3 where you can now do a mass action on more than 200 items.

So long as you have selectedProvider configured correctly there should be no problems getting ids in the controller now.

0

After spending two days I found the solution.

public function execute()
{
    $collectionSize = 0;
    $parameterData = $this->getRequest()->getParams();
   //For selected Ids only
    if(array_key_exists("selected", $parameterData))  
    {
        $collection = $this->filter->getCollection($this->collectionFactory->create());
        $collectionSize = $collection->getSize();
        foreach ($collection as $item) {
            $item->delete();
        }    
    }else if(!array_key_exists("selected", $parameterData) && array_key_exists("excluded", $parameterData))   //For select all
    {
      
        $collection = $this->collectionFactory->create();
        $collectionSize = $collection->getSize();
        foreach ($collection as $item) {
            $item->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('*/*/');
}

I hope someone will find this helpful.

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.