Yii Framework Forum: Sorting and Pagination does not work after I build a custom keyword search that is build using relational tables - Yii Framework Forum

Jump to content

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

Sorting and Pagination does not work after I build a custom keyword search that is build using relational tables Sorting and Pagination does not work after I build a custom keyword se Rate Topic: -----

#1 User is offline   YIINewBy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-September 09
  • Location:Pretoria, South Africa

Posted 21 March 2010 - 06:30 AM

I recently started to build a custom keyword search using Yii 1.1.x

The search works 100%. But as soon as I sort the columns and use pagination in the admin view the search gets lost and all results are shown. So with otherwords it's not filtering so that only the search results show. Somehow it resets it.

In my controller my code looks as follows

 $builder=Messages::model()->getCommandBuilder();
    		
    		//Table1 Columns
    		$columns1=array('0'=>'id','1'=>'to','2'=>'from','3'=>'message','4'=>'error_code','5'=>'date_send');
    		
    		//Table 2 Columns
    		$columns2=array('0'=>'username');
    		
    		//building the Keywords
    		$keywords = explode(' ',$_REQUEST['search']);
    		$count=0;
    		foreach($keywords as $key){
    			$kw[$count]=$key;
    			++$count;
    		}	
    		
    		$keywords=$kw;
    	
    		$condition1=$builder->createSearchCondition(Messages::model()->tableName(),$columns1,$keywords,$prefix='t.');
    		$condition2=$builder->createSearchCondition(Users::model()->tableName(),$columns2,$keywords);
    		$condition = substr($condition1,0,-1) . " OR ".substr($condition2,1);
    		$condition = str_replace('AND','OR',$condition);
    
    
    $dataProvider=new CActiveDataProvider('Messages', array(
    				'pagination'=>array(
    					'pageSize'=>self::PAGE_SIZE,
    				),
    				'criteria'=>array(
    					'with'=>'users',
    					'together'=>true,
    					'joinType'=>'LEFT JOIN',
    					'condition'=>$condition,
    				),
    				
    				'sort'=>$sort,
    			));
    
    $this->render('admin',array(
    			'dataProvider'=>$dataProvider,'keywords'=>implode(' ',$keywords),'sort'=>$sort
    		));

and my view looks like this

$this->widget('zii.widgets.grid.CGridView', array(
    	'dataProvider'=>$dataProvider,
    	'columns'=>array(
    		'id',
    		array(
    			'name'=>'user_id',
    			'value'=>'CHtml::encode(Users::model()->getReseller($data->user_id))',
    			'visible'=>Yii::app()->user->checkAccess('poweradministrator')
    		),
    		'to',
    		'from',
    		'message',
    		/*
    		'date_send',
    		*/
    		array(
    			'name'=>'error_code',
    			'value'=>'CHtml::encode($data->status($data->error_code))',
    		),
    		array(
    			'class'=>'CButtonColumn',
    			'template'=>'{view} {delete}',
    		),
    		
    		
    	),
    	
    ));



I really do not know what do do anymore since I'm terribly lost, any help will be hihsly appreciated
0

#2 User is offline   Fran├žois Gannaz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 87
  • Joined: 24-November 09

Posted 22 March 2010 - 06:12 PM

Are you using AJAX? By default, the CGridView widget is in AJAX and it causes you to loose your search parameters in some cases (especially when navigating through the browser history).

You should try to disable it and see if your problem persists. In your view, make the search form visible, remove the javascript and add to your CGridView:
	'ajaxUpdate' => false


I attach a full view that has a trigger for AJAX. If the cause of your problem isn't in this, good luck!

Attached File(s)


-1

#3 User is offline   YIINewBy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-September 09
  • Location:Pretoria, South Africa

Posted 23 March 2010 - 01:49 AM

View PostFran├žois Gannaz, on 22 March 2010 - 06:12 PM, said:

Are you using AJAX? By default, the CGridView widget is in AJAX and it causes you to loose your search parameters in some cases (especially when navigating through the browser history).

You should try to disable it and see if your problem persists. In your view, make the search form visible, remove the javascript and add to your CGridView:
	'ajaxUpdate' => false


I attach a full view that has a trigger for AJAX. If the cause of your problem isn't in this, good luck!



I'll check this out, looks promising
0

#4 User is offline   YIINewBy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-September 09
  • Location:Pretoria, South Africa

Posted 24 March 2010 - 12:13 PM

View PostYIINewBy, on 23 March 2010 - 01:49 AM, said:

I'll check this out, looks promising



Okey I managed to find out what the problem is here, it's not sending the search values through to the next page using if I save the search values in a session the pagination and sorting works but this is not probably not the right way.

Maybe setData has something to do with this

eg.
$dataProvider=new CActiveDataProvider('Messages', array(
				'pagination'=>array(
					'pageSize'=>self::PAGE_SIZE,
				),
				'criteria'=>array(
					'condition'=>'user_id='.Yii::app()->user->u_id.' '.$condition,
				),
				'sort'=>$sort,
				
			));

$dataProvider->setData(????);

0

#5 User is offline   luoshiben 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 15-January 09

Posted 24 March 2010 - 10:50 PM

I solved the problem by storing my criteria and sort objects in the session after they were set. Then, if the action method was called via ajax request, I reloaded them from the session before rendering the dataprovider. Here's the forum post: http://www.yiiframew...ith-cgridview/. Good luck!
0

#6 User is offline   YIINewBy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-September 09
  • Location:Pretoria, South Africa

Posted 25 March 2010 - 02:07 AM

View Postluoshiben, on 24 March 2010 - 10:50 PM, said:

I solved the problem by storing my criteria and sort objects in the session after they were set. Then, if the action method was called via ajax request, I reloaded them from the session before rendering the dataprovider. Here's the forum post: http://www.yiiframew...ith-cgridview/. Good luck!



Thanx, I also did it this way and it works!!
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