如何使用AngularJs创建多重搜索

时间:2016-07-17 10:54:37

标签: javascript angularjs json

如果我想找到这样的组合位置+国籍,如果我输入例如Keeper England

,我该怎么办?
<section ng-controller="searchController">
                    <input class="field" ng-model="searchField">
                    <div id="update">
                        <ul>
                            <li ng-repeat="item in players | filter: searchField | orderBy: playersOrder">
                                <p class="name">{{item.name}}</p>
                                <p>{{item.position}}</p>
                                <p>{{item.jerseyNumber}}</p>
                                <p>{{item.dateOfBirth}}</p>
                                <p>{{item.nationality}}</p>
                                <p>{{item.contractUntil}}</p>
                                <p>{{item.marketValue}}</p>
                            </li>
                        </ul>
                    </div>
                </section>

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

myApp.controller("searchController", function($scope, $http) {
    $http.get("scripts/players.json").success(function(data) {
        $scope.players = data;
        $scope.playersOrder = 'name';
    });
});

json文件: 玩家     {
          “ID”:2138,           “名字”:“Thibaut Courtois”,           “位置”:“守护者”,           “jerseyNumber”:13,           “出生日期”:“1992年5月11日”,           “国籍”:“比利时””        },        {
          “ID”:2140,           “名字”:“贾马尔·布莱克曼”,           “位置”:“守护者”,           “jerseyNumber”:27,           “出生日期”:“1993年10月27日”,           “国籍”:“英格兰”        },

3 个答案:

答案 0 :(得分:1)

在AngularJS中,您可以创建filter函数:

angular
    .module('myApp', [])
    .controller('searchController', function($scope) {
        // the json response
        $scope.players = [{"id": 2138,"name": "Thibaut Courtois","position": "Keeper","jerseyNumber": 13,"dateOfBirth": "1992-05-11","nationality": "Belgium"}, {"id": 2140,"name": "Jamal Blackman","position": "Keeper","jerseyNumber": 27,"dateOfBirth": "1993-10-27","nationality": "England"}];
        $scope.playersOrder = 'name';
        $scope.searchField = '';
        $scope.myFileterFunction = function(row) {
            var query = angular.lowercase($scope.searchField);
            if (query.indexOf(' ')) {
                var query_array = query.split(' ');
                var search_result = false;
                for (var x in query_array) {
                    query = query_array[x];
                    search_result = (angular.lowercase(row.position).indexOf(query || '') !== -1 
                        || angular.lowercase(row.nationality).indexOf(query || '') !== -1)  
                        ? true 
                        : false;
                }
                return search_result;
            }
            return (angular.lowercase(row.position).indexOf(query || '') !== -1 
                    || angular.lowercase(row.nationality).indexOf(query || '') !== -1);
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<section ng-app="myApp" ng-controller="searchController">
  <input class="field" ng-model="searchField">
  <div id="update">
    <ul>
      <li ng-repeat="item in players | filter: myFileterFunction">
        <p class="name">{{item.name}}</p>
        <p>{{item.position}}</p>
        <p>{{item.nationality}}</p>
      </li>
    </ul>
  </div>
</section>

答案 1 :(得分:1)

filterBy中创建具有item对象的过滤器函数item in players

<li ng-repeat="item in players | filter: filterBy | orderBy: playersOrder">
 ...
</li>

以下方法检查项目是否与NameNationality的搜索查询匹配:

$scope.filterBy = function (item) {
 return item.name.toLowerCase().indexOf($scope.searchField.toLowerCase()) != -1 || 
        item.nationality.toLowerCase().indexOf($scope.searchField.toLowerCase()) != -1
 };

答案 2 :(得分:1)

@Dima Vleskov您可以通过创建自定义过滤器来滚动自己的过滤器。

angular
  .module('euro2016', [])
  .controller('DefaultController', DefaultController)
  .filter('searchPlayer', searchPlayer);
  
  function DefaultController() {
    var vm = this;
    vm.players = players;
  }
  
  searchPlayer.$inject = ['$filter'];
  
  function searchPlayer($filter) {
    return function(data, param) {
      if (angular.isArray(data) && angular.isDefined(param)) {
        var params = param.split(' ');
        if (angular.isDefined(params) && params.length === 2) {        
          var searchTerm = { 
            position: params[0],
            nationality: params[1]
          };
          
          var players = $filter('filter')(data, searchTerm);
          
          return players;
        }
      }
      
      return data;
    }
  }
  
  var players = [
  { id: 1, name: 'Jack Butland', position: 'Keeper', nationality: 'England', img: 'http://www.telegraph.co.uk/content/dam/football/2016/01/25/butland_3548459k-large_trans++qVzuuqpFlyLIwiB6NTmJwfSVWeZ_vEN7c6bHu2jJnT8.jpg' },
  { id: 2, name: 'Thibaut Courtois', position: 'Keeper', nationality: 'Belgium', img: 'http://www.telegraph.co.uk/content/dam/football/2016/01/25/Thibaut_Courtois_3138923k-large_trans++qVzuuqpFlyLIwiB6NTmJwfSVWeZ_vEN7c6bHu2jJnT8.jpg' },
  { id: 3, name: 'Marc-Andre ter Stegen', position: 'Keeper', nationality: 'Germany', img: 'http://www.telegraph.co.uk/content/dam/football/2016/01/25/ter-stegen_3548100k-large_trans++qVzuuqpFlyLIwiB6NTmJwfSVWeZ_vEN7c6bHu2jJnT8.jpg' },
  { id: 4, name: 'Joe Hart', position: 'Keeper', nationality: 'England', img: 'http://www.telegraph.co.uk/content/dam/football/2016/01/25/Joe_Hart_3138807k-large_trans++qVzuuqpFlyLIwiB6NTmJwfSVWeZ_vEN7c6bHu2jJnT8.jpg' }
  ];
  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="euro2016">
  <div ng-controller="DefaultController as ctrl">
    <label>Search</label>&nbsp;<input type="text" ng-model="ctrl.searchTerm">
    <div ng-repeat="player in ctrl.players | searchPlayer: ctrl.searchTerm">
      <h2>{{player.name}} {{player.nationality}}</h2>
      <img ng-src="{{player.img}}" style="height: 200px;"/>
    </div>
  </div>
</div>