Skip to content

Instantly share code, notes, and snippets.

@krtek krtek/
Last active Aug 24, 2017

What would you like to do?
Simple Angular.js filter to search in array of items.
app.controller 'ListController', ($scope, $filter) ->
$ = {}
$scope.all_data = [
{title: 'Three men on the bummel', 'author': 'Jerome Klapka Jerome', meta: {isbn: 2234}}
{title: 'Three men on a boat', 'author': 'Jerome Klapka Jerome', meta: {isbn: 1234}}
{title: 'Three musketeers', 'author': 'Alexandre Dumas', meta: {isbn: 4566}}
{title: 'Four tank men and a dog', 'author': 'Janusz Przymanowsky', meta: {isbn: 1222}}
$scope.$watch("search.query", ( ->
#Object is resource
$scope.display_data = $filter("matchAllToProperties")($scope.all_data, $,
), true)
$scope.clearSearch = () ->
$ = {}
angular.module("filters", [])
.filter('matchAllToProperties', () ->
return (input, query, exclude) ->
if not input or not query
return input
bits = (b.toLowerCase() for b in query.split(' ') when b isnt ' ')
filtered = []
for item in input
item_copy = angular.copy(item)
#Remove fields in which we don't want to search
_.each(exclude, (prop)-> delete item_copy[prop])
json = JSON.stringify(item_copy).toLowerCase()
if _.every(bits, (bit) -> json.indexOf(bit) > 0)
return filtered
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.