Magento Certified Developer GuideCategory: 6. AdminhtmlHow can grid filters be customized
Manish Prakash Staff asked 5 years ago

We can set default filter to grid using

$this->setDefaultFilter('column=value');

or

$this->setDefaultFilter(array(
  'column1' => 'value1',
  'column2' => 'value2'
));

This basically sets default filters for grid, when a grid is loaded these filters are always applicable.
These are the two functions used to set grid filters

    protected function _setFilterValues($data)
    {
        foreach ($this->getColumns() as $columnId => $column) {
            if (isset($data[$columnId])
                && (!empty($data[$columnId]) || strlen($data[$columnId]) > 0)
                && $column->getFilter()
            ) {
                $column->getFilter()->setValue($data[$columnId]);
                $this->_addColumnFilterToCollection($column);
            }
        }
        return $this;
    }

    protected function _addColumnFilterToCollection($column)
    {
        if ($this->getCollection()) {
            $field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
            if ($column->getFilterConditionCallback()) {
                call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
            } else {
                $cond = $column->getFilter()->getCondition();
                if ($field && isset($cond)) {
                    $this->getCollection()->addFieldToFilter($field , $cond);
                }
            }
        }
        return $this;
    }