I want to reset the admin grid filter programatically in my custom module.
-
can you please explain me detail which type so I will help you.– Keyul ShahCommented Jun 21, 2014 at 11:06
-
Please refer this magento.stackexchange.com/questions/24596/…– saravanaveluCommented Jun 21, 2014 at 11:08
-
did u understand that– saravanaveluCommented Jun 21, 2014 at 11:26
3 Answers
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);
}
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 thevalue
attribute
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);
}
}