Multiple CJuiAutocomplete in Yii - items not rendering

允我心安 提交于 2019-12-22 10:36:34

问题


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 );
    };');
?>

回答1:


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.



来源:https://stackoverflow.com/questions/15567762/multiple-cjuiautocomplete-in-yii-items-not-rendering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!