Mauserrifle.nl Tech and Life

    Home     Archive     Projects     Contact

Magento admin grid filter values

Getting the filter values from an grid within the Magento admin can be a tricky task and is not very straight forward. In my case I required these values for a custom order export type to build a proper file name. After digging around in the code I found a way.

The following code can be used within a custom controller to fetch a simple array of all the chosen filters within a grid (sales_order_grid as example):

// Get a grid
$grid = $this->getLayout()->createBlock('adminhtml/sales_order_grid');

// Get filter key from this grid block
$filter = $grid->getParam($grid->getVarNameFilter(), null);

// Get data using the filter key
$data = $grid->helper('adminhtml')->prepareFilterString($filter);

If you would be extending an existing grid class instead of calling one from the controller, for example one that extends Mage_Adminhtml_Block_Sales_Order_Grid.

Just change $grid in $this and use the following code within that grid class:

$filter = $this->getParam($this->getVarNameFilter(), null);
$data = $this->helper('adminhtml')->prepareFilterString($filter);

Cheers!

If you liked this post, you can share it with your followers!