Yii 1.1: How to keep filters simple in CGridView

2 followers

The problem is not new: after setting some filters in a grid (typically in admin view), and gone to another page, returning in the grid's page I would like to find the filters with previous setting mantained.

There are extensions doing that (and more) but I always look for the simplest solution. So, thanking some users that in various contributes approach the problem, this is the simplest way I found.

Keep is simple

The admin action has usually this structure, where the render command calls a view containing the CGridView widget ("Fornitore" is the model):

public function actionAdmin() {
    $model = new Fornitore('search');
    $model->unsetAttributes();
 
    if (isset($_GET['Fornitore']))
        $model->attributes = $_GET['Fornitore'];
 
    $this->render('admin', array(
        'model' => $model,
    ));
}

The added code to obtain the result is just a few lines, as follows:

public function actionAdmin() {
    $model = new Fornitore('search');
    $model->unsetAttributes();
 
    if (isset($_GET['Fornitore'])) {
        $model->attributes = $_GET['Fornitore'];
        // stores filters setting in user's variable
        Yii::app()->user->setState('filtroFornitore', $_GET);
    } else {
        if (isset(Yii::app()->user->filtroFornitore) {
            // restores the previous stored filters setting
            foreach (Yii::app()->user->filtroFornitore['Fornitore'] as $campo => $valore) {
                $model->$campo = $valore;
            }
        } else {
            // optional default filters
            $model->attivo = 1;
        }
    }
 
    $this->render('admin', array(
        'model' => $model,
    ));
}

The settings are stored in a persistent user session variable; customizing the name of the variable with model name, it's possible to use the mechanism in several models pages at the same time.

Using a user session variable, it's lost when user logoff, so there are no problem in multiuser environment. More, if you want to restore filters setting through sessions, I think it's simple to store them in database (but I didn't try it).

The "optional default filters" let you decide to set some filters as default when the page is open for first time.

One step ahead

Some more to let clearing filters simpler too. Just a little action, and a link somewhere in the grid's page to call it.

// this in the controller...
public function actionPulisciFiltro() {
    if (isset(Yii::app()->user->filtroFornitore)) {
        unset(Yii::app()->user->filtroFornitore);
    }
    $this->redirect(array('admin'));
}
 
// ...and this in the view
echo CHtml::link('Pulisci filtri', Yii::app()->controller->createUrl('pulisciFiltro'));

Destroying the variable, it restores the initial situation, clearing filters and setting, if present, the default values too.

It's all. Hope this is useful.

Total 3 comments

#19531 report it
Kavitama at 2015/08/25 02:50pm
KISS - Very good Peppe!

The truth is keep things simple (KISS = keep it simple stupid!) Peppe's solution is simple and useful, I have been using the same extension and always fought with the fact I was using TbGridView instead o CGridView.

Once more simple things are the best!

#18588 report it
Peppe at 2014/11/22 06:10pm
Re: Using an extension is simple too

@ le_top Sure, you're right. I've nothing against extensions. I just need to have the control of the code, because of some particular behaviors (combined filters mainly).

Thanks for revision; my english is so bad.

#18586 report it
le_top at 2014/11/22 01:56pm
Using an extension is simple too

Hi

I use 'ERememberFiltersBehavior' and using it is simple too and what is more, I do not need to think about it anymore because I updated my Gii template.

What you need is:

class MyModel extends CActiveRecord {
    public function behaviors() {
        return array(
            'ERememberFiltersBehavior' => array(
                'class' => 'ext.ERememberFiltersBehavior.ERememberFiltersBehavior',
                'defaults'=>array(),
                'defaultStickOnClear'=>false,
            )
         );
    }
}

The extension is more complex, but the use of it is simple.

Leave a comment

Please to leave your comment.

Write new article
  • Written by: Peppe
  • Updated by: le_top
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +1 / -1
  • Viewed: 6,708 times
  • Created on: Nov 22, 2014
  • Last updated: Nov 22, 2014
  • Tags: CGridView, filtering