I'm very new to Magento
, i created a Mass Action for Delete i write this code in /EC/Downloads/view/adminhtml/ui_component/ec_downloads_items.xml
<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top">
<settings>
<sticky>true</sticky>
</settings>
<paging name="listing_paging"/>
<filters name="listing_filters"/>
<massaction name="listing_massaction">
<action name="delete">
<settings>
<confirm>
<message translate="true">Delete selected items?</message>
<title translate="true">Delete items</title>
</confirm>
<url path="*/items/MassDelete"/>
<type>delete</type>
<label translate="true">Delete</label>
</settings>
</action>
</massaction>
</listingToolbar>
<columns name="ec_downloads_items_columns">
<selectionsColumn name="ids">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="resizeEnabled" xsi:type="boolean">false</item>
<item name="resizeDefaultWidth" xsi:type="string">55</item>
<item name="indexField" xsi:type="string">downloads_id</item>
<item name="sortOrder" xsi:type="number">10</item>
</item>
</argument>
</selectionsColumn>
<column name="downloads_id">.......</column>
</columns>
</listing>
and this code in /EC/Downloads/Controller/Adminhtml/Items/MassDelete.php
<?php
namespace EC\Downloads\Controller\Adminhtml\Items;
use Magento\Backend\App\Action;
/**
* Class MassDelete
*/
class MassDelete extends \Magento\Backend\App\Action
{
/**
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
$ids = $this->getRequest()->getParam('downloads_id');
echo $ids;
exit;
if (!is_array($ids) || empty($ids)) {
echo 'Bad';
exit;
$this->messageManager->addError(__('Please select Item.'));
} else {
echo 'Good';
exit;
try {
foreach ($ids as $id) {
$emp = $this->_objectManager->get('EC\Downloads\Model\Downloads')->load($id);
$emp->delete();
}
$this->messageManager->addSuccess(
__('A total of %1 record(s) have been deleted.', count($ids))
);
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
}
}
return $this->resultRedirectFactory->create()->setPath('downloads/index/items');
}
}
code in /EC/Downloads/Model/ResourceModel/Items.php
<?php
namespace EC\Downloads\Model\ResourceModel;
/**
* Class Items
* @package EC\Downloads\Model\ResourceModel
*/
class Items extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
*
*/
protected function _construct()
{
$this->_init('downloads', 'downloads_id');
}
}
When i try to delete more items it's not working, when i try to echo|exit
, i found that i dosen't go to the delete part coz ids
is empty.