I have a search function as part of a backbone app that I'm writing. I'd like some help expanding it out. Ideally I'd like it to be able to
- search more than one category in my model and
- not have to be such an exact match (perhaps using RegEx?)
Here's my code:
search: function(e) {
var search = this.$('.search').val(); //pulling in from the search bar
results = namesCollection.filter(function(item) { return item.get('First_Name') == (search);}); //returns a filtered collection that matches (exactly) the search results
filteredCollection.reset(results); //this just resets the current view
}
The code works, but right now it only returns results for an exact match on First_Name. Is there any way to have it match both First_Name and Last_Name? Also I'm trying to find a way to have it return for incomplete searches. So that entering 'Joh' in the search bar, would return a First_Name of John and Johanna.
Here's what my data looks like (simplified, of course):
var data = [
{
"First_Name": "John",
"Last_Name": Smith
},
{
"First_Name": "Johanna",
"Last_Name": "Greene"
}
];
And namesCollection refers to a backbone collection of all the names in my data set. Thanks!
if
statements, or use the||
operator. For example,return item.get('First_Name') == search || item.get('Last_Name') == search
– quietmint