0

I have used jQuery's autocomplete plugin in the past, but never before through Yii's implementation of it as a widget.

I am dynamically creating another autocomplete field and not able to set it to live:

$('input.ui-autocomplete-input').live("keydown.autocomplete", function() {
    $(this).autocomplete();
});

or simply calling .autocomplete() when it is created to get it to work. Any ideas why this would work differently through Yii than through just using the plugin itself, or am I missing something really simple?

Thanks for any help!

Owen McAlack
  • 399
  • 10
  • 28

2 Answers2

1

My solution was overly simple.

Each autocomplete field must have a unique name attribute. I was using

    <input name="family[]"/>
    <input name="family[]"/>
    <input name="family[]"/>

Fixed by changing it to:

    <input name="family_0"/>
    <input name="family_1"/>
    <input name="family_2"/>

Just a little embarrassing, but worth posting in case anyone else ever overlooks such a simple issue.

Note: the code above isn't exact, the point is to make sure you are using unique names for each of the fields that are using jQueryUI's autocomplete.

Owen McAlack
  • 399
  • 10
  • 28
0

CJuiAutoComplete relies on jQuery UI's autocomplete so please be sure that jQuery UI is registered for the view you create a field dynamically in:

Yii::app()->clientScript->registerCoreScript("jquery.ui");

Also be sure that source parameter with your autocomplete tags is provided for autocomplete() call. The following works fine for me:

<?php
$source = array(
    "hello",
    "test"
);
?>
<div id="wrapper">
</div>
<?php
Yii::app()->clientScript->registerScript("autocomplete", "
    $('<input />')
        .addClass('ui-autocomplete-input')
        .appendTo($('#wrapper'));
    $('input.ui-autocomplete-input').autocomplete({
        'source': " . CJavaScript::encode($source) . "
    });
", CClientScript::POS_READY);
ezze
  • 3,933
  • 2
  • 34
  • 58
  • Thanks for the answer, I learned a bit about Yii's registerScript and family that your code directed me towards. Simply using the CJuiAutoComplete widget in Yii registers jQuery and jQueryUI/ Re-registering executes the code twice, so for anyone else: watch out for that. I posted the very simple solution here. **/me smacks head on desk** – Owen McAlack Apr 21 '13 at 04:43