Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been messing around with Angular.js but I can't seem to resolve this issue,

Take the pen below and try searching the entire name Zoey White - the filter works fine until you start typing 'White'. I'm assuming something in the code isn't picking up a type of 'AND' function which allows you to filter multiple arrays at a time.

Does anyone have any suggestions to solve this?

http://codepen.io/liamtarpey/pen/jDHcB

share|improve this question
    
Similar, but with a space meaning OR rather than AND: stackoverflow.com/questions/17994699/… –  Blackhole May 25 at 12:00
    
Thanks @Blackhole, very interesting approach but I went with the answer below as it suited my situation better. Thanks again! –  Liam Tarpey May 25 at 12:22

1 Answer 1

up vote 1 down vote accepted

Option 1:

Add fullName to users.

$scope.users = [
    { firstName: "Camila", lastName: "Gilson", fullName: "Camila Gilson" },
    { firstName: "Zoey", lastName: "White", fullName: "Zoey White" },
];

Option 2:

Create an custom search function

HTML

<input ng-model="query">
<ul>
  <li ng-repeat="user in users | filter:search" >
    {{user.firstName}} {{user.lastName}}
  </li>
</ul>

Angular Ctrl

function UsersCtrl($scope) {
  // Defina query
  $scope.query = "";
  $scope.users = [
    { firstName: "Camila", lastName: "Gilson" },
    { firstName: "Zoey", lastName: "White" },
  ];

  // Custom search method
  $scope.search = function(user) {
    // Accept everything if query is empty
    if ($scope.query.length <= 0) return true;

    // Store value of query and name as lower case to make it kind of case insensitive
    var query = (""+$scope.query).toLowerCase(),
        fullName = [user.firstName, user.lastName].join(" ").toLowerCase();

    // Return true full name includes the query
    return fullName.indexOf(query) > -1;
  }
}
share|improve this answer
    
Perfect! Thanks aross. Just what I needed. Went for option 2 as it suited best in this situation. :) –  Liam Tarpey May 25 at 12:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.