cjuiautocomplete problem

Little problem here: I use cjuiautocomplete and an url that returns json data for the feed (same host). The autocomplete only gives a full list of city names…so when I type: “Gelu…” he won’t show me the city names starting with “gelu”, but instead the full list. Anyone knows a fix?

controller:


class ApiController extends Controller

{

	public function actionCity()

	{

		echo CJSON::encode($this->extractCities());

	}

view:


<?php		

                $this->widget('zii.widgets.jui.CJuiAutoComplete', array

                    (

                        'attribute' => 'city_id',

			'model'=>$model,

                        'source'=>$this->createUrl('/api/city'),

                        // additional javascript options for the autocomplete plugin

                        'options' => array(

                            'minLength' => '3',

                        ),

                        'htmlOptions' => array(

                            'maxlength' => 128,

                        ),

                    ));

                 ?>

json files looks like this:


[{"value":"1","label":"Antwerpen 2000"},{"value":"2","label":"Antwerpen 2018"}, ......

thx!

Can you post your extractCities() method? I think that’s where your problem is. Sounds like your search method/criteria isn’t processing the posted term.

But if I put my array with cities directly into the ‘source’, everything works perfect… So strange :P


	private function extractCities(){

		return Yii::app()->db->createCommand()

			->select('id AS value, CONCAT(name," ", code) AS label')

			->from('city')

			->queryAll();

	}

Yes, you’re not using the posted form value at all for your query of the cities.

You need to add a condition/where clause that checks whether the "label" matches the posted string.

I don’t really understand this answer, can you give an example?

view:


<?php		

                $this->widget('zii.widgets.jui.CJuiAutoComplete', array

                    (

                        'attribute' => 'city_id',

			'model'=>$model,

                        'source'=>Yii::app()->db->createCommand()

			->select('id AS value, CONCAT(name," ", code) AS label')

			->from('city')

			->queryAll(),

                        // additional javascript options for the autocomplete plugin

                        'options' => array(

                            'minLength' => '3',

                        ),

                        'htmlOptions' => array(

                            'maxlength' => 128,

                        ),

                    ));

                 ?>

if i change the view to this, it works perfect… so the query should work? But remotely…

Thanks in advance Dana!

No, it’s because when the array provided in advance, searches it client side. When you use an external action, you’re telling it that you’re going to process the list of answers yourself. This lets you do much more complex searching etc. than you can do by default.

This is an example from an autocomplete lookup we use:




	/**

	 * @param string $term The string to match (automatically sent by the cjuiautocomplete widget)

	 * @param int $limit  

	 */

	public function actionAutoComplete( $term, $limit = 50 )

	{

		// build the query criteria

		$countryCriteria = new CDbCriteria();

		$countryCriteria->condition = "Country LIKE '$term%'";

		$countryCriteria->order = "Country ASC";

		$countryCriteria->limit = $limit;


		// get the possible options

		$countries = Country::model()->findAll( $countryCriteria );

		foreach( $countries as $country )

		{

			$returnVal[] = array('label'=>$country->Country,'id'=>$country->CountryId);

		}


		// output as json for the autocomplete to render

		echo CJSON::encode($returnVal);

	}



I tried your code as follows:


public function actionCity()

	{

                $cities = City::model()->findAll();

                foreach( $cities as $city )

                {

                        $returnVal[] = array('label'=>$city->name,'id'=>$city->id);

                }

                echo CJSON::encode($returnVal);

	}

Still the same behavior:

)

As you see, he still shows every city starting with the "A" while I typed "Merks…"

what a mystery :rolleyes:

No mystery =)

Youre still pulling in ALL the models:

You need to:

Thanks Dana

what is the $term variable? it has to be defined somewhere?

Plus, i want to pull ALL the models, the autocomplete module must know all the cities, no?

ps: cdbcriteria doesn’t have a select method.

Really thanks for your time!

Sorry, that should be $criteria->select = “blah” rather than in the () … I went too fast ;)

If you’re just using the name property though, you should just ignore the select and if you want to add the code to the string, do so when you build the output array.

$term is posted by the CJuiAutoComplete widget and is the string that you typed in the box. In the example I posted, we’re using action parameter binding to require the term and pull it in from the $_GET array as part of the action method definition. Make sense?

It doesn’t need to know all the cities all the time, it just needs to know the ones that matched what you sent it to find.

Then, next time you type, it will look up what matches again.

yessssssss it isssssssssssssssss! Yaaaaaaaaaaaaaaaaaa

Thanks Dana, It works!

You’re an angel! :lol:

Glad to help =)