How to add massactions to Magento’s grid

This article describes how does Magento’s mass actions work and how to add them in your existing grids.
You need to do 2 things:
– extend the block (grid) class with “protected function _prepareMassaction()”
– add an action into your controller, which will handle your desired logic.
In my example I have added mass action in existing Tax Rates Grid which can delete a large number of tax rates, not just delete them one by one.
code for Mage_Adminhtml_Block_Tax_Rate_Grid class:
protected function _prepareMassaction()
{
$this->setMassactionIdField('tax_calculation_rate_id');
$this->getMassactionBlock()->setFormFieldName('tax_id');
$this->getMassactionBlock()->addItem('delete', array(
'label'=> Mage::helper('tax')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete', array('' => '')), // public function massDeleteAction() in Mage_Adminhtml_Tax_RateController
'confirm' => Mage::helper('tax')->__('Are you sure?')
));
return $this;
}
Your grid will look like this:
Extend your controller with an action which will handle mass delete
code for Mage_Adminhtml_Tax_RateController class:
public function massDeleteAction()
{
$taxIds = $this->getRequest()->getParam('tax_id'); // $this->getMassactionBlock()->setFormFieldName('tax_id'); from Mage_Adminhtml_Block_Tax_Rate_Grid
if(!is_array($taxIds)) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('tax')->__('Please select tax(es).'));
} else {
try {
$rateModel = Mage::getModel('tax/calculation_rate');
foreach ($taxIds as $taxId) {
$rateModel->load($taxId)->delete();
}
Mage::getSingleton('adminhtml/session')->addSuccess(
Mage::helper('tax')->__(
'Total of %d record(s) were deleted.', count($taxIds)
)
);
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
Note: Do not modify Magento’s core files. Override Mage_Adminhtml_Block_Tax_Rate_Grid with your block and extend it with the “protected function _prepareMassaction()” and do the same thing with Mage_Adminhtml_Tax_RateController by adding “public function massDeleteAction()”.
Enjoy coding!
21 comments
This is best tutorial and very much helpful to me..
How to add another action in the same grid, which is hidden and appears on particular selection of the previous action?
The post really enjoyable. 🙂 (y)
Thanks! works great..
I am getting 404 on delete action , what to do here ?
I’ve read this tutorial, and i have a question. This tutorial shows me how to create massDeleteAction, but how if i want to create massSaveAction that fetch all data from grid and save it to database. Any Ideas?
Thanks. This help me a lot. One thing I had to change (using 1.13.1.0) was to use
instead of
in controller
Hi!
I’m writing an extension that as part of it’s functionality adds a tab to the product edit screen. That tab ajax-loads a magento grid, much like the “Related Products” one. Everything works fine, up to the point where I would like to add a mass action, namely “Delete”. The mass action bar gets displayed correctly, together with the checkboxes and all the other stuff. However, when selecting the mass action from the dropdown and pressing “Submit”, a JS error is thrown, telling me that “grid_name_massactionJsObject is not defined”. This is because as part of the grid’s template, some JavaScript is supposed to be output after the grid’s body, which should initialize this object. I can see the code in the template. It is inside the condition “if($this->canDisplayContainer())”, which, when var_dump’ed, returns true. The javascript itself, however, either does not get output at all, or get’s somehow removed later on. This javascript is critical for the mass action to work. Any ideas?
thanks…it helped me in mass Delete for coupon codes
Hi,
How can is post data to thet massaction controller ??
Any wiki or tip is appreciated..
Many thanks..
This code no longer works in 1.7. The first part does. I got the checkboxes and the action dropdown to appear, but the MassDelete Action function itself is not working for me. I copied Ratecontroller.php into my local folder following the same folder structure as in Core.
Confirming the mass delete action give me 404 on this page:my domain.com/index.php/admin/tax_rate/massDelete/key/505c8e2da1326579fe8d88062b3c75d1/
What needs to change?
In case you need in bulk to change status, visibility, stock level, assign products to categories or web-sites, it is possible in bulk with Store Manager for Magento, as it has built-in tool for mass changes – Multi-Editor. Using this tool there is no need to export/import the data.
Read more on Multi Editor functionality – http://www.mag-manager.com/useful-articles/additional-features/magento-multi-editor
@Shaaaaa
Add:
protected function getNoFilterMassactionColumn(){
return true;
}
to your gird.php file that extends Mage_Adminhtml_Block_Widget_Grid
Great post, as usual, just tested and is working flawlessly
You can also use event-observer method for adding new mass action using event called core_block_abstract_prepare_layout_before
For more, please visit the following blog article:
http://www.blog.magepsycho.com/adding-new-mass-action-to-admin-grid-in-magento/
Thanks
Great artical, @Sdam Moss and Shaaa,
I suggest looking at the sorce code in adminhtml. Theres doezens of examples.
Is there anyway to remove the filtering drop down menu in that mass action drop down. I mean i want to remove this “Any”,”Yes” and “No” option.
@Adam Moss: exactly its a nightmare. I second your suggestion mate
Would you guys care to write a brief summary on how to generally use grids in the admin area? It’s a right pain in the ass for me at the moment!
where in the files do i add this code?
thanks in advance
I believe that we have an article which describes you how to use custom adminhtml blocks, you just need to dig it 🙂
I am having problems writing the config.xml to use my custom adminhtml blocks. Could i see the code behind this? Thanks!