1

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

2
  • Similar, but with a space meaning OR rather than AND: stackoverflow.com/questions/17994699/… Commented May 25, 2014 at 12:00
  • Thanks @Blackhole, very interesting approach but I went with the answer below as it suited my situation better. Thanks again! Commented May 25, 2014 at 12:22

1 Answer 1

2

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;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks aross. Just what I needed. Went for option 2 as it suited best in this situation. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.