supernova
(Hernandes Sousa)
October 23, 2011, 10:07pm
1
Hi,
I’m trying to get the CJuiAutoComplete widget to work from Ajax datasource but Im’m not having much success (I can get it working from simple array thought).
I tried following this example from the Yii playground: www.yiiplayground.cubedwater.com/index.php?r=UiModule/jui/ziiAutocomplete .
I have a model class called Ci and I want to have the attibute ci_nome picked up for autocomplete as well as ci_id on a hidden filed
On my controller I have:
public function actionAutocompleteTest() {
$res =array();
if (isset($_GET['term'])) {
// http://www.yiiframework.com/doc/guide/database.dao
$qtxt ="SELECT ci_nome FROM {{ci}} WHERE ci_nome LIKE :nome";
$command =Yii::app()->db->createCommand($qtxt);
$command->bindValue(":nome", '%'.$_GET['term'].'%', PDO::PARAM_STR);
$res =$command->queryColumn();
}
echo CJSON::encode($res);
Yii::app()->end();
}
on the view:
<?php
echo "test";
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'test',
'source'=>$this->createUrl('ci/autocompleteTest')
)); ?>
I have tried different approaches to the method on the Controller from the internet but none succeeded…
Thanks
spidee
(Spiro)
December 19, 2011, 5:12pm
2
Are you able to get a the data output by typing the address directly in your browser? Otherwise for URLs you should use the ‘sourceUrl’ parameter and not the ‘source’ parameter.
<?php
echo "test";
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'test',
'sourceUrl'=>$this->createUrl('ci/autocompleteTest')
)); ?>
zu3i4ot
(Zu3i4ot)
December 20, 2011, 4:47am
3
here is what i am using to get the list of countries with name and id it will help you
in the controller
public function actionGetNames(){
if (!empty($_GET['term'])) {
$sql = 'SELECT id,name FROM countries WHERE name LIKE :qterm';
$sql .= ' ORDER BY name ASC';
$command = Yii::app()->db->createCommand($sql);
$qterm = $_GET['term'].'%';
$command->bindParam(":qterm", $qterm, PDO::PARAM_STR);
$models = $command->queryAll();
$arr = array();
foreach ($models as $model)
{
$arr[] = array(
'id'=>$model['id'],
'value'=>$model['name'],
'label'=>$model['name'],
);
}
echo CJSON::encode($arr); exit;
} else {
return false;
}
}
here is i used it in the form (i m using it in the Profile models form)
<div class="row">
<?php echo $form->labelEx($model,'country_id'); ?>
<?php
if(isset($_POST['Profile']['country_id'])){
$con_name = Countries::model()->findByPk($_POST['Profile']['country_id']);
if($con_name){
$con_name = $con_name->name;
}
}else if(isset($model->country_id)){
$con_name = Countries::model()->findByPk($model->country_id);
if($con_name){
$con_name = $con_name->name;
}
}else {
$con_name = '';
}
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'id'=>'country',
'name'=>'country',
'value'=>$con_name,
'source'=>$this->createUrl('countries/getnames'),// <- path to controller which returns dynamic data
// additional javascript options for the autocomplete plugin
'options'=>array(
'showAnim'=>'fold',
'minLength'=>'3', // min chars to start search
'select'=>"js:function(event, ui) {
jQuery('#Profile_country_id').val(ui.item.id);
jQuery('#Profile_country_name').val(ui.item.value);}",
'change'=>"js:function(event, ui) {
if(jQuery('#country').val()==''){
jQuery('#Profile_country_id').val('');
jQuery('#Profile_country_name').val('');
}
if (!ui.item) {
jQuery('#Profile_country_id').val('');
jQuery('#Profile_country_name').val('');
}}",
),
));
?>
<?php echo $form->hiddenField($model, 'country_id') ?>
<?php echo $form->hiddenField($model,'country_name') ?>
<?php echo $form->error($model,'country_id'); ?>
</div>