0

I want to reset the admin grid filter programatically in my custom module.

3

3 Answers 3

3

Edit grid.php file in your custom module,

By default,

public function __construct()
{
  parent::__construct();
  $this->setId('productsGrid');
  // This is the primary key of the database
  $this->setDefaultSort('id'); 
  $this->setDefaultDir('ASC');
  $this->setSaveParametersInSession(true);
  $this->setUseAjax(true);
}

change the code to,

public function __construct()
{
  parent::__construct();
  $this->setId('productsGrid');
  // This is the primary key of the database
  $this->setDefaultSort('id'); 
  $this->setDefaultDir('ASC');
  $this->setSaveParametersInSession(false);
  $this->setUseAjax(true);
}
0

You can use jQuery / JavaScript to clear the field manually after every load of a collection.

Once the field is clear and a new value is entered it will search the whole collection again.

More specifically you can do the following steps:

  • Get the grid table via selector
  • Loop through all of the <th>'s
  • Find the <input> tags and clear the value attribute
0

The previous replies are not really replying to the question that is asking to reset the filters.
In my case, I didn't want to remove the ability to have the parameters into the session as this is a nice feature. I wanted to remove parameters ONLY if there are specific configurations in the URL that sends default data.

My approach was to remove data in the session directly.

public function __construct()
{
    parent::__construct();
    $this->setId('gridId');
    $this->setDefaultSort('updated_at');
    $this->setDefaultDir('DESC');
    $this->setUseAjax(true);
    $this->setSaveParametersInSession(true);

    if ($default = $this->getRequest()->getParam('default')) {
        $defaultFilters = [];
        // add script to add your personalized filters here

        $sessionParamName = $this->getId() . $this->getVarNameFilter();
        Mage::getSingleton('adminhtml/session')->setData($sessionParamName, '');
        $this->setDefaultFilter($defaultFilters);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.