一个值树的JQuery文本输入自动完成

时间:2012-07-11 11:50:04

标签: javascript jquery-plugins

大家

我有一个文本输入,其内容应限于一组值。这些值的形式为dependency1,dependency2,dependency3,因此它们形成一个树。我想为这个知道这个结构的领域提供智能自动完成功能。例如,结构是这样的:

Engineering
  Electric
    Communication
    Power
  Electronic
    Computation
    Control
Economy
  Economy
  Accounting
Education
  Basic
  Language
    English
    French
  Sciences
    Math
    Physics

因此,一旦用户键入了足够的字母,就应该完成第一个依赖项,并且应该准备好自动完成下一个依赖项。如果某个选项出现在弹出窗口中,它们应该仅限于我们所依赖的依赖项,而不是所有树。

你知道任何提供类似内容的jQuery(或其他)库吗?

提前致谢。

2 个答案:

答案 0 :(得分:2)

从另一个关于值分组的搜索中找到了一个小提琴。对它进行了更多的工作 - 我认为这样做会很好 - 我只有一个级别的分组,但我认为可以添加更多:

http://jsfiddle.net/Kieveli/c1wgjLfv/3/

HTML:

<label for="search">Search: </label>
<input id="search">

CSS:

    .ui-autocomplete-category {
        font-weight: bold;
        padding: .2em .4em;
        margin: .8em 0 .2em;
        line-height: 1.5;
    }   
    .ui-menu .ui-menu-item {
        padding: 0em 1.5em;
        width: 220px;
        clear: both;
    }
    .Products {
        width: 220px;
    }
    .People {
        width: 220px;
    }

jQuery的:

 $.widget( "custom.catcomplete", $.ui.autocomplete, {

     _create: function() {
        this._super();
        this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
    },

     _renderMenu: function( ul, items ) {

         var that = this,
        currentCategory = "";

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

            li = that._renderItemData( ul, item );

            if ( item.category ) {
                li.attr( "aria-label", item.category + " : " + item.label );
            }
        });
    },

     _renderItem: function( ul, item ) {
        return $( "<li>" )
        .addClass(item.category)
        .attr( "data-value", item.value )
        .append( $( "<a>" ).text( item.label ) )
        .appendTo( ul );
    }
});

$(function() {
    var data = [
        { label: "annhhx10", category: "Products" },
        { label: "annk K12", category: "Products" },
        { label: "annttop C13", category: "Products" },
        { label: "bannttop C13", category: "Products" },
        { label: "cannttop C13", category: "Products" },
        { label: "dannttop C13", category: "Products" },
        { label: "eannttop C13", category: "Products" },
        { label: "fannttop C13", category: "Products" },
        { label: "gannttop C13", category: "Products" },
        { label: "hannttop C13", category: "Products" },
        { label: "iannttop C13", category: "Products" },
        { label: "jannttop C13", category: "Products" },
        { label: "anders andersson", category: "People" },
        { label: "andreas andersson", category: "People" },
        { label: "andreas johnson", category: "People" },
        { label: "bandreas johnson", category: "People" },
        { label: "candreas johnson", category: "People" },
        { label: "dandreas johnson", category: "People" },
        { label: "eandreas johnson", category: "People" },
        { label: "fandreas johnson", category: "People" },
        { label: "gandreas johnson", category: "People" },
    ];

    $( "#search" ).catcomplete({
        delay: 0,
        source: data
    });
});

答案 1 :(得分:-2)

不完全是应该在SO上提出的问题类型,但是有一个名为jQuery-UI的插件,它依赖于jQuery,内置了自动完成插件。请看一下:

http://jqueryui.com/demos/autocomplete/