Typehead.js远程奇怪的行为 - 有时不显示结果

时间:2016-10-02 20:16:50

标签: javascript bloodhound typehead

我使用最新的typehead(typeahead.js 0.11.1)作为typeheadbundle发生了很多奇怪的事情

首先,我使用typeahead.bundle.min.js v0.11.1远程使用2个数据集。问题是服务器是否正确响应,但是打字机并不关心,并且没有发现任何问题。

奇怪的是,有时打字机会正确显示结果,有时只有一个数据集,有时它什么都不显示。但服务器会给出结果!

更新

我发现typehead在最新的稳定版本中有一个错误。这是固定的https://github.com/corejavascript/typeahead.js

2 个答案:

答案 0 :(得分:0)

typehead在最新的稳定版本中有bug :)

我不相信。

直到发现一些好人做了一个叉子并修好了 https://github.com/corejavascript/typeahead.js

我觉得如果这是由twitter维护的 - 而且我使用的是最新的稳定版本 - 可能我做错了,但即使是大牌也失败了:(

答案 1 :(得分:0)

我找到了你。

当我第一次开始使用typehead时,我也遇到了一些错误,但据我发现,这是我的错。

使用typehead.js需要一些配置。因此,我创建了一个使用本地数据和使用服务器数据(AJAX)来使用typehead.js的完整示例。

您可以查看this Github Repo

重新 -

为了您的理解,我在这里给出了一个小型演示(没有CSS) -



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'
];

$('#typehead_example').typeahead(
						{
							hint:		true,
							highlight:	true,
							minLength:	1
						},
						{
							name:		'states',
							displayKey:	'value',
							source:		substringMatcher(states)
						}
					);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/corejs-typeahead/1.0.1/typeahead.bundle.min.js" type="text/javascript" charset="utf-8"></script>

<input id="typehead_example" class="typeahead" type="text" placeholder="States of USA">
&#13;
&#13;
&#13;

完整示例在this Github Repo中给出。