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:
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.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 typein_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.Where Magento gets the selected products looks like the following.
The column itself is simply added with the rest of the columns in the grid with the following lines.
So to summarise we need to do the following.
_addColumnFilterToCollection
check to see if the column is ourin_products
column and add our custom filter.I hope this helps with your development.