There are quite some tutorials explaining how to add a custom admin grids. Unfortunately some of them are missing the fact that sorting and filtering functionality require additional controller action.
This action is specified by getGridUrl()
method of your grid class. The pitfall is that this method is defined in Mage_Adminhtml_Block_Widget_Grid
so if you forget to override it you may notice it until you try to sort or filter your grid. Mage_Adminhtml_Block_Widget_Grid::getGridUrl()
returns $this->getCurrentUrl()
which doesn't make much sense. So obvious move would be to override it with something like this:
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current'=>true));
}
The simple controller action that will just render a layout will wrap things up:
public function gridAction()
{
$this->loadLayout();
$this->renderLayout();
}
The normal behaviour for the grid checkbox is to bind a js event to it, and action the checkbox check/uncheck. Normally this would be to populate a hidden field with the selected values, and then that field is what gets posted to your controller action.
See varienGrid.prototype /js/mage/adminhtml/grid.js : (this.checkboxCheckCallback) and also setCheckboxChecked method in that class.
For this reason the checkbox element generally do not get a name assigned to it, which you can see if you view your element properties.
This is thus the reason why you cannot get the selected options in your post action, since elements with no name will not post in the form, and you most likely do not have the required callback set to deal with the check/uncheck of the box. Using the callback js, you could also effect other actions in your gui, to enhance the user experience.
However, looking at your usage requirements, you may just get away with setting a name for the checkbox element, which will then post the data to your action.
Thus if you adjust your element definition to this (adding in the 'field_name' property), you will get your data in an array called 'selectedproducts'
$this->addColumn('in_category', array(
'header_css_class' => 'a-center',
'type' => 'checkbox',
'values' => $this->_getSelectedProducts(),
'align' => 'center',
'index' => 'entity_id',
'name' => 'in_category',
'field_name' => 'selectedproducts[]'
));
Hope that helps.
Best Answer
On the webguys page is a german article about this problem: http://www.webguys.de/magento/turchen-23-pimp-my-produktgrid/
Translation:
Not all changes are made by magento, e.g. sorting and filtering of the new attributes doesn't work.
That's because they are made after the
_prepareCollection()
call, where the informations about the columns are passed to the collection.More in the post. I think the source code is quite self explaining and if not, use google (if you don't understand german ;-))