Magento admin grid mass delete, mass status change checkbox option
In your Grid.php add
protected function _prepareMassaction()
{
$this->setMassactionIdField('Module_id');
$this->getMassactionBlock()->setFormFieldName('banners');
$this->getMassactionBlock()->addItem('delete', array(
'label' => Mage::helper('Module')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('Module')->__('Are you sure?')
));
$statuses = Mage::getSingleton('Module/status')->getOptionArray();
array_unshift($statuses, array('label'=>'', 'value'=>''));
$this->getMassactionBlock()->addItem('status', array(
'label'=> Mage::helper('Module')->__('Change status'),
'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
'additional' => array(
'visibility' => array(
'name' => 'status',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('Module')->__('Status'),
'values' => $statuses
)
)
));
return $this;
}
-----------------------------------------------------------
in your adminhtml-> XXXcontroller .php file
public function massDeleteAction() {
$ids = $this->getRequest()->getParam('Module');
if(!is_array($ids)) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select Banner(s)'));
} else {
try {
foreach ($ids as $id) {
$banners = Mage::getModel('Module/Module')->load($id);
$banners->delete();
}
Mage::getSingleton('adminhtml/session')->addSuccess(
Mage::helper('adminhtml')->__(
'Total of %d record(s) were successfully deleted', count($ids)
)
);
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
public function massStatusAction()
{
$bannersIds = $this->getRequest()->getParam('module');
if(!is_array($ids)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select Banner(s)'));
} else {
try {
foreach ($ids as $id) {
$banners = Mage::getSingleton('Module/Module')
->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');
}
In your Grid.php add
protected function _prepareMassaction()
{
$this->setMassactionIdField('Module_id');
$this->getMassactionBlock()->setFormFieldName('banners');
$this->getMassactionBlock()->addItem('delete', array(
'label' => Mage::helper('Module')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('Module')->__('Are you sure?')
));
$statuses = Mage::getSingleton('Module/status')->getOptionArray();
array_unshift($statuses, array('label'=>'', 'value'=>''));
$this->getMassactionBlock()->addItem('status', array(
'label'=> Mage::helper('Module')->__('Change status'),
'url' => $this->getUrl('*/*/massStatus', array('_current'=>true)),
'additional' => array(
'visibility' => array(
'name' => 'status',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('Module')->__('Status'),
'values' => $statuses
)
)
));
return $this;
}
-----------------------------------------------------------
in your adminhtml-> XXXcontroller .php file
public function massDeleteAction() {
$ids = $this->getRequest()->getParam('Module');
if(!is_array($ids)) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select Banner(s)'));
} else {
try {
foreach ($ids as $id) {
$banners = Mage::getModel('Module/Module')->load($id);
$banners->delete();
}
Mage::getSingleton('adminhtml/session')->addSuccess(
Mage::helper('adminhtml')->__(
'Total of %d record(s) were successfully deleted', count($ids)
)
);
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
public function massStatusAction()
{
$bannersIds = $this->getRequest()->getParam('module');
if(!is_array($ids)) {
Mage::getSingleton('adminhtml/session')->addError($this->__('Please select Banner(s)'));
} else {
try {
foreach ($ids as $id) {
$banners = Mage::getSingleton('Module/Module')
->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');
}
thanks a lot that helps me a lot :p
ReplyDelete