如何使用ember.js做一个类似的过滤器 [英] How to do a like filter with ember.js
问题描述
如果我用户搜索数组,我下面的工作如何...
{code> filtered = ['id','username']。map(function(property){
return self.get('content')。filterProperty(property,filter);
});
...和一些用户具有相同的用户名。例如=>如果我通过smith搜索/过滤,它将返回两个记录,因为username属性与smith完全匹配
我如何改变这个地图函数以使用类似的样式查询,所以当我输入单词sm时,它仍然发现这两个记录。
这里是显示过滤器的jsfiddle在上面显示 http://jsfiddle.net/Rf3h8/
提前谢谢
您可以使用 RegExp
对象来测试匹配的数据。由于您正在编写自己的过滤器逻辑,因此您必须使用过滤器
函数。我更新了你的小提琴来做这个工作: http://jsfiddle.net/Rf3h8/1/
您的小提琴包含大量代码,可能难以让其他人遵守。以下是使用 RegExp
过滤数组的一个非常简单的示例。
var regex = new RegExp('ry');
var filtered = names.filter(function(person){
return regex.test(person);
});
已过滤// => ['ryan']
其实你甚至可以将它重构为
var filtered = names.filter(regex.test,regex);
I have a simple ArrayController in ember pre 1.0 and found that I can chop the list down if the filter finds an exact match for a given property, but what I can't seem to find is how do a "like" query with filter.
What I have below works if I search an array with users...
filtered = ['id', 'username'].map(function(property) {
return self.get('content').filterProperty(property, filter);
});
... and a few of the users have the same username. For example => if I search / filter by "smith" it will return both records as the "username" property has the exact match for "smith"
How can I change this map function to work with the like style query so when I type the word "sm" it still finds both of these records
Here is the jsfiddle showing the filter I show above in action http://jsfiddle.net/Rf3h8/
Thank you in advance
You can use a RegExp
object to test pieces of data for a match. Since you are writing your own filter logic, you'll have to use the filter
function. I've updated your fiddle to make this work: http://jsfiddle.net/Rf3h8/1/
Your fiddle contains a lot of code and may be hard for others to follow. Here is a very simple example of using RegExp
to filter an array.
var names = ['ryan', 'toran', 'steve', 'test'];
var regex = new RegExp('ry');
var filtered = names.filter(function(person) {
return regex.test(person);
});
filtered // => ['ryan']
In fact you could even refactor this to be
var filtered = names.filter(regex.test, regex);
这篇关于如何使用ember.js做一个类似的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!