简体   繁体   English

使用typeahead.js在字符串中搜索?

[英]Search within string using typeahead.js?

I would like typeahead.js to behave like jqueryui autocomplete with regards to how it matches items. 我希望typeahead.js表现得像jqueryui autocomplete关于它如何匹配项目。 Using jqueryui autocomplete it's possible to search inside the text items. 使用jqueryui自动填充,可以在文本项内搜索。 In typeahead it's only from the beginning of the string. 在typeahead中,它只是从字符串的开头。

Autocomplete example: http://goo.gl/O43afF 自动填充示例: http//goo.gl/O43afF

Typeahead example: http://twitter.github.io/typeahead.js/examples/ Typeahead示例: http//twitter.github.io/typeahead.js/examples/

With autocomplete, it seems like it's possible to assign a comparison function, but I haven't found anything like that in typeahead. 使用自动完成功能,似乎可以分配比较功能,但我没有在typeahead中找到类似的东西。

If I have a list that contains the item "Equestrian (Horses)" then I would like to get a match if I start writing "o". 如果我有一个包含“Equestrian(Horses)”项目的列表,那么如果我开始写“o”,我想得到一个匹配。

Typeahead.js code as is will look for prefix matches, as you correctly say. 正如你所说的那样,Typeahead.js代码会查找前缀匹配。 There is a "trick" though: every datum may also contain a tokens element, which as the Typeahead documentation says is "a collection of strings that aid typeahead.js in matching datums with a given query". 但是有一个“技巧”:每个数据也可能包含一个tokens元素,正如Typeahead文档所说的那样是“一个字符串集合,可以帮助在给定查询的匹配基础中输入typeahead.js”。

The prefix matching is done against tokens . 前缀匹配是针对tokens完成的。 If you don't supply a tokens value for one of your datums, its value is tokenized (space-separated) for you. 如果您没有为其中一个基准提供tokens值,则会为您标记其值(以空格分隔)。 However, you could supply tokens to get what you want. 但是,您可以提供tokens以获得您想要的内容。 For example, in your case you would supply a value of tokens that is all the unique substrings of all the words in your query string. 例如,在您的情况下,您将提供一个tokens值,它是查询字符串中所有单词的所有唯一子字符串。

I suggest "all unique substrings of length >= 2", btw. 我建议“长度> = 2”的所有唯一子串,顺便说一句。

typeahead's datasource is set via the 'source' parameter. typeahead的数据源是通过'source'参数设置的。 So it's perfectly ok to place another method instead an array in there. 所以在那里放置另一个方法而不是数组是完全可以的。 Also note that it internally expects an array of strings so you have to format everything to string. 另请注意,它在内部需要一个字符串数组,因此您必须将所有内容格式化为字符串。

Take a look at this fiddle for an example 看看这个小提琴的例子

EDIT: this example now always generates values from Test 0 to Test 9, so you can of course only check by entering parts of "test" 编辑:此示例现在始终生成从测试0到测试9的值,因此您当然只能通过输入“测试”部分进行检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM