javascript - Angular search for combined key -
say have following object:
var user: {firstname: 'marc', lastname:'ras', age:30};
you have alot of these objects in array called: user_array
now wish display them in table create table:
<table> <thead> <th>full name</th> <th>age</th> </thead> <tbody> <tr ng-repeat="user in user_array"> <td>{{user.firstname + ' '+user.lastname}}</td> <td>{{user.age}}</td> </tr> </tbody> </table>
now wish create input field can search user's full name:
and here kinda ends me :s how possible search combinded key? have:
<input type="text" ng-model="search.fieldname" />
however cant here since field combined of 2 fieldnames
?
add controller
$scope.filternamelastname = function (user) { var fullname = user.firstname + ' '+ user.lastname; if(fullname == $scope.searchparamater) return true; else return false; };
and add filter ng-repeat
<table> <thead> <th>full name</th> <th>age</th> </thead> <tbody> <tr ng-repeat="user in user_array | filter:filternamelastname "> <td>{{user.firstname + ' '+user.lastname}}</td> <td>{{user.age}}</td> </tr> </tbody> </table>
this should give idea on how custom filters work. haven't tested code may have modifications.
Comments
Post a Comment