使用Array Value在Angularjs中搜索过滤

时间:2016-03-28 13:15:19

标签: arrays angularjs filter

当用户多选择peoplepicker中的“作者”时,我会有一个数组。当他们选择一个人时,我想使用ng-repeat="item in filteredart =(filteredArticles |filter: AuthorArray ")

使用相应的DisplayText过滤数组

所以现在我可以使用$scope.AuthorArray="sridhar"之类的单个值过滤数组。但我无法使用多个值$scope.AuthorArray="sridhar,Alan"进行过滤。

帮帮我,我是新人:)

2 个答案:

答案 0 :(得分:1)

我没有明确提出您的问题但据我了解,我认为您需要的是编写自定义过滤器。

在此示例中, matchMyCriteria 将数组中的所有项目与 AuthorArray 数组中的可用名称列表匹配。

HTML:

<div ng-repeat="item in items | filter: matchMyCriteria()">
{{ item }}
</div>

JS:

$scope.items = [{title: "abc", author: "Alan", .....}, ......];
$scope.AuthorArray = ["sridhar", "Alan"];
$scope.matchMyCriteria = function() {
  return function(item) {
    return ($scope.AuthorArray.indexOf(item.author) > -1);
  };
};

还有另一种解决方案,我认为它是好的解决方案。

在此示例中, myFilter 用于根据作者数组过滤项目数组。

HTML:

<div ng-repeat="item in items | myFilter: AuthorArray">
    {{item}}
</div>

JS:

app.filter('myFilter', function() {
  return function(list, criteria) {
    return list.filter(function(l) {
      return (criteria.indexOf(l.author) > -1);
    });
  };
});

答案 1 :(得分:0)

实际上我已经使用了我知道的其他方法。但是我没有在ng-repeat上使用过滤器,而是使用了一个按钮来触发过滤器。谢谢:))

`

 $scope.authorChange = function () {
            var ppick = SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
            var filteredArticlestemp = [];
            $scope.authorarray= getUsers(ppick);
            //$scope.authorarray = $scope.authorarray.join();
            for (var j = 0; j < $scope.authorarray.length; j++) {
                for (var i = 0; i < $scope.filteredArticles.length; i++) {
                    if ($scope.filteredArticles[i].Author == $scope.authorarray[j]) {
                        filteredArticlestemp.push($scope.filteredArticles[i]);
                    }
                }
            }
            $scope.filteredArticles = filteredArticlestemp;
        }
        function getUsers(peoplePicker) {

            // Get information about all users.
            var users = peoplePicker.GetAllUserInfo();
            var userInfo = [];
            if (users.length > 0) {
                for (var i = 0; i < users.length; i++) {
                    var user = users[i];
                    userInfo.push(user.DisplayText);

                }
            }


            return userInfo;
        }

`

`<div id="peoplePickerDiv" ng-model="authorsearch"></div>
 <input type="button" value="Refine" ng-click="authorChange()" />`
相关问题