Ali*_*xel 6 javascript javascript-events typeahead typeahead.js
我正在使用带有基准数据集的typeahead.js(不是Bootstrap 2.x one!),local在任何给定点都没有其他基准被请求.我正在尝试在输入字段聚焦时呈现所有建议,然后在用户输入时简单地过滤它们.
这个问题解决了同样的需求,但是只有当我有一些令牌要搜索时,接受的解决方案才有用 - 在我的情况下,我想要显示所有内容,而不仅仅是有Uni*令牌的基准.
是否可以通过无证/模糊的方法来实现这一点,还是我必须破解其来源?
我想实现类似的目标,所以我查看了 typeahead 代码并一起修改了一些东西,请参阅以下内容:
它在处理占位符和单击离开时关闭下拉菜单方面仍然存在一些问题,但这给了我一个切换按钮,我可以单击该按钮,它是我的输入元素的同级元素,并且它从数据集中获取完整列表而不是小建议清单。
示例 html(我使用自定义提前输入数据绑定进行淘汰,但您明白了):
<div class="col-md-12 input-group tt-dropdown-group">
<input id="category" name="category" type="text" class="form-control" data-bind="
attr: { placeholder: categoryCaption },
typeahead: categories,
typeaheadValueKey: 'Name',
typeaheadValue: category,
typeaheadDestroy: true" />
<span id="category-drop" class="input-group-addon tt-dropdown-icon">
<span class="glyphicon glyphicon-chevron-down"></span>
</span>
</div>
Run Code Online (Sandbox Code Playgroud)
使用 jQuery 的 JavaScript 示例:
$(".tt-dropdown-group .tt-dropdown-icon").on("click", function() {
var $input = $(this).parent(".tt-dropdown-group").find("input.tt-query");
var $toggleBtn = $(this);
// these are all expected to be objects so falsey check is fine
if (!$input.data() || !$input.data().ttView || !$input.data().ttView.datasets || !$input.data().ttView.dropdownView || !$input.data().ttView.inputView) {
return;
}
var ttView = $input.data().ttView;
var toggleAttribute = $toggleBtn.attr("data-toggled");
if (!toggleAttribute || toggleAttribute === "off") {
$toggleBtn.attr("data-toggled", "on");
$input.typeahead("setQuery", "");
if ($.isArray(ttView.datasets) && ttView.datasets.length > 0) {
// only pulling the first dataset for this hack
var fullSuggestionList = []; // renderSuggestions expects an suggestions array not an object
$.each(ttView.datasets[0].itemHash, function(i, item) {
fullSuggestionList.push(item);
});
ttView.dropdownView.renderSuggestions(ttView.datasets[0], fullSuggestionList);
ttView.inputView.setHintValue("");
ttView.dropdownView.open();
}
}
else if (toggleAttribute === "on") {
$toggleBtn.attr("data-toggled", "off");
ttView.dropdownView.close();
}
Run Code Online (Sandbox Code Playgroud)