The ERememberFiltersBehavior extension adds up some functionality to the default possibilites of CActiveRecord/Model implementation.
It will detect the search scenario and it will save the filters from the GridView. This comes handy when you need to remember them between navigation during page changes. For lot of navigation and heavy filtering, this functionality can be activated by just a couple of lines.
It supports default filter values and remember scenarios also. For example if you want to show only eg: Active Products, you can setup the default filter using this extension. Or if you have the same modal on different views, you can set different scenarios to remember state separated from each other. See the optional params under Advanced Functionalities and Scenarios section.
To use this extension, just copy this file to your components/ directory, add 'import' => 'application.components.ERememberFiltersBehavior', [...] to your config/main.php and paste the following code to your behaviors() method of your model
public function behaviors() { return array( 'ERememberFiltersBehavior' => array( 'class' => 'application.components.ERememberFiltersBehavior', 'defaults'=>array(), /* optional line */ 'defaultStickOnClear'=>false /* optional line */ ), ); }
Your actionAdmin() must not use unsetAttributes() as this was moved to the extension.
With this extension the actionAdmin instead of the classic
public function actionAdmin() { $model=new Company('search'); $model->unsetAttributes(); // clear any default values if(isset($_GET['Company'])) $model->attributes=$_GET['Company']; $this->render('admin',array( 'model'=>$model, )); }
can be as simple as:
public function actionAdmin() { $model=new Company('search'); $this->render('admin',array( 'model'=>$model, )); }
(if you use these functionalities please Donate)
'defaults' is a key value pair array, that will hold the defaults for your filters.
For example when you initially want to display active products, you set to array('status'=>'1').
You can of course put multiple default values in the array.
'defaultStickOnClear'=>true can be used, if you want the default values to be put back when the user clears the filters
The default set is false so if the user clears the filters, also the defaults are cleared out, the user will get an absolutely clean filter form. When true, if the form is cleared, he will get a form with defaults values.
(if you use these functionalities please Donate)
You can use scenarios to remember the filters on multiple states of the same model. This is helpful when you use the same model on different views and you want to remember the state separated from each other.
Known limitations: the views must be in different actions (not on the same view)
To set a scenario add the setRememberScenario call after the instantiation Sample code:
public function actionAdmin() { $model=new Company('search'); $model->setRememberScenario('scene1'); $this->render('admin',array( 'model'=>$model, )); }
This extension has also a pair Clear Filters Gridview
June 3, 2011
March 7, 2011
January 31, 2011
remember, filters, cgridview, gridview, store, reload, controller, model, behavior, interface, widget, stick, scenario
Total 12 comments
Hi, Lubos.
Can you explain / provide example?
Thank you.
Hi, I think you can pretty easily add support for remebering sorting order and active page in pager like so:
This should be placed into Model::search() function, whereas Model has two additional filter variables "sort" and "page".
Lubos
Would be great to persist de filter state in the database instead of the user session. This feature may enable the user to keep the prefered filters between sessions and between several computers.
@bas_vdl This extension is designed to remember filter's values, sorting and pagination are other layers and probably will need a separate extension.
it doesn't remember the page. using the latest vesion of yii...
@laqrhead You can alter the code of the extension and implement the gridview and model id. I will release in the upcoming days an update, that will include a configurable parameter to override the ID of the memory and other requested changes and bug fixes.
This is a great extension, thanks.
I have two different views that each have a CGridView for the same model (different layout). When I filter in one view, the values are then "remembered" in the other view. Do you know if there is a way to disable the "memory" in a specific CGridView? I quickly looked at the source, perhaps using the CGridView id instead of the modelName would work in the getState/setState? What do you think?
This is what I needed. Seems to be working fine.
That's exactly what I was looking for!!
@adinugro I've updated the extension to include support for defaults. Please setup the behavior in your model file, and there you can now set defaults. Donation is welcome for implementing this functionality in for you.
@jeremy: your idea is great, but how to handle model? should we also create parent model with the behavior method like above than inherits our model to this parent model?
I have a problem, my current actionAdmin() is
my idea is to return only active companies in the gridview. after using this extensions how could I achieve it? hoe could I set default status = active in my admin?
thx a lot
I added this behavior in the parent class for all my models; and then removed the actionAdmin from all my controllers and put it in the parent class (e.g. MyController) from which all my controllers descend. Now every admin page has this behavior. Quite simple.
Leave a comment
Please login to leave your comment.