How to add massactions to Magento’s grid

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!

You made it all the way down here so you must have enjoyed this post! You may also like:

Magento Grid Serializer for Ajax Grids Damir Korpar
Damir Korpar, | 10

Magento Grid Serializer for Ajax Grids

Show product thumbnail in grids Petar Sambolek
Petar Sambolek, | 14

Show product thumbnail in grids

Filter order grid by multiple ID’s Petar Sambolek
Petar Sambolek, | 14

Filter order grid by multiple ID’s

21 comments

  1. How to add another action in the same grid, which is hidden and appears on particular selection of the previous action?

  2. 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?

  3. Thanks. This help me a lot. One thing I had to change (using 1.13.1.0) was to use

    $this->getRequest()->getParam('massaction');

    instead of

     $this->getRequest()->getParam('tax_id'); 

    in controller

  4. 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?

  5. 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?

  6. @Shaaaaa

    Add:

    protected function getNoFilterMassactionColumn(){
    return true;
    }

    to your gird.php file that extends Mage_Adminhtml_Block_Widget_Grid

  7. Great artical, @Sdam Moss and Shaaa,
    I suggest looking at the sorce code in adminhtml. Theres doezens of examples.

  8. 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

  9. 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!

  10. I believe that we have an article which describes you how to use custom adminhtml blocks, you just need to dig it 🙂

  11. I am having problems writing the config.xml to use my custom adminhtml blocks. Could i see the code behind this? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.