0

I have a problem on my filter when searching for a numerical value. I wish to have an exact search (using custum filter) on a "matricule" but it is a problem in my code and I do not know. this is my index :

<html>
  <head>
    <meta charset='utf-8'>
    <script src="js/angular.js"></script>
    <script src="js/app.js"></script>
    <link rel="stylesheet" href="css/bootstrap.css">
  </head>
    <body ng-app="MyApp">
    <div ng-controller="MyCtrl">

      <h3>Filter by numeric value</h3>
      <form class="form-inline">
        <input ng-model="match.matricule" type="text" placeholder="Filter by matricule" autofocus>
      </form>
      <ul ng-repeat="friend in (result = (friends | exact: match ) ) ">
        <li>{{friend.matricule}} - {{friend.name}} ({{friend.age}})</li>
      </ul>
      <p ng-show="result.length == 0"> Not Found </p>
      {{result}}

    </div>

  </body>
</html>

this is my controller :

var app = angular.module("MyApp", []);

app.controller("MyCtrl", function($scope) {
  $scope.friends = [
    { matricule :1 , name: "Peter",   age: 20 , region : 'analamanga1'},
    { matricule :2 ,name: "Pablo",   age: 55 , region : 'analamanga2'},
    { matricule :3 ,name: "Linda",   age: 20 , region : 'analamanga3'},
    { matricule :4 ,name: "Marta",   age: 37 , region : 'analamanga4'},
    { matricule :5 ,name: "Othello", age: 20 , region : 'analamanga5'},
    { matricule :11 ,name: "Markus",  age: 32 , region : 'analamanga6'}
  ];

  $scope.filterFunction = function(element) {
    return element.name.match(/^Ma/) ? true : false;
  };

})

app.filter('exact', function(){
  return function(items, match){
    var matching = [], matches, falsely = true;
    
    // Return the items unchanged if all filtering attributes are falsy
    angular.forEach(match, function(value, key){
      falsely = falsely && !value;
    });
    if(falsely){
      return items;
    }
    
    angular.forEach(items, function(item){ 
      matches = true;
      angular.forEach(match, function(value, key){ // e.g. 'all', 'title'
        if(!!value){ 
          matches = matches && (item[key] === value);  
        }
      });
      if(matches){
        matching.push(item);  
      }
    });
    return matching;
  }
});

sample : plnkr SAMPLE

THANKS

ALFA
  • 263
  • 1
  • 5
  • 20

1 Answers1

1

Omg... if u only want filter this you make it so hard, u can use angular filter to make its so easy, u only need do this:

HTML:

<html>
  <head>
    <meta charset='utf-8'>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="app.js"></script>

  </head>
    <body ng-app="MyApp">
    <div ng-controller="MyCtrl">

      <h3>Filter by numeric</h3>
      <form class="form-inline">
        <input ng-model="match.matricule" type="text" placeholder="Filter by matricule" autofocus>
      </form>

      <ul ng-repeat="friend in friends | filter:match:strict">
        <li>{{friend.matricule}} - {{friend.name}} ({{friend.age}})</li>
      </ul>
      <p ng-show="result.length == 0"> Not Found </p>

    </div>

  </body>
</html>

CONTROLLER

var app = angular.module("MyApp", []);

app.controller("MyCtrl", function($scope) {
  $scope.friends = [
    { matricule :1 , name: "Peter",   age: 20 , region : 'analamanga1'},
    { matricule :2 ,name: "Pablo",   age: 55 , region : 'analamanga2'},
    { matricule :3 ,name: "Linda",   age: 20 , region : 'analamanga3'},
    { matricule :4 ,name: "Marta",   age: 37 , region : 'analamanga4'},
    { matricule :5 ,name: "Othello", age: 20 , region : 'analamanga5'},
    { matricule :11 ,name: "Markus",  age: 32 , region : 'analamanga6'}
  ];
});

Reference: https://docs.angularjs.org/api/ng/filter/filter

----------------------- EDITED -------------

Ok, sorry I didn't understand your question very well. Let's try this:

HTML:

<html>
  <head>
    <meta charset='utf-8'>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="app.js"></script>

  </head>
    <body ng-app="MyApp">
    <div ng-controller="MyCtrl">

      <h3>Filter by numeric</h3>
      <form class="form-inline">
        <input ng-model="matricule" type="text" placeholder="Filter by matricule" autofocus>
      </form>

      <ul ng-repeat="friend in friends | filter:myFilter">
        <li>{{friend.matricule}} - {{friend.name}} ({{friend.age}})</li>
      </ul>
      <p ng-show="result.length == 0"> Not Found </p>

    </div>

  </body>
</html>

CONTROLLER:

var app = angular.module("MyApp", []);

app.controller("MyCtrl", function($scope) {
  $scope.friends = [
    { matricule :1 , name: "Peter",   age: 20 , region : 'analamanga1'},
    { matricule :2 ,name: "Pablo",   age: 55 , region : 'analamanga2'},
    { matricule :3 ,name: "Linda",   age: 20 , region : 'analamanga3'},
    { matricule :4 ,name: "Marta",   age: 37 , region : 'analamanga4'},
    { matricule :5 ,name: "Othello", age: 20 , region : 'analamanga5'},
    { matricule :11 ,name: "Markus",  age: 32 , region : 'analamanga6'}
  ];

  $scope.myFilter = function (data) {

    if ($scope.matricule === '' || !$scope.matricule) return true;
    var reg = RegExp("^" + $scope.matricule + "$");
    return reg.test(data.matricule);
};
});

Reference: exact filter in angular

--------- UPDATED v2 -----

To display "none" from the list if the search field is empty, need save result filtered:

HTML:

<html>
  <head>
    <meta charset='utf-8'>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="app.js"></script>

  </head>
    <body ng-app="MyApp">
    <div ng-controller="MyCtrl">

      <h3>Filter by numeric</h3>
      <form class="form-inline">
        <input ng-model="matricule" type="text" placeholder="Filter by matricule" autofocus>
      </form>

      <ul ng-repeat="friend in (filteredItems = (friends | filter:myFilter))">
        <li>{{friend.matricule}} - {{friend.name}} ({{friend.age}})</li>
      </ul>
      <p ng-show="filteredItems.length == 0"> Not Found </p>

    </div>

  </body>
</html>
Community
  • 1
  • 1
Javierif
  • 632
  • 1
  • 6
  • 18
  • My problem is when I search "1" , I want have "1" only in result but not "1" and "11". it not work with this code , – ALFA Nov 25 '15 at 12:04
  • @Javief -->Is it possible to display none from the list if the search field is empty? – ALFA Nov 26 '15 at 16:41
  • Is there is no change in the controller? I already tried that but it does not work – ALFA Nov 27 '15 at 05:16
  • hello Javief ==> I wish to have a "Chef1 "and "Chef2" searching on the same list and I know how to do at my ng-model. this is a plnkr example : http://plnkr.co/edit/sofXBZfNWEaKP0L4y5fZ?p=preview – ALFA Dec 02 '15 at 07:44