My custom module massaction action code as below:
<?php
namespace Namespace\Modulename\Controller\Adminhtml\Index;
use Magento\Backend\App\Action;
use Namespace\Modulename\Model\ResourceModel\Modulename\CollectionFactory;
use Magento\Ui\Component\MassAction\Filter;
/**
* Class MassDelete
*/
class MassDelete extends \Magento\Backend\App\Action
{
protected $_coreRegistry = null;
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
protected $moduleFactory;
/**
* @param Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
* @param \Magento\Framework\Registry $registry
*/
public function __construct(
Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Registry $registry,
Filter $filter,
CollectionFactory $module
) {
$this->resultPageFactory = $resultPageFactory;
$this->_coreRegistry = $registry;
$this->moduleFactory = $module;
$this->filter = $filter;
parent::__construct($context);
}
/**
* Execute action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @throws \Magento\Framework\Exception\LocalizedException|\Exception
*/
public function execute()
{
$deleteIds = $this->getRequest()->getParams('selected');
$collection = $this->moduleFactory->create();
$collection->addFieldToFilter('id', array('in' => $deleteIds));
$count = 0;
foreach ($collection as $child) {
$child->delete();
$count++;
}
$this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $count));
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
}
Using above code if I select some record it is working fine.But if I select all it is not working (no one record deleted).Also I have tried using below code.
<?php
namespace Namespace\Modulename\Controller\Adminhtml\Index;
use Magento\Backend\App\Action;
use Namespace\Modulename\Model\ResourceModel\Modulename\CollectionFactory;
use Magento\Ui\Component\MassAction\Filter;
/**
* Class MassDelete
*/
class MassDelete extends \Magento\Backend\App\Action
{
protected $_coreRegistry = null;
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
protected $moduleFactory;
/**
* @param Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
* @param \Magento\Framework\Registry $registry
*/
public function __construct(
Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Registry $registry,
Filter $filter,
CollectionFactory $module
) {
$this->resultPageFactory = $resultPageFactory;
$this->_coreRegistry = $registry;
$this->moduleFactory = $module;
$this->filter = $filter;
parent::__construct($context);
}
/**
* Execute action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @throws \Magento\Framework\Exception\LocalizedException|\Exception
*/
public function execute()
{
$collection = $this->filter->getCollection($this->moduleFactory->create());
$count = 0;
foreach ($collection as $child) {
$child->delete();
$count++;
}
$this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $count));
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
}
Above code gives error as below:
SQLSTATE[42S22]: Column not found: 1054 Champ '' inconnu dans where clause, query was: SELECT
main_table
.* FROMmytable
ASmain_table
WHERE (`` IN('', '', '', '', '', ''))
ui_component code:
<massaction name="listing_massaction">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="selectProvider" xsi:type="string">module_index_grid.module_index_grid.module_columns.ids</item>
<item name="indexField" xsi:type="string">module_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="module/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>
Model code app/code/Namespace/Modulename/Model/ResourceModel/Module.php
<?php
namespace Namespace\Modulename\Model\ResourceModel;
class Module extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('mytable', 'module_id');
}
}
?>