2017-08-02 4 views
0

私は角1.4.8です。私は基本的な検索フィルターを使用してユーザーを探しています。検索は機能しますが、データセット全体を検索しています。たとえば、「Mar」の下で検索すると、3つの検索結果が見つかります。これは、1つの姓に3月が含まれているためです。名前で検索したいのですが。最終的な目標は、見つかったレコードの数を見つけることです:Result {{count}}。したがって、 "St"を検索すると、最初の名前Steve(Stanfordではない)という結果が1つ見つかります。 http://jsfiddle.net/mediaguru/z5shgzxw/1/特定の配列によるAngularJSフィルター

<div ng-app="myApp"> 
    <div ng-controller="MyCtrl"> 
     <input type="text" ng-model="search"> 
     <table> 
     <tbody> 
      <tr ng-repeat="item in items"> 
      <td>ID: {{item.id}}</td> 
      <td>Name: {{item.firstname}}</td> 
      <td>Gender: {{item.lastname}}</td> 
      </tr> 
     </tbody> 
     </table> 
     Result {{count}} 
    </div> 
    </div> 

JS

var myApp = angular.module('myApp', []); 
    myApp.controller('MyCtrl', ['$scope', '$filter', function($scope, $filter) { 


    $scope.items = [{ 
     id: 1, 
     firstname: 'John', 
     lastname: 'Jones' 
    }, { 
     id: 2, 
     firstname: 'Steve', 
     lastname: 'Jones' 
    }, { 
     id: 3, 
     firstname: 'Joey', 
     lastname: 'Maritza' 
    }, { 
     id: 4, 
     firstname: 'Mary', 
     lastname: 'Linqust' 
    }, { 
     id: 5, 
     firstname: 'Marylin', 
     lastname: 'Stanford' 
    }]; 

    $scope.items2 = $scope.items; 

    $scope.$watch('search', function(val) { 
     $scope.items = $filter('filter')($scope.items2, val); 
     $scope.count = $scope.items.length; 
    }); 


    }]); 

答えて

4

あなたがこれを行う必要があります:

$scope.items = $filter('filter')($scope.items2, {firstname:val}); 

それがすべてだ

はここでフィドルです。

+0

@ luis-machillandaさんありがとうございました! – mediaguru

0

あなたは、コードの下に使用して、あなたの問題を解決することができます($scope.items2, {firstname:val});

を記述する必要があります。

var myApp = angular.module('myApp', []); 
myApp.controller('MyCtrl', ['$scope', '$filter', function($scope, $filter) { 

$scope.items = [{ 
    id: 1, 
    firstname: 'John', 
    lastname: 'Jones' 
}, { 
    id: 2, 
    firstname: 'Steve', 
    lastname: 'Jones' 
}, { 
    id: 3, 
    firstname: 'Joey', 
    lastname: 'Maritza' 
}, { 
    id: 4, 
    firstname: 'Mary', 
    lastname: 'Linqust' 
}, { 
    id: 5, 
    firstname: 'Marylin', 
    lastname: 'Stanford' 
}]; 

$scope.items2 = $scope.items; 

$scope.$watch('search', function(val) { 
    $scope.items = $filter('filter')($scope.items2, {firstname:val}); 
    $scope.count = $scope.items.length; 
}); 
}]); 
関連する問題