Yii Framework Forum: Cgridview Sorting Resets The Filters - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cgridview Sorting Resets The Filters Rate Topic: -----

#1 User is offline   Azy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 156
  • Joined: 16-May 12

Posted 18 January 2013 - 01:58 AM

i am displaying set of records using cgridview and i have given sorting for each coloumn available in grid. The sorting works fine with Ajax.

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, the sorting works but my data set in gridview is rested to normal which will now list all the data records without any filter..

in other words,

in my gridview i have 100 records 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 20 records which is expected.

now i am sorting again... what happens is, sorting happens but i get back all the records (100 records). :(

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

helps are highly appreciated.

Thanks
0

#2 User is offline   Reza m 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 07-March 12

Posted 18 January 2013 - 03:08 AM

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
0

#3 User is offline   Azy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 156
  • Joined: 16-May 12

Posted 18 January 2013 - 04:03 AM

View PostReza m, on 18 January 2013 - 03:08 AM, said:

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 - http://www.yiiframew...tom-attributes/
0

#4 User is offline   Reza m 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 07-March 12

Posted 18 January 2013 - 04:18 AM

so you can do something like this:
$model=new YourModel('search');
$model->unsetAttributes();  // clear any default values
        
if(isset($_SESSION['YourModel']))
      $model=$_SESSION['YourModel'];
		
		
if(isset($_GET['YourModel']))
      $model->attributes=$_GET['YourModel'];
        
$_SESSION['YourModel']=$model;


1

#5 User is offline   Azy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 156
  • Joined: 16-May 12

Posted 18 January 2013 - 04:51 AM

View PostReza m, on 18 January 2013 - 04:18 AM, said:

so you can do something like this:
$model=new YourModel('search');
$model->unsetAttributes();  // clear any default values
        
if(isset($_SESSION['YourModel']))
      $model=$_SESSION['YourModel'];
		
		
if(isset($_GET['YourModel']))
      $model->attributes=$_GET['YourModel'];
        
$_SESSION['YourModel']=$model;




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);
   }



0

#6 User is offline   Azy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 156
  • Joined: 16-May 12

Posted 18 January 2013 - 06:52 AM

View PostLittle Man, on 18 January 2013 - 04:51 AM, said:

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);
   }




//------------------------------------
Correction - we can pass an array including the additional attributes we created in model to get those values as well.
$arr = array('cusAttr1','cusAttr2');


getAttributes($arr)

:)
0

#7 User is offline   Jay_69 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 27-August 10

Posted 18 April 2014 - 04:58 AM

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_id',$this->data_id);
… … ...
            $criteria->compare('data_content',$this-> data_content,true);

            $params=array(
                get_class($this)=>array(
                    'data_id'=>$this->data_id,
… … ...
                    'data_content'=>$this->data_content,
		)
            );

            $dp=new CActiveDataProvider(get_class($this), array(
                'criteria'=>$criteria, 
                    'sort'=>array('params'=>$params,'sortVar'=>'sort','defaultOrder'=>array('data_id'=>true)), 
                    'pagination'=>array('params'=>$params,'pageVar'=>'page','pageSize'=>100))
            );

        $sort='';
        $directions=$dp->sort->directions;
        $sep=$dp->sort->separators;
        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);
            $sort=$tmpStr[0];
        } else {//remove last char $sep[0]
            $sort=substr($sort, 0, -1);
        }

            $dp->pagination->params=$dp->pagination->params+array($dp->sort->sortVar=>$sort); 
            return $dp;	
	}
}


Appreciate if there are any others documented methods for this.
Regards.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users