我在一个自动完成的文本输入框中使用了Typeahead.js技术,这个技术很不错。但是当输入框获得焦点时,我需要激活下拉菜单并显示所有可选项。我看到的每一个可能的解决方案都涉及使用某些值初始化输入框,但我需要展示所有的选项。
我该如何实现这一目标?
我在一个自动完成的文本输入框中使用了Typeahead.js技术,这个技术很不错。但是当输入框获得焦点时,我需要激活下拉菜单并显示所有可选项。我看到的每一个可能的解决方案都涉及使用某些值初始化输入框,但我需要展示所有的选项。
我该如何实现这一目标?
identify: function(obj) { return obj.team; },
function nflTeamsWithDefaults(q, sync) {
if (q === '') {
sync(nflTeams.all()); // This is the only change needed to get 'ALL' items as the defaults
} else {
nflTeams.search(q, sync);
}
}
var nflTeams = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify: function(obj) { return obj.team; },
prefetch: '../data/nfl.json'
});
function nflTeamsWithDefaults(q, sync) {
if (q === '') {
sync(nflTeams.all()); // This is the only change needed to get 'ALL' items as the defaults
}
else {
nflTeams.search(q, sync);
}
}
$('#default-suggestions .typeahead').typeahead({
minLength: 0,
highlight: true
},
{
name: 'nfl-teams',
display: 'team',
source: nflTeamsWithDefaults
});
http://twitter.github.io/typeahead.js/examples/#default-suggestions
我希望这能对某些人有所帮助,因为我花了一些时间才找到这个信息(通过跟踪所有的错误报告和尝试使用 .all() 方法来找到它)...
minLength: 0
注意:
有一个拉请求解决了这个问题。我对10.2进行了快速修改,使得“the basics”示例(在此处找到)在焦点上显示。
我将混合方法_onFocus(第1459行)改为:
_onFocused: function onFocused() {
this.isActivated = true;
this.dropdown.open();
},
收件人:
_onFocused: function onFocused() {
this.isActivated = true;
var val = this.input.getInputValue();
var query = Input.normalizeQuery(val);
this.dropdown.update(query);
this.dropdown.open();
},
虽然并不正式,但它完成了任务。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="typeahead.js"></script>
<script src="jquery-ui.js"></script>
<script>
$(function(){
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substrRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push({ value: str });
}
});
cb(matches);
};
};
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 0
},
{
name: 'states',
displayKey: 'value',
source: substringMatcher(states)
});
$('.typeahead').on( 'focus', function() {
if($(this).val() === '') // you can also check for minLength
$(this).data().ttTypeahead.input.trigger('queryChanged', '');
});
});
</script>
</head>
<body>
<input class="typeahead" type="text" placeholder="States of USA">
</div>
</body>
</html>
已验证此方法可在0.10.5版本中使用。注意:发现此方法不适用于Bloodhound搜索引擎,因为Bloodhound的queryTokenizer期望一个字符。
使用0.10.4版本
要返回空查询的所有结果,请在bloodhound.js的第450行添加以下内容:
if (query == "") { return that.datums; }
$(input_element).on("click", function () {
ev = $.Event("keydown")
ev.keyCode = ev.which = 40
$(this).trigger(ev)
return true
});
现在有一种更简单的方法来完成这项工作,无需修改源代码。可能自原始回答以来源代码已经发生了变化,但我认为值得在此提出。
创建typeahead后:
var $element = $('#myTextElement');
$element.typeahead({ source: ['Billy', 'Brenda', 'Brian', 'Bobby'] });
只需将minLength设置为0:
$element.data('typeahead').options.minLength = 0;
当创建typeahead时,minLength选项被强制设为1,但您可以在创建后设置它,并且它完美地工作。
TypeError:$element.data(...)未定义
。 - Steve Llet baseUrl = 'some-url.com',
url = baseUrl + '?search=%QUERY';
...
$el.typeahead({
minLength: 0,
highlight: true,
prefetch: baseUrl
}, {
...
});
minLength: 0
适用于键盘或鼠标聚焦。无需更改代码或添加新事件。另一个选择是使用Typeahead的非公共API。完整的Typeahead对象可以通过jQuery的data
方法获得。
var _typeaheadElem = $('#myInput').find('.typeahead');
var _typeahead = _typeaheadElem.data('ttTypeahead');
_typeaheadElem.focus(function() {
/* WARNING: This is hackery abusing non-public Typeahead APIs */
if (_typeaheadElem.val() === "") {
var input = _typeahead.input; //Reference to the TA Input class
//Set the component's current query to something !== input.
input.query = "Recent People";
input._onInput("");
}
});
查看在v0.10.2中可用的更多代码 http://pastebin.com/adWHFupF
这与PR 719相关联 https://github.com/twitter/typeahead.js/pull/719