简体   繁体   English

Autcomplete-自定义renderItem /菜单和项目选择问题

[英]Autcomplete - custom renderItem/Menu and item selection issue

I am using jQueryUI (1.10.2) and jQuery (1.9.1) with the autocomplete tool. 我正在将jQueryUI(1.10.2)和jQuery(1.9.1)与自动完成工具一起使用。

I have remote json data AND categories AND custom display with an image for each item from the json data (showed in the autocomplete list). 我有远程json数据,类别和自定义显示,其中包含json数据中每个项目的图片(显示在自动填充列表中)。

This is my autocomplete init 这是我的自动完成初始化

var autoSearch = $("#header-search-text").autocomplete({
    source: function( request, response ) {
        $.ajax({
            //I have items with label, value, category and imgPath info
        });
    },
     focus: function( event, ui ) {
        console.log("focus is ");
        //This line shows "null"
        console.log(ui);
        return false;
    },
    minLength: 4,
    select: function( event, ui ) {
        //Never shows up
        console.log("selected is ");
        console.log( ui.item);/* ?
        "Selected: " + ui.item.label :
        "Nothing selected, input was " + this.value);*/
    },
    open: function() {
        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
    },
    close: function() {
        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
    }
}).data('ui-autocomplete');

I have a custom _renderMenu 我有一个自定义_renderMenu

//Working
autoSearch._renderMenu = function( ul, items ) {
        var that = this,
        currentCategory = "";
        $.each( items, function( index, item ) {
            console.log(item);
            if ( item.category != currentCategory ) {
                ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
                currentCategory = item.category;
            }
            that._renderItem( ul, item );
        });
};

But when I had the _renderItem, the keys (up and down) for selection doesn't work anymore and I cannot click on anything (no :hover CSS showing up) 但是当我有_renderItem时,用于选择的键(向上和向下)不再起作用,并且我无法单击任何东西(不显示:hover CSS)

autoSearch._renderItem = function(ul, item) {
    console.log("ul is ");
    console.log(ul);
    return $('<li>')
        .data('item.autocomplete', item)
        .append('<img style="width:50px;height:50px;" src="' + base_url + "/"+ item.imgPath + '" alt="" />' + item.label)
        .appendTo(ul);
};

Ok, I checked the HTML and I was obviously missing this line in the _renderItem : 好的,我检查了HTML,显然_renderItem中缺少此行:

 //Before the appendTo(ul);
.append( $( "<a>" ).text( item.label ) )

I don't know why nor how but by adding an , jQuery generates a bunch of attributes like 我不知道为什么也不怎么做,但是通过添加,jQuery生成了一堆属性,例如

id="ui-id-7" class="ui-corner-all" tabindex="-1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM