2017-03-18 71 views
0

我想使用javascript typeahead bloodhound,但它無法正常使用遙控器。Jquery Typeahead Bloodhound無法正常工作

這裏是我的Javascript代碼:

<script type="text/javascript"> 
    jQuery(document).ready(function($) { 
     var clubs = new Bloodhound({ 
     datumTokenizer: function() { 
      return Bloodhound.tokenizers.obj.whitespace; 
     }, 
     queryTokenizer: function() { 
      return Bloodhound.tokenizers.whitespace; 
     }, 

     remote: { 
      url: "{{ url('club/listall') }}"+'/%QUERY%', 
      filter: function(searchClubs) { 
       console.log(searchClubs); 
       return searchClubs; 
      }, 
      wildcard: "%QUERY%" 
     }, 
    }); 

     $(".club-search").typeahead({ 
      hint: true, 
      highlight: true, 
      minLength: 1 
     }, { 
      source: clubs.ttAdapter(), 
      name: 'clubsList', 
      templates: { 
       empty: [ 
        '<div class="list-group search-results-dropdown"><div class="list-group-item">Aucun club trouvé.</div></div>' 
       ], 
       header: [ 
        '<div class="list-group search-results-dropdown">' 
       ], 
       suggestion: function (data) { 
        return '<a href="{{ url('club') }}' + '/' + data.id + '" class="list-group-item"><span class="row">' + 
           '<span class="avatar">' + 
            '<img src="{{asset('/')}}' + blason + '">' + 
           "</span>" + 
           '<span class="name">' + data.name + '<br><small style="color:grey;">(Ligue ' + data.league_name + ')</small></span>' + 
          "</span>" 
      } 
      } 
     }); 
    }); 
</script> 

但它不工作完全正常......在一般情況下,它發現的結果,但我將給你一個搜索查詢的一個例子。一個可能的查詢是「montagnarde」。我會給你每封信的結果。打字:

m --> lot of results 
mo --> lot of results 
mon --> lot of results 
mont --> lot of results 
monta --> lot of results 
montag --> lot of results 
montagn --> lot of results 
montagna --> no result 
montagnar --> finds only "J.S. MONTAGNARDE" 
montagnard --> finds only "J.S. MONTAGNARDE" 
montagnarde --> finds only "J.S. MONTAGNARDE" and "LA MONTAGNARDE" 
montagnarde i --> finds only "U.S. MONTAGNARDE INZINZAC" 

而且console.log(searchClubs)給了我不同的結果。

有沒有人看到問題在哪裏?先謝謝你!

回答

0

我找到了解決方案。

我使用的是twitter/typeahead.js版本0.11.1,不幸的是這個版本不再被維護。

我們必須使用:corejavascript/typeahead.js version 1.1.1