Saving CGridView filter

You are viewing revision #2 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#1)

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