How to do a like filter with ember.js

ぐ巨炮叔叔 提交于 2019-12-21 05:31:13

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/12554335/how-to-do-a-like-filter-with-ember-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!