Question

I need two CJuiAutocomplete items on my view page. Unfortunately - only one of them is rendering items properly. The other one - renders empty rows. I checked in firebug and the values are retrieved from database properly. Actually if I change the order of the registerScript - only the autocomplete from last registerScript renders items properly.

Here is my code:

<?php
    $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
        'name' => 'autocities',
        'sourceUrl'=>$this->createUrl('projects/dynamicGetCities'),
        'options' => array(
            'minLength' => 2,
            'select' => "js: function(event, ui) {
                        $('#lastSelectedCityId').val(ui.item.idCity);
                        var ciname = ui.item.name + ' (' + ui.item.directional + ')';
                        $('.selectedCity').html(ciname);
                    }
                "
        ),
    ));
?>
<br/><br/>
<?php
    $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
        'name' => 'autostreets',
        'sourceUrl' => 
            'js: function(request, response) {
                $.ajax({
                    url: "'.$this->createUrl('projects/dynamicGetStreets').'",
                    dataType: "json",
                    data: {
                        term: request.term,
                        idCity: $("#lastSelectedCityId").val()
                    },
                    success: function (data) {
                        response(data);
                    } 
        })}',
        'options' => array(
            'minLength' => 2,
            'select' => "js: 
                function(event, ui) 
                {
                    $('#lastSelectedStreetId').val(ui.item.idStreet);
                    $('.selectedStreet').html(ui.item.name);
                }"
        ),
    ));

    Yii::app()->clientScript->registerScript('input', '
        $("#autostreets").data("autocomplete")._renderItem = function( ul, item ) {
        return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( "<a>"+item.name+"<br/><span style=\"font-size: 9px;\">Abonentów: "+item.customCount+"</span></a>")
        .appendTo( ul );
    };');

    Yii::app()->clientScript->registerScript('input', '
        $("#autocities").data("autocomplete")._renderItem = function( ul, item ) {
        return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( "<a>"+item.name + " - " + item.directional+"<br/><span style=\"font-size: 9px;\">Abonentów: "+item.customCount+"</span></a>")
        .appendTo( ul );
    };');
?>
Was it helpful?

Solution

So much searching and right after asking here on SO I found it heh. The answer is simple in the method registerScript - first parameter is the unique id of the script. The id was the same both times so it was overriding the previous one. Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top