Select2 Infinite Scroll With Remote Data in Yii

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

I was able to set up the infinite scroll of select2 in Yii. I am glad to share it with you. The demo and source code can be found here. However, select2 for Yii can be found here which uses a widget.

The controller action is shown below

public function actionSearch() 
			if (isset($_GET['term'])&& !empty($_GET['term'])) 
				$search = "SELECT SQL_CALC_FOUND_ROWS pm.title as text, pm.poem_id as id,bio.pix as pix,n.penname as name FROM pty_poem pm
				LEFT JOIN pty_biodata bio ON pm.poet_id = bio.poet_id LEFT JOIN pty_name n ON pm.poet_id = n.poet_id  WHERE category IN ( Select cat.value from pty_category cat where cat.category LIKE CONCAT('%', :title, '%')) Limit 5 ";
				$command = Yii::app()->db->createCommand($search);
				$command->bindValue(":title", '%'.$_GET['term'].'%', PDO::PARAM_STR);
				$result =$command->queryAll();	
				$total_rows = Yii::app()->db->createCommand('SELECT FOUND_ROWS() AS total')->queryRow();				
				$total = $total_rows['total'];

				 echo CJSON::encode(array('result'=>$result,'total'=>$total));
		 } else
				throw new CHttpException(403, "The requested page does not exist");    	

The SQL_CALC_FOUND_ROWS is a MySQL function that selects all the rows that can be found given that there is no limit. But it does not give you the result. Another query has to be executed with FOUND_ROWS() to retrieve the found rows executed before it. This is needed in order to know the total number of rows searchable for any search string. The client code would use this to know if it should continue sending request for more pages.

Assuming that you downloaded and installed the Yii select2, you can have this below

		echo CHtml::textField('test','',array('id'=>'test','style'=>'height:30px;width:350px;'));					
                           'selector' => "#test",
                           'options'  => array(                              
                               'placeholder'        => 'Search poems by category',
                               'quietMillis' => 200,
                               'ajax'               => array(
                                   'url'      => $this->createUrl("poem/search"),
                                   'dataType' => 'json',
                                   'data'     => 'js: function(term,page) {
                                                        return {
                                                            term: term,
															page: page,
                                                          page_limit: 5,
                                   'results'=>'js:function (data, page) {
           			 					var more = (page * 5) <;          
            							return {results: data.result, more: more};							
                               'formatResult'=>'js:function(movie) {
			                   var folder = "'.Yii::app()->request->baseUrl.'/images/";   
            var markup = "<table class=movie-result><tr>";
			if (movie.pix !== undefined) {
			markup += "<td class=movie-image><img class=image-size src="+folder+movie.pix +" /></td>";
				markup += "<td class=movie-image><img class=image-size src="+folder+"avater.png" + " /></td>";
			markup += "<td class=movie-info><div class=movie-title>" + movie.text + "</div><div class=author-name>by ""</div>";
            markup += "</td></tr></table>"
            return markup; 
                               'formatSelection'=>'js:function(data) {                                   
								top.location = "'.$this->createUrl("poem/view").'"+"/"
									return data.text;
								'dropdownCssClass'=>"bigdrop", // apply css that makes the dropdown taller
								'escapeMarkup'=>'js:function (m) { return m; }',
								'formatNoMatches'=>'js:function () { return "No category match"; }',		 

I customized the look and feel with a little css to suite my taste. It's not bad to show you

<style type="text/css">

The page_limit is 5. There should have been a way of dynamically including it in the action but I'm trying to avoid sql injection. It needs to some param binding. I welcome any suggestion on. that. Clicking any highlighted selection would take you to a view of it ( e.g /poem/view/id/89)