2016-03-28 162 views
0

我将有一个数组,当用户从多选择peoplepicker.when“作者”,他们选择的人,我想用ng-repeat="item in filteredart =(filteredArticles |filter: AuthorArray ")搜索过滤中Angularjs与数组值

与相应的DisplayText阵列过滤所以现在我可以用像$scope.AuthorArray="sridhar"这样的单个值来过滤数组。但我不能使用多个值$scope.AuthorArray="sridhar,Alan"进行过滤。

帮帮我吧,我是新:)

+0

目前尚不清楚你想要什么筛选。你想要过滤的数组值是什么样的?请为您的问题提供更多代码/上下文。 – ryanyuyu

回答

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()" />` 
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); 
    }); 
    }; 
});