Magento – Mass Action not sending all data

adminhtmlgridmagento-2.1massaction

I've created a mass action for my custom grid. i've encountered a weird problem which is when i check all the checkboxes, it won't send any post id data when i try to retrieve it with:

$this->getRequest->getParams();

but when i don't check all of them, it will send all the data that has been checked, here's my mass action code in xml:

<massaction name="listing_massaction">
  <action name="test">
    <argument name="data" xsi:type="array">
      <item name="config" xsi:type="array">
          <item name="type" xsi:type="string">test</item>
          <item name="label" xsi:type="string" translate="true">Test</item>
          <item name="url" xsi:type="url" path="namespace_module/test/massTest"/>
      </item>
    </argument>
  </action>
</massaction>

MassTest.php:

use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

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

   public function __construct(
       Context $context,
       Filter $filter,
       \Namespace\Module\Model\ResourceModel\Test\CollectionFactory $collectionFactory
   ) {
       $this->filter = $filter;
       $this->_listingProduct = $listingProduct;
       $this->_collectionFactory = $collectionFactory;
       parent::__construct($context, $filter);
   }

   public function execute(){
     //$collection = $this->filter->getCollection($this->_collectionFactory->create()); // get your selected collection
     //$collectionSize = $collection->getSize();
     //echo $collectionSize;
     $data = $this->getRequest()->getParams();
     print_r($data['selected']);
     exit;
   }

   /**
    * @param AbstractCollection $collection
    * @return \Magento\Backend\Model\View\Result\Redirect
    */
   protected function massAction(AbstractCollection $collection)
   {

   }

}

Best Answer

I spent a lot of time trying to understand, what is wrong with my code, and why Magento does not send selected parameter with ids in POST request, when mass action is called. Later I found this "feature":

if you have 1 item in list of items, mass action does not send any data. You must have at least 2 items in list, and then selected parameter will exists in POST request and will consists of selected ids.

I have it in Magento 2.1.6.