2017-02-20 3 views
0

나는 운이 없다고 생각했는데, 처음에 나는 그것을 가지고 있다고 생각했지만 지금은 여러 필터의 결과를 일관되게 유지할 수 없습니다.

제 문제는 프런트 엔드에서 필터를 선택하면 처음 사용하는 필터가 작동하지만 그 위에 두 번째 필터를 선택하면 filtered.length 및 페이지 수가 재설정되지만 표시된 데이터가 정확합니다. 아래는 내 컨트롤러에있는 내용입니다

$scope.list= response.data; 
     var memberList = $scope.list; 

     $scope.currentPage = 1; //current page 
     $scope.maxSize = 5; //pagination max size 
     $scope.entryLimit = 25; //max rows for data table 

     $scope.listLength = memberList.length; 
     $scope.noOfPages = 22; 


     $scope.$watch('filterA', 
      function(term) { 
       $scope.filtered = filterFilter($scope.list, term); 
       $scope.noOfPages = Math.ceil($scope.filtered.length/25); 
       $scope.listLength = $scope.filtered.length; 
     }, true); 
     $scope.$watch('filterB', 
      function(term) { 
       $scope.filtered = filterFilter($scope.list, term); 
       $scope.noOfPages = Math.ceil($scope.filtered.length/25); 
       $scope.listLength = $scope.filtered.length; 
     }, true); 
     $scope.$watch('filterC', 
      function(term) { 
       $scope.filtered = filterFilter($scope.list, term); 
       $scope.noOfPages = Math.ceil($scope.filtered.length/25); 
       $scope.listLength = $scope.filtered.length; 
     }, true); 
     $scope.$watch('filterD', 
      function(term) { 
       $scope.filtered = filterFilter($scope.list, term); 
       $scope.noOfPages = Math.ceil($scope.filtered.length/25); 
       $scope.listLength = $scope.filtered.length; 
     }, true); 

그리고보기에는 4 개의 입력이 있습니다. 두 개 이상의 필터를 사용하면 길이가 업데이트되지 않습니다.

<input type="text" ng-model="filterA"> 
<input type="text" ng-model="filterB"> 
<input type="text" ng-model="filterC"> 
<input type="text" ng-model="filterD"> 

{{filtered.length}} 
<uib-pagination total-items="filtered.length" items-per-page="25" next-text="Next" previous-text="Previous" ng-model="currentPage" max-size="maxSize" class="pagination-sm" boundary-link-numbers="true" boundary-links="true" num-pages="noOfPages"></uib-pagination> 
<table> 
    <tbody> 
     <tr ng-repeat="post in filtered | filterA | filterB | filterC | filterD | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit"> 
     <td>{{post.name}}</td> 
     </tr> 
    </tbody> 
    </table> 
+0

더 많은 코드를 보여주고 프런트 엔드에서 사용하는 방법을 알려주십시오. 코드도 –

답변

1

여기에 무슨 일이 일어나고 있는지 확실히 알 수 있습니다. $watch 표현식 중 하나가 실행될 때마다 가장 최근에 변경된 필터가 원래 목록에 적용되므로 모든 필터가 영향을 미칩니다.

아마도 가장 좋은 방법은 변경 될 때마다 네 개의 필터를 모두 적용하는 것입니다.

function applyFilters() { 
    var filters = ['filterA', 'filterB', 'filterC', 'filterD']; 

    $scope.filtered = filters.reduce(function (l, name) { 
     return filterFilter(l, $scope[name]); 
    }, $scope.list); 
    $scope.noOfPages = Math.ceil($scope.filtered.length/25); 
    $scope.listLength = $scope.filtered.length; 
} 

$scope.$watch('filterA', applyFilters, true); 
$scope.$watch('filterB', applyFilters, true); 
$scope.$watch('filterC', applyFilters, true); 
$scope.$watch('filterD', applyFilters, true); 

당신은 또한 당신의보기에서 | filterA | filterB | filterC | filterD를 제거해야 확실 해요 : 여기에 당신이 할 수있는 한 방법입니다. 시계 식에서 이미 필터링하는 경우 유용하지 않을 수 있습니다.

+0

잘 보내 주셔서 감사합니다. 표시되는 결과는 적용된 필터와 일치하지만 'filtered.length'가 잘못되었습니다. 내가 'filtered'객체를 체크했을 때, 너무 많은 값을 가졌지 만, ng-repeat는 적절한 결과를 보여줍니다. 이것은 나에게 기운이 넘칩니다. – TheIntrepidSpiff

+0

@TheIntrepidSpiff 어떻게 그럴 수 있을지 생각하고있어. 'filtered' 객체에 필터와 일치하지 않는 값이 있다고 말하는 것입니까? 길이는 어떻게 지내니? 그것이 전혀 변하지 않습니까, 아니면 고정 된 가치를 유지하고 있습니까? – JLRishe

+0

@JLRrishe : 도와 줘서 고마워! 마침내 제대로 작동했습니다. 내가 필터링 한 방식은 키에 대해 특별히 필터링되지 않았고, 필터링중인 고유 한 것들이 밝혀졌고, 너무 독특하지 않았으며 다른 필드와 교차 충돌을 일으켰습니다. 내가 여러 개의 필터를 사용했을 때 이것은 카운트에서 벗어났습니다. 제안 사항을 업데이트 한 후 입력란을 'field1'키를 추가하여 필드를 필터링하도록 변경해야했습니다. 그런 다음 내 ng- 반복하여 변경하십시오 : '... | filter : filterA | ... '각 필터에 대해 – TheIntrepidSpiff

관련 문제