简体   繁体   English

Jquery UI自动完成自定义HTML(项目未定义)

[英]Jquery UI Autocomplete Custom HTML (item is undefined)

I've been banging my head off my desk trying to get the jQuery UI Autocomplete to output custom HTML. 我一直在试图让jQuery UI Autocomplete输出自定义HTML。 Here is my code. 这是我的代码。

        $(document).ready(function(){

        $.widget( "custom.catcomplete", $.ui.autocomplete, {
            _renderMenu: function( ul, items ) {
                var self = this,
                    currentCategory = "";
                $.each( items, function( index, item ) {
                    if ( item.category != currentCategory ) {
                        ul.append( "<li class='ui-autocomplete-category'>" + item.category + "<ul class='autocomplete-category'></ul></li>" );
                        currentCategory = item.category;
                    }
                    self._renderItem( ul, item);
                });
            }
        });

        var data = [
            { label: "anders", category: "Antigen" },
            { label: "andreas", category: "Antigen" },
            { label: "antal", category: "Antigen" },
            { label: "annhhx10", category: "Products" },
            { label: "annk K12", category: "Products" },
            { label: "annttop C13", category: "Products" },
            { label: "anders andersson", category: "People" },
            { label: "andreas andersson", category: "People" },
            { label: "andreas johnson", category: "People" }
        ];

        $( "#typeAhead" ).catcomplete({
            delay: 0,
            source: data,
        })
        .data( "catcomplete" )._renderItem = function( ul, item ) {
            return $( "<li></li>" )
                .data( "item.catcomplete", item )
                .append( $( "<a class='ui-menu-item'></a>" ).text( item.label ) )
                .appendTo( $('ul').last('.autocomplete-category'));
        };
    });

I seem to be running into trouble by nesting my lists in the renderItem function. 我似乎通过在renderItem函数中嵌套我的列表而遇到麻烦。 The HTML output is exactly how I want it. HTML输出正是我想要的。 However when I "keydown" the I get a javascript error (item is undefined). 但是,当我“keydown”时,我得到一个javascript错误(项目未定义)。

Any ideas or suggestions? 任何想法或建议? I've tried just about everything. 我已经尝试了一切。

You were almost there! 你快到了! I only made two changes to get this to work ( Updated after comments from OP ): 我只进行了两次更改才能使其工作( 在OP发表评论后更新 ):

After digging into the jQueryUI autocomplete source, it looks like the underlying menu that's used by the autocomplete widget isn't too friendly toward nested elements. 在深入研究jQueryUI自动完成源之后,看起来自动完成小部件使用的底层菜单对嵌套元素不太友好。

I could be wrong about this, but I think the menu expects a simple <ul> with just children <li> s containing an anchor tag. 我可能错了,但我认为菜单需要一个简单的<ul> ,只有子<li>包含一个锚标记。

Edit : This line confirms my suspicion about the menu (found in jqueryUI 1.8.9's menu widget): 编辑 :这一行证实了我对菜单的怀疑(在jqueryUI 1.8.9的菜单小部件中找到):

    var items = this.element.children("li:not(.ui-menu-item):has(a)")
        .addClass("ui-menu-item")
        .attr("role", "menuitem");

    items.children("a")
        .addClass("ui-corner-all")
        .attr("tabindex", -1)
        // mouseenter doesn't work with event delegation
        .mouseenter(function( event ) {
            self.activate( event, $(this).parent() );
        })
        .mouseleave(function() {
            self.deactivate();
        });

Basically, since your a tags were buried inside a nested list, they weren't getting recognized by the menu. 基本上,由于您a标签被隐藏在嵌套列表中,因此菜单无法识别它们。

I bet you noticed in your original code that your autocomplete menu items were not highlighting when you moused over them. 我打赌你注意到你的原始代码中你的自动完成菜单项没有突出显示你的鼠标。 This highlighting actually coincides with which menu item the widget thinks is active, which was causing your widget to fail when the user selected an item. 此突出显示实际上与窗口小部件认为处于活动状态的菜单项一致,这导致窗口小部件在用户选择项目时失败。

Since there's nothing semantically wrong or visually wrong with just giving the category li sa different class, I would restructure the widget's menu like this: 由于没有什么错语义或视觉错只是给该类别li SA不同的阶级,我会重组这样的小部件菜单:

JavaScript: JavaScript的:

_renderItem function: _renderItem函数:

.data( "catcomplete" )._renderItem = function( ul, item ) {
    return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( $( "<a class='ui-menu-item'></a>" )
                 .text( item.label ) )
        .appendTo(ul);
};

Your _renderMenu function: 你的_renderMenu函数:

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

Generated HTML for AutoComplete menu: 为自动完成菜单生成的HTML:

<ul class="ui-autocomplete>
    <li class="ui-autocomplete-category">Antigen</li>
    <li class="ui-menu-item" role="menuitem">
         <a class="ui-menu-item ui-corner-all" tabindex="-1">anders</a>
    </li>
    <li class="ui-menu-item" role="menuitem">
        <a class="ui-menu-item ui-corner-all" tabindex="-1">andreas</a>
    </li>
    <li class="ui-menu-item" role="menuitem">
        <a class="ui-menu-item ui-corner-all" tabindex="-1">antal</a>
    </li>
    <!-- More categories, items, etc.-->
</ul>

Judging by your comments, it seemed like you wanted the menu's HTML to be nested ul s inside of li s for each category. 通过您的意见来看,这似乎是你想要的菜单的HTML是嵌套ul S的内li S代表每个类别。 Let me know if changing the HTML of the generated menu isn't an option for you for some reason. 如果由于某种原因更改生成的菜单的HTML不是您的选项,请告诉我。

I've updated the example: http://jsfiddle.net/andrewwhitaker/pjs7a/2/ 我已经更新了这个例子: http//jsfiddle.net/andrewwhitaker/pjs7a/2/

Hope that helps. 希望有所帮助。

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

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