Yii 1.1: Saving CGridView filter

32 followers

In this tutorial we will try to save cgridview filter(search) with title etc. basically we want to save two forms data i.e one represents filters and other data about filters like title, description etc. You can define your own structure for filter saving table here are some basic steps

Step -1: create table according to requirements for filters ,in my case save_filter

Step-2: in admin within controller

$saveFilter=new SaveFilters;
…
$this->render('admin',array('saveFilter'=>$saveFilter,
));

Step-3: render form

$this->renderPartial('_saveFilters', array(
            'model' => $saveFilter,
 ));

Step-4: write scripts for getting data from both forms

// #save-filters-form & #SearchFormNew are form IDs
Yii::app()->clientScript->registerScript('filter', "
 
$('.filter-form form').submit(function(){
      jQuery.ajax({
            'success': function(data) {
                 $('#FilterStatus').html(data);
            },
            'type': 'POST',
            'data': $('#save-filters-form,#SearchFormNew').serialize(), 
            'url': '" . $this->createUrl('/mycontroller/saveFilters') . "',
 
        });
    return false;
});
 
", CClientScript::POS_READY);

Step-5: get in controller

public function  actionSaveFilters(){
 
       // your data structure
       if(isset($_POST['SaveFilters'])) print_r($_POST['SaveFilters']);
       // define table (fields or json format,text or different)
       // Generate Crud 
       // feed data  and show some message or redirection 
       // load data when needed
       // you can use $criteria = new CDbCriteria; loading filters in admin when filter is apllied
}

Step-6: load your filter when applied :P

Total 5 comments

#18187 report it
rackycz at 2014/09/24 02:20am
And how about this approach?

As I already wrote many times, I'm friend of very simple native solutions. What do you think about this?

http://www.yiiframework.com/wiki/462/yii-for-beginners-2/#hh10

#17884 report it
vijay p s at 2014/08/06 02:43am
Doubt!

Can u please explain the Step 3rd and 4th.

Like were we use the code?

#17454 report it
Tavi at 2014/06/14 07:48am
another way

why not just save the filter data in your session? this is what i do and it works ok till now

public function actionAdmin()
    {
        $model=new Cartuse('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Cartuse'])) //are there new filters?
                    $_SESSION['filterData'][$this->id][$this->action->id] = $_GET['Cartuse']; //save them in session
                    $model->attributes=$_SESSION['filterData'][$this->id][$this->action->id];  //apply filters from session
 
        $this->render('admin',array(
            'model'=>$model,
        ));
    }

this way you don't need another object. i think is faster, it resets on login / logout or session destroy / expire, and like so you can save all your model's filters in any and all controllers. works whit afterajax update, or whit pagerefresh, etc

#16282 report it
sefburhan at 2014/02/06 03:46am
@peppe

as its clear that i am saving form selected fields in db with filter data like filter title,description and reload when somebody select filter and applying all those conditions in admin() when load dataprovider for cgridview. this is just general form , can u tell me your problem specific? b/c from step 1 to 6 i have explained with example which i used for my self!

#16281 report it
Peppe at 2014/02/06 03:01am
example needed

Sorry but it's not so clear how can I implement it. Can you post a complete example? I think it will be helpful.

One more thing. Is it possible to save sort setting too?

Leave a comment

Please to leave your comment.

Write new article