2015-09-12 26 views
1

我正在尝试使用typeahead.js在窗体中显示ajax结果。 起初我尝试Bloodhound建议引擎默认与typeahead来。但它没有显示服务器返回的所有项目(我只显示1或有时两个)。为什么typeahead.js没有显示ajax响应中的所有项目?

以下是我使用的代码;

$('.autocomplete').livequery(function() { 
      var input = this; 
      $(input).removeClass('autocomplete'); 

      var _source = new Bloodhound({ 
       limit: 25, 
       datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Value'), 
       queryTokenizer: Bloodhound.tokenizers.obj.whitespace('Text'), 
       prefetch: $(input).attr('data-source'), 
       remote: { 
        url: $(input).attr('data-source') + '?query=%QUERY', 
        wildcard: '%QUERY' 
       } 
      }); 

      _source.initialize(); 

      $(input).typeahead({ 
       hint: true, 
       highlight: true, 
       minLength: 0 
      }, { 
       displayKey: 'Text', 
       source: _source, 
       templates: { 
        notFound: 'No results found' 
       } 
      }); 

      $(input).on('typeahead:selected', function (evt, item) { 
       $(input).parent().parent().find('input[type="hidden"]').val(item.Value); 
      }); 
     }) 

然后我试着用没有血猎犬的方式做下面的代码,结果没有任何变化;

$('.autocomplete').livequery(function() { 
      var input = this; 
      $(input).removeClass('autocomplete'); 

      $(input).typeahead({ 
       hint: true, 
       highlight: true, 
       limit: 50, 
       minLength: 0 
      }, { 
       displayKey: 'Text', 
       source: function (query, syncResults, asyncResults) { 
        $.get($(input).attr('data-source') + '?query=' + query, function (data) { 
         asyncResults(data); 
        }); 
       }, 
       templates: { 
        notFound: 'No results found' 
       } 
      }); 

      $(input).on('typeahead:selected', function (evt, item) { 
       $(input).parent().parent().find('input[type="hidden"]').val(item.Value); 
      }); 
     }) 

服务器对请求的响应如下;

[ 
{ 
    "Text": "Marketing Executive", 
    "Value": 1 
}, 
{ 
    "Text": "CEO", 
    "Value": 5 
}, 
{ 
    "Text": "Manager", 
    "Value": 6 
}, 
{ 
    "Text": "Accountant", 
    "Value": 7 
}] 

这是什么问题?以及如何获得打印以显示服务器返回的所有结果?

+0

默认只有5建议将显示,使用'限制'选项来增加它。检查[文档](https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#options) –

+0

@JSantosh'极限:50',检查问题。 – zeroflagL

+0

你看不到50个结果吧? –

回答

2

这是控件中的一个错误。这可以在typeahead.bundle.js

开关线1723和1724是固定由下面的代码更改,以便它看起来像这样

that._append(query, suggestions.slice(0, that.limit - rendered)); 
rendered += suggestions.length; 

荣誉的职位Bootstrap Typeahead not showing hints as expected