E
E
EVOSandru62015-07-31 13:02:03
JavaScript
EVOSandru6, 2015-07-31 13:02:03

How to display array elements in a list using CJuiAutoComplete in Yii?

Greetings!

Tell me how to display in the drop-down list of the text field the elements of the array that was received by the controller action as a result of the search?

This is what the widget looks like:

$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
               'name'=>'searchbox',
                'value'=>'',
               'source'=>Yii::app()->createUrl('customer/places/autocomplete'),
                'options'=>array(
                           'showAnim'=>'fold',
                            'minLength'=>'1',
                            'select'=>'js:function( event, ui ) {
                                  $("#searchbox").val( ui.item.label );
                                  $("#selectedvalue").val( ui.item.value );
                                  return false;
                             }',
                         ),
                         'htmlOptions'=>array(
                                 // 'autocomplete'=>'off',
                                 'onfocus' => 'js: this.value = null; $("#searchbox").val(null); $("#selectedvalue").val(null);',
                                                    'class' => 'input-xxlarge search-query',
                                                    'placeholder' => "Живой поиск...",
                          ),
             ));


This is what the controller action looks like:

public function actionAutocomplete($term)
  {
        $query = Places::model()->findallbyattributes( array('name'=>$term));
        $list = array();
        foreach($query as $q)
        {
            $data['value']= $q['id'];
            $data['label']= $q['name'];

            $list[]= $data;
            unset($data);
        }
        echo json_encode($list);
}


There are no errors in the console, the request is sent and the response is received. But the list does not drop out (

If I change:

1. $list[]= $data;

By

2. Only now the list drops out, but I would like the names to be taken from the array$list[]= $data['name'];

array(array( 'id', 'name' ),array( 'id', 'name' ),array( 'id', 'name' ),array( 'id', 'name' ))
Which is formed in the first version.

And one more thing - how can you access the id of the selected name element from the list without extra requests? For example, through js manually or using the framework

I redid it like this:

foreach($model as $item)
        {
            $data['id']= $item['id'];
            $data['value']= $item['name'];
            $list[]= $data;
            unset($data);
        }


Earned! I just don’t know how to pull out the id when selecting an item in the list ( Is it somewhere in the Dom tree or in memory?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2015-08-06
@EVOSandru6

....
'select'=>'js:function( event, ui ) {
// вот тут лежит ID
    ui.item.id    

}',
....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question