In AngularJS, is it possible to filter inside of a specific element of an array using only a view? -
i've got data this:
people = [ { names: [ {first: "joe", last: "smith"}, {first: "joseph", last: "smith"}, ... ] }, ... ] in other words, array of objects array of names. example, person called "joe smith" or "joseph smith". how can use filter search first element of names? ie: if typed in "jo" or "smith" find first person. but, if typed in "seph" wouldn't.
i've been looking @ examples on page, there isn't example of filtering inside arrays. here's i've tried gives me error:
<input ng-model="search.names[0].$"> typeerror: cannot set property '$' of undefined
working code
input html
<input ng-model="searchterm">
results
<tr ng-repeat="p in people | filter:searchfunc">...</tr>
controller
$scope.searchfunc = function(person) { var firstpersonsname = [person.names[0]]; // wrapping in array since 'filter' $filter expects array var matches = $filter('filter')(firstpersonsname, $scope.searchterm); // running firstpersonsname through filter searching $scope.searchterm return matches.length > 0; } answer question in title
i played around filter , doesn't seem can go beyond 1 level deep when specifying pattern object e.g. ng-model="search.names" works ng-model="search.names.otherval" doesn't.
also, if filter supported going beyond 1 level deep, still wouldn't able ng-model="search.names[0]". because filter expects input array, elements of names array objects e.g. people[0].names[0] == {first: "joe", last: "smith"} filtering never work.
the way asking purely through view , no code in controller create own custom filter handles case.
Comments
Post a Comment