Cgridview Sorting Resets The Filters

i am displaying set of records using cgridview and i have given sorting for each coloumn available in grid. [size="2"]The sorting works fine with Ajax. [/size]

now i have a search field, which takes the date as a input and filter the cgrdview data records.

assume now i am having the filtered data’s in grid view and sorting them. what happens is, [color="#8b0000"] the sorting works but my data set in gridview is rested to normal which will now list all the data records without any filter…[/color]

in other words,

in my gridview i have [color="#8b0000"]100 records[/color] by default. now i am sorting them and they work fine with all sorting ways.

now i am applying a filter by giving a created data which i expect data to be shown on that date. so the filter applies correctly and my data records are as appropriate. now have only [color="#8b0000"]20 records[/color] which is expected.

now i am sorting again… what happens is, sorting happens but i get back all the records ([color="#8b0000"]100 records[/color]). :(

why does this happens ? can somebody help me out to fix this. i am quiet running out of time.

helps are highly appreciated.


It won’t happen if you use gridview native filter inputs.

By looking at jquery.yiigridview.js you can see that inputSelector just take data from gridview’s filters so if you use your own inputs, after filtering or sorting by gridview’s internal functionality you will lose your filters , to solve it you have some options:

first option is to keep your own filters and use it on further actions.(for example you can keep your model in a session variable)

second an in my opinion a better option is to extend gridview and customize it’s inputSelector in js file to recognize your own filter inputs as gridview’s internal filter inputs

for example you can change it to

inputSelector = '.' + settings.filterClass + ' input, ' + '.' + settings.filterClass + ' select';

this way if you put your filter inputs in a div that has filterClass (it’s default value is ‘filters’ ) it works as gridview internal filter inputs

Thanks for your reply Reza.

I am using sessions to keep the filters but i have just figured out that my session variables are empty which means they are not set.

wondering why is that… is it because i have two model variables additionaly created and they are set to public.

below is the function i am using to assign the values to sessions.

   public function setStateAttributes() {

      $attrState = array();

      foreach ($this->getAttributes() as $key=>$value) {

     	if (!empty($value))

            $attrState[$key] = $value;


      Yii::app()->user->setState('searchFields', $attrState);


here is a thread which includes the same issue -

so you can do something like this:

$model=new YourModel('search');

$model->unsetAttributes();  // clear any default values










Thanks Reza.

I looped around the post array and assigned them into a session if they are set. ;)


    * To set all attributes in model

    * Note: getAttributes() method doesnt support for additional variables

    * created in model.


    * @param post array $data


   public function setStateAllAttributes($data) {

      $attrState = array();

      foreach ($data as $key=> $value) {

         if (!empty($value))

            $attrState[$key] = $value;


      Yii::app()->user->setState('searchFields', $attrState);



[size=2]Correction - we can pass an array including the additional attributes we created in model to get those values as well.[/size]

[size=2]$arr = array(‘cusAttr1’,‘cusAttr2’);[/size]





Hi forum!

Here is my solution to keep filters and sorting while paging and sorting.

Note that I do not use Sessions but pass GET params.

Just have added some code to the search() function:

	public function search(){

            $criteria=new CDbCriteria;


… … ...

            $criteria->compare('data_content',$this-> data_content,true);




… … ...




            $dp=new CActiveDataProvider(get_class($this), array(








        foreach($directions as $key=>$val){

            if($val==1){$sort.=$key.$sep[1].'desc'.$sep[0];} else {$sort.=$key.$sep[0];}



        if($dp->sort->multiSort==false){//remove all after and including $sep[0] 

            $tmpStr = explode($sep[0],$sort, 2);


        } else {//remove last char $sep[0]

            $sort=substr($sort, 0, -1);



            return $dp;	



Appreciate if there are any others documented methods for this.