Magento 1.8 – Keep Mass Action Checkboxes Checked by Default

admingridmagento-1.8massaction

In my Grid.php of custom module, I added the following code (excerpt) to enable mass action.

protected function _prepareMassaction(){
    $this->setMassactionIdField('entity_id');
    $this->getMassactionBlock()->setFormFieldName('some_thing');
    // options code here
    return $this;
}

I want the checkboxes of mass action to be checked by default for some items based on a variable which holds the field entity_id of the items in an array.

I am trying to show the checked items as shown in the below image even after page reload:

enter image description here

How to do this?

Best Answer

If I have understood what you want, which I hope I have. You are looking for a filtered list of products where there is a checkbox for associated product that will be checked when associated and will stay checked even on page reload etc.

For this I would suggest looking at how Magento itself does it and the best place to see this is in the Associated products tab on the product edit screen in the admin section.

Firstly how does Magento assign the current selection to the gird. Well we can see this in the construct of Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Super_Group with the following lines.

if ($this->_getProduct()->getId()) {
    $this->setDefaultFilter(array('in_products'=>1));
}

Here Magento checks for a currenlty set product, Mage::registry('current_product');, and if we have one then is sets some default filters to the grid.

Now lets look a bit into these filters. It sets that the filter in_products => 1 but what does that actually mean. Well we can see it being used in the function _addColumnFilterToCollection where here it checks the column for type in_products and when this is set then it will get the selected products and add them as filters. If you follow were this function is called it should be called at some point as part of the _prepareCollection code.

$productIds = $this->_getSelectedProducts();
if (empty($productIds)) {
    $productIds = 0;
}
if ($column->getFilter()->getValue()) {
    $this->getCollection()->addFieldToFilter('entity_id', array('in'=>$productIds));
}
else {
    $this->getCollection()->addFieldToFilter('entity_id', array('nin'=>$productIds));
}

Where Magento gets the selected products looks like the following.

protected function _getSelectedProducts()
{
    $products = $this->getProductsGrouped();
    if (!is_array($products)) {
        $products = array_keys($this->getSelectedGroupedProducts());
    }
    return $products;
}

The column itself is simply added with the rest of the columns in the grid with the following lines.

$this->addColumn('in_products', array(
    'header_css_class' => 'a-center',
    'type'      => 'checkbox',
    'name'      => 'in_products',
    'values'    => $this->_getSelectedProducts(),
    'align'     => 'center',
    'index'     => 'entity_id'
));

So to summarise we need to do the following.

  1. Add the column as if it is a normal column in the grid,
  2. On construct set the default filters,
  3. During the _addColumnFilterToCollection check to see if the column is our in_products column and add our custom filter.
  4. ???
  5. PROFIT!!!

I hope this helps with your development.