Hello Readers,
In this tutorial, i will explain how to add massDelete & massStatus action in magento. This tutorial assumes you already know how to create Magento admin grid.
Advantage of Mass Action: Mass Action option plays very important role in grid. It work with many rows at the same time & perform action without viewing each row in details.
How to add Massactions into Magento Grid
It is very easy to add Massaction in magento. To accomplish this, follow the below steps:
Step 1 Go to Namespace/Modulename/Block/Adminhtml/Modulename/Grid.php file and add _prepareMassaction() in Grid.php.
protected function _prepareMassaction()
{
$this->setMassactionIdField('Modulename_id');
$this->getMassactionBlock()->setFormFieldName('module');
$this->getMassactionBlock()->addItem('delete', array(
'label' => Mage::helper('Modulename')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
));
$statuses = Mage::getSingleton('slideshow/status')->getOptionArray();
//print_r($statuses); die;
array_unshift($statuses, array('label'=>'', 'value'=>''));
$this->getMassactionBlock()->addItem('status', array(
'label'=> Mage::helper('Modulename')->__('Change status'),
'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
'additional' => array(
'visibility' => array(
'name' => 'status',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('Modulename')->__('Status'),
'values' => $statuses
)
)
));
return $this;
}
$this->getMassactionBlock()->setFormFieldName(’module’):
This above line passed all the ID's to the controller massAction methods.
‘url’ => $this->getUrl(‘*/*/massDelete’): This will go to massDeleteAction() in the controller.
similarly, 'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)): This will go to massStatusAction() in the controller.
Step: 2 Go to Namespace/Modulename/controllers/Adminhtml/IndexController.php
In this controller file, you need to implement above mass actions which is define in _prepareMassaction() method in step 2.
public function massDeleteAction()
{
$Ids = $this->getRequest()->getParam('module');
//print_r($Ids); die;
if(!is_array($Ids)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select checkbox'));
} else {
try {
$model = Mage::getSinglton('module/module');
foreach ($Ids as $adId) {
$model->load($adId)->delete();
}
Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Total of %d record(s) were deleted.', count($Ids)));
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$this->_redirect('*/*/');
}
public function massStatusAction()
{
$ids = $this->getRequest()->getParam('slideshow');
//print_r($ids); die;
if(!is_array($ids)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select checkbox'));
} else {
try {
foreach ($ids as $id) {
//print_r($id); die;
Mage::getSingleton('slideshow/slideshow')
->load($id)
->setStatus($this->getRequest()->getParam('status'))
->setIsMassupdate(true)
->save();
}
$this->_getSession()->addSuccess(
$this->__('Total of %d record(s) were successfully updated', count($ids))
);
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
0 Comment(s)