11

我的前端有一个 TypeAhead/Bloodhound 实现,它从 Play/Scala 服务器获取 JSON 数据。预输入版本是 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 在叠加层中显示 0 个结果。如果服务器给出 4 个结果,TypeAhead 在叠加层中显示 1。如果服务器给出 3 个结果,TypeAhead 显示 2 个结果。对于 2 和 1 结果,它显示叠加中正确的元素数量。如果我删除页面长度并且服务器返回超过 10 个结果,则 TypeAhead 显示 5 个结果(限制)。过滤器中的 console.log 显示正确数量的数据结果,因此它们至少会转到 Bloodhound。

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

感谢您的任何回复:)

4

5 回答 5

9

推特据说放弃了这个项目。尝试维护的前叉。它已经解决了这个问题。

于 2015-12-03T17:51:06.807 回答
5

代码中的 typeahead 存在问题:

https://github.com/twitter/typeahead.js/issues/1218

您可以按照问题页面中的说明在 JS 中自己更改代码。

于 2015-05-08T14:24:51.943 回答
2

尝试engine使用engine.initialize();进行初始化 返回suggestion对象 at filter,它应该在templates->处可用suggestion;用初始化;engine_ 返回元素为at ,以填充“建议”下拉菜单。请参阅Typeahead - 示例 - 自定义模板sourcesource:engine.ttAdapter()Stringsuggestion

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>

于 2015-05-08T14:43:01.483 回答
2

如果(比如我)你不想进入预输入源,(例如因为你想使用最小版本)你也可以将限制设置得非常高。然后,您必须自己限制要放入列表的项目数量。

$('#typeahead .typeahead')
.typeahead({
    hint: true,
    highlight: true,
    minLength: 1,
},
{
    name: 'engine',
    displayKey: 'fullName',
    source: engine,
    limit: 1000
})
于 2015-10-06T08:50:50.493 回答
1

对于发现此问题的任何人,请使用代码的维护版本。原作是废话。

https://github.com/corejavascript/typeahead.js

于 2019-10-10T00:42:52.217 回答