Thursday, September 27, 2012

Magento : Add massdelete action in magento admin grid

Consider module Bd_Demo and model Test

Add following lines in _prepareMassaction() method of admin grid file
protected function _prepareMassaction()
{
    $this->setMassactionIdField('id');
    $this->getMassactionBlock()->setFormFieldName('test');
    $this->getMassactionBlock()->addItem('delete', array(
         'label'    => Mage::helper('demo')->__('Delete'),
         'url'      => $this->getUrl('*/*/massDelete'),
         'confirm'  => Mage::helper('demo')->__('Are you sure?')
    ));
           
    return $this;
}

And following method in admin controller
public function massDeleteAction()
{
        $webIds = $this->getRequest()->getParam('test');
        if(!is_array($webIds)) {
               Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
        } else {
            try {
                foreach ($webIds as $webId) {
                    $web = Mage::getModel('demo/test')->load($webId);
                    $web->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('adminhtml')->__(
                        'Total of %d record(s) were successfully deleted', count($webIds)
                    )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/index');
}

No comments:

Post a Comment