TypeAhead.js和Bloodhound显示奇数个结果

11

我在前端实现了TypeAhead/Bloodhound,从Play/Scala服务器获取JSON数据。Typeahead版本为0.11.1。具体实现如下:

HTML:

<div id="typeahead" class="col-md-8">
   <input class="typeahead form-control"  type="text" placeholder="Select the user">
</div>

JavaScript:

var engine = new Bloodhound({
  datumTokenizer: function (datum) {
    var fullName = fullName(datum);
    return Bloodhound.tokenizers.whitespace(fullName);
  },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  identify: function(obj) { return obj.id; },
  remote: {
    url: routes.controllers.Users.index("").url,
    cache: false,
    replace: function (url, query) {
        if (!isEmpty(query)) {
            url += encodeURIComponent(query);
        }
        return url;
    },
    filter: function (data) {
        console.log(data);
        return $.map(data, function (user) {
            return {
                id: user.id,
                fullName: viewModel.fullName(user)
            };
        });
    }
}
});

// instantiate the typeahead UI
$('#typeahead .typeahead')
.typeahead({
    hint: true,
    highlight: true,
    minLength: 1,
},
{
    name: 'engine',
    displayKey: 'fullName',
    source: engine
})

function fullName(data) {
  if (data === undefined) return "";
  else {
    var fName = data.firstName === undefined ? "" : data.firstName;
    var lName = data.lastName === undefined ? "" : data.lastName;
    return fName + ' ' + lName;
  }
};

服务器返回的JSON响应:

[{"firstName":"Test","lastName":"User", "id":1}, ... ]

服务器对结果进行分页,以便最多提供5个结果,这应该也是Typeahead/Bloodhound的默认限制。

问题在于当服务器返回5个结果时,TypeAhead覆盖上不显示任何结果。如果服务器提供4个结果,则TypeAhead覆盖上显示1个结果。如果服务器提供3个结果,则TypeAhead覆盖上显示2个结果。对于2和1个结果,它会正确地显示覆盖层中的元素数量。如果我删除页面长度并且服务器返回超过10个结果,则TypeAhead会显示5个结果(限制)。筛选器中的console.log显示了正确数量的数据结果,因此它们至少已传递给Bloodhound。

此代码有什么问题?此TypeAhead字段是此页面中唯一的TypeAhead字段。我检查了DOM,并且TypeAhead生成了错误数量的结果集字段,因此这不是CSS的问题(也尝试删除所有自定义CSS)。

感谢任何回复:)

5个回答


5

TypeAhead确实存在问题,按照Github页面上Stnaire所说的修改typeahead.bundle.js有所帮助。 - mpartan
这浪费了很多时间,太荒谬了。 - JsonStatham

2

如果你不想查看typeahead源代码(例如因为你想使用min版本),你也可以将限制设置得非常高。然后,你需要自己限制要放入列表中的项目数量。

$('#typeahead .typeahead')
.typeahead({
    hint: true,
    highlight: true,
    minLength: 1,
},
{
    name: 'engine',
    displayKey: 'fullName',
    source: engine,
    limit: 1000
})

2
尝试使用engine.initialize()初始化engine; 在filter中返回suggestion对象,该对象应在templates中可用-> suggestion; 使用source:engine.ttAdapter()source中初始化engine,并将元素作为String返回至suggestion,以填充"suggestion"下拉菜单。请参见Typeahead - examples - Custom Templates

var data = [{
  "firstName": "Test",
  "lastName": "User",
  "id": 1
}, {
  "firstName": "Test2",
  "lastName": "User2",
  "id": 2
}, {
  "firstName": "Test3",
  "lastName": "User3",
  "id": 3
}, {
  "firstName": "Test4",
  "lastName": "User4",
  "id": 4
}, {
  "firstName": "Test5",
  "lastName": "User5",
  "id": 5
}];

var engine = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value"),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  local: $.map(data, function(val, key) {
            // return `suggestion` object for `templates` `suggestion`         
            return {value:val.firstName, suggestion:val}
         })
});
// initialize `engine`
engine.initialize();

// instantiate the typeahead UI
$("#typeahead .typeahead")
  .typeahead({
    hint: true,
    highlight: true,
    minLength: 1,
  }, {
    name: "engine",
    displayKey: "firstName",
    templates: {
      suggestion: function (data) {
        // `suggestion` object passed at `engine`
        console.log(data.suggestion);
        // return suggestion element to display
        var _suggestion = "<div>" 
                        + data.suggestion.firstName 
                        + " " 
                        + data.suggestion.lastName + "</div>";
        return _suggestion
      }
    },
    source: engine.ttAdapter()
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>
<div id="typeahead" class="col-md-8">
  <input class="typeahead form-control" type="text" placeholder="Select the user">
</div>


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接