【问题标题】:Noncontiguous substring autocomplete without spaces不带空格的非连续子字符串自动完成
【发布时间】:2013-06-13 17:30:09
【问题描述】:

我在 SO 上看到了很多关于带有多个关键字或标记的 jquery 自动完成的问题,例如

“el wo”匹配“hello world”(由于SO的markdown解析不添加空格,我无法将子字符串匹配加粗)

我想实现类似的效果,但不在搜索字符串中使用空格。这已在 JetBrains IntelliJ 和他们的其他类似产品中成功使用。例如,

aco 不仅匹配aconite,还匹配admin_controller

jquery 和/或 bootstrap 自动完成或 typeahead 插件可以做到这一点吗? IntelliJ 如何做到这一点?

【问题讨论】:

    标签: jquery twitter-bootstrap autocomplete intellij-idea bootstrap-typeahead


    【解决方案1】:

    它不会检查匹配的顺序,但你可以试试这个:

    http://jsfiddle.net/tEJdx/5/

    var possible = [
        'admin_controller',
        'accommodation',
        'printer',
        'advertised',
        'aoc', // this will match even if it's in wrong order
    ];
    
    $('input').keypress(function(){
        setTimeout( function() {
            var result = [], input = $('#input').val();
            possible.filter(function(element, index, array) {
                var count = 0;
                for( i in input ) {
                    if( element.indexOf( input[i] ) != -1 )
                        count++;
                }
                if( count == input.length )
                result.push( element );
            });
    
            $('#result').html(result.join('<br/>'));
        }, 200);
    
    });
    

    使用这样的 HTML:

    <input id="input" />
    <div id="result"></div>
    

    【讨论】:

    • 太棒了。谢谢!希望有办法让子字符串保持有序。
    • @tralston 如果“按顺序”是指按字母顺序排序,只需将 result.join('
      ') 更改为 result.sort().join('
      ' )
    • 不,我的意思是按照它们自然发生的顺序。所以 "adc" 将匹配 "admin_controller" (ad..min_..c..ontroller) 但不匹配 "accomodation" (ac..como..d..ation) - 字符的顺序应保留为输入字符串中的相同顺序。
    猜你喜欢
    • 2013-03-31
    • 2012-05-10
    • 1970-01-01
    • 2011-04-11
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多