Magento2 : massdelete is not working
-
07-01-2021 - |
Frage
I have created a custom module with ui_component grid, below is my grid xml file :
...
<massaction name="listing_massaction">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="selectProvider" xsi:type="string">module_record_custom_list.module_record_custom_list.jbs_module_custom_columns.ids</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
<item name="indexField" xsi:type="string">entity_id</item>
</item>
</argument>
<!-- Mass actions which you want to add in your grid-->
<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/custom/massdelete"/>
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Delete</item>
<item name="message" xsi:type="string" translate="true">Do you want to delete selected row record?</item>
</item>
</item>
</argument>
</action>
</massaction>
...
And Created a controller file MassDelete.php
:
<?php
namespace Package\Module\Controller\Adminhtml\Custom;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Package\Module\Model\ResourceModel\Custom\CollectionFactory;
//use Jbs\CustomStore\Helper\Data;
class MassDelete extends \Magento\Backend\App\Action
{
public $request;
/**
* Massactions filter.
*
* @var Filter
*/
protected $_filter;
/**
* @var CollectionFactory
*/
protected $_collectionFactory;
//protected $_customstorehelper;
/**
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
Data $helper
*/
public function __construct(
Context $context,
Filter $filter,
CollectionFactory $collectionFactory,
\Magento\Framework\App\Request\Http $request
)
{
$this->request = $request;
$this->_filter = $filter;
$this->_collectionFactory = $collectionFactory;
//$this->_customstorehelper = $helper;
parent::__construct($context);
}
/**
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
$collection = $this->_filter->getCollection($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)
);
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/');
}
}
But on select Delete from dropdown I'm getting error as below :
"Invalid method Magento\Framework\View\Element\UiComponent\DataProvider\Document::delete"
Where am I doing wrong?
Lösung
I think you are wrong here.
foreach ($collection as $item)
{
$item->delete();
}
You need to load $item
before deleting it.
foreach ($collection as $item)
{
$item = $this->YOUR_MODEL->load($item->getId());
$item->delete();
}
Replace $item->getId()
with your Primary Key.
Hope it'll help you.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange