Another requirement for my forum app is to use auto completion in search. I'm writing a simple JSON action to take the query string from the text field, how do I query my action? and how do I bind this result with the CAutoComplete-widget?
Another requirement for my forum app is to use auto completion in search. I'm writing a simple JSON action to take the query string from the text field, how do I query my action? and how do I bind this result with the CAutoComplete-widget?
If I correctly understand the CAutoComplete-class, you simply have to create a Widget and set the url-property (http://www.yiiframework.com/doc/api/CAutoComplete#url) to your JSON-Action.
But I can't test it now.
Below is how we use CAutoComplete in www.yiiframework.com/doc/api:
view:
<?php $this->widget('CAutoComplete', array(
'model'=>$form,
'attribute'=>'keyword',
'minChars'=>2,
'max'=>100,
'url'=>'/doc/api/suggest',
'htmlOptions'=>array('size'=>'50'))); ?>
Action:
<?php
public function actionSuggestApi()
{
if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
{
$results=$this->getApiSuggestions($_GET['q']);
echo implode("n",$results);
}
}
protected function getApiSuggestions($search)
{
$search=strtolower($search);
$results=array();
foreach($this->getApiKeywords() as $keyword)
{
if(strpos(strtolower($keyword),$search)!==false)
$results[]=$keyword;
}
return $results;
}
[/code]
Sweeeeeet!
Just had to create my own SearchForm (CFormModel) and point the ‘url’-property to the right action with CHtml::normalizeUrl();
When I have a look at the generated source I see there's quite a lot of JS and CSS files loaded when dealing with such an AJAX widgets (like the CAutoComplete and others), I see there is some discussion on this in another thread and an issue for it so I guess it will be some options to stuff these files together in a controlled way when you want to optimize the application for production use.