Magento

How to Add Massactions into Magento’s Grid?

Today I have got solutions regarding How to Add Massactions into Magento’s Grid? It is very easy to add Massaction in magento. Step 1 open your [Namespace]/[Modulename]/Block/Adminhtml/[module]/Grid.php file and extend the functionality. so we have to add one method _prepareMassaction() as below.

protected function _prepareMassaction()
{
$this->setMassactionIdField('[module]_listing_id');
$this->getMassactionBlock()->setFormFieldName('[module]_id');
$this->getMassactionBlock()->addItem('delete', array(
'label'=> $this->__('Delete'),
'url'  => $this->getUrl('*/*/massDelete', array('' => '')),
'confirm' => $this->__('Are you sure you want to delete the selected listing(s)?')
));
return $this;
}

Step 2 : When you add above code you will see the grid as below image.

massaction
massaction

Step 3 : Now add Delete funtion code in  [Namespace]/[Modulename]/Block/Adminhtml/[module]Controller.php and write below code.

public function massDeleteAction()
{
$adListingIds = $this->getRequest()->getParam('[module]_id');

if(!is_array($adListingIds)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select Ad Listing(s).'));
} else {
try {
$model = Mage::getSingleton('/');
foreach ($adListingIds as $adId) {
$model->load($adId)->delete();
}
Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Total of %d record(s) were deleted.', count($adListingIds)));
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$this->_redirect('*/*/');
}

Step 4: If you are follow above steps and you can achieve this functionality on your custom grid.

Advertisements

Leave your comments

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s