2017-04-26 34 views
0

手头的问题是,搜索建议不被同一起跑线字符排列如可以在图片中可以看出:'ne' should be at the very top, yet it is at the very bottomTwitter的建议事先键入的内容订货

我怎样才能解决这个问题呢?

以下是我code` $(文件)。就绪(函数(){ // VAR的查询=银行;

var queries = ['there is no need', 'need', 'no need', 'ne']; 

//Dataset defined in index.php 
// ***** 
//(NOTE: Typehead works by the order of the elements in dataset, thus 
//   they are ordered in the database first based on count) 

//Constructing the suggestion engine 
var queries = new Bloodhound(
    { 
     datumTokenizer: Bloodhound.tokenizers.whitespace, 
     queryTokenizer: Bloodhound.tokenizers.whitespace, 
     local: queries 
    }); 

// Initializing the typeahead (.typehead is the selector having what's currently 
//         being typed) 
$('.typeahead').typeahead(
    { 
     hint: true, 
     highlight: true, /* Enable substring highlighting */ 
     minLength: 1 /* Specify minimum characters required for showing result */ 
    }, 
    { 
     name: 'queries', 
     source: queries 
    }); 

}); `

回答

0

如果sorter属性与功能初始化,Bloodhound将使用它来订购匹配的条目,象下面这样:

var queries = new Bloodhound(
{ 
    datumTokenizer: Bloodhound.tokenizers.whitespace, 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    local: queries, 
    sorter: function(a, b) { 
     if (a < b) { 
      return -1; 
     } 
     else if (a > b) { 
      return 1; 
     } 
     else return 0; 
    } 
}); 

所以,仅仅延长Bloodhound初始化,你是好去。

相关问题