使用backboneJS集合中的任意顺序搜索词 [英] Search words in any order using backboneJS collection
问题描述
我有code搜索从文本框中键入的文字,获取所打的词,搜索它收集和显示结果在HTML页面中。
I have code to search the typed words from the text box, Get the typed word, search it in the collection and show the result in the HTML page.
在我可以查的单词,正是因为它是使用类似。
In that i can search the word exactly as it is, using LIKE.
(即)
search: function(type,letters){
var pattern = new RegExp(letters,"gi");
return _(this.filter(function(data) {
return pattern.test(data.get(type));
}));
}
如果模型有
- 医生你好
- 喜先生
- 的Hello World
- 欢迎程序员
和用户类型,你好,它显示我
And the user types as "Hello", it shows me
您好医生
您好世界
我需要显示,即使用户错误地排列词语的结果,
Hello Doctor
Hello World
I need to display the result even if the user wrongly arranges the words,
即如果用户键入世界你好
i.e If the user types as "World Hello"
它没有显示任何结果。但是,我想向用户展示
It is not showing any results. But, I want to show the user
的Hello World
Hello World
在相同的情况下在网上SQL解决。
参考这里
The same scenario is solved in web SQL.
Ref here
我想要backboneJS集合中来实现这一点。
你能帮帮我吗?
I want to achieve this in the backboneJS collection. Can you please help me?
推荐答案
您可以在空间分割你的文字,然后反复筛选模型,并减少他们所需的选择。例如:
You could split your text on spaces and then iteratively filter the models and reduce them to the desired selection. For example:
var C = Backbone.Collection.extend({
search: function(type, letters) {
var words = letters.split(/\s+/), //array of words
models = _.clone(this.models); //copy of the models
//for each word, check if it is found in the given attribute
_.each(words, function(word) {
var pattern = new RegExp(word, "i");
models = _.filter(models, function(model) {
return pattern.test(model.get(type));
});
});
return models;
}
});
和演示 http://jsfiddle.net/ha8RM/1/
这篇关于使用backboneJS集合中的任意顺序搜索词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!