从Jquery-ui自动完成到typeahead.js

Arn*_*lay 8 jquery-ui twitter-bootstrap bootstrap-typeahead typeahead.js

我正在将我的应用程序迁移到twitter-bootstrap,我想用typeahead.js替换我的jquery-ui自动完成.

最好使用twitter-bootstrap中嵌入的功能,但如果需要,我可以使用额外的typeahead插件.

我的问题是我很难再现当前的行为,尤其是在没有结果的情况下.

你会怎么做那样的事情?

$("#search").autocomplete({
           source : myUrl,
            delay : 100,
            minLength : 2,
            select : function(event, ui) {
              // do whatever i want with the selected item
            },

            response : function(event, ui) {
                if (ui.content.length === 0) {
                    ui.content.push({
                        label : "No result",
                        value : customValue
                    });
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

基本上,如果没有结果,我想在组件中显示自定义消息.

谢谢!

Zim*_*Zim 5

迁移到 Bootstrap typeahead 看起来像..

$('.typeahead').typeahead({
  minLength:2,
  updater: function (item) {
     /* do whatever you want with the selected item */

    },
  source: function (typeahead, query) {
     /* put your ajax call here..
     return $.get('/typeahead', { query: query }, function (data) {
        return typeahead.process(data);
     });
     */      
    }
});
Run Code Online (Sandbox Code Playgroud)

编辑:

我已经更新了演示以包含一个sorterhighlighter。我认为这会让你得到你想要的结果..

  sorter: function(items) {
    if (items.length == 0) {
        var noResult = new Object();
        items.push(noResult);
    }
    return items;    
    },
  highlighter: function(item) {
    if (dataSource.indexOf(item) == -1) {
        return "<span>No Match Found.</span>";
    }
    else {
       return "<span>"+item+"</span>";
    }
  },
Run Code Online (Sandbox Code Playgroud)

Bootstrap 预先输入演示

我不认为 typeahead 相当于延迟,但是有一些 jquery 解决方法。


Arn*_*lay 4

使用最新版本的 Typeahead.js (0.10),现在可以指定在未找到结果时显示的空模板。

   $('.typeahead').typeahead({
     hint: true,
     highlight: true,
     minLength: 2
    },{
      name: 'examples',
      source: examples.ttAdapter(),
      templates: {
        empty: [
          '<div class="empty-message">',
          'unable to find any results that match the current query',
          '</div>'
        ].join('\n')
      }
    });
Run Code Online (Sandbox Code Playgroud)