2013-09-05 2 views
0

여러 개의 체크 박스로 데이터를 필터링하고 싶습니다. 하나의 변수는 여러 개의 필터를 가질 수 있습니다. 4와 5가 확인 될 때 ​​각도 UI 부트 스트랩 다중 체크 박스 필터

Here is the link

은 그래서 예를 들어, 모든 4S와 5 초는 shwon된다.

HTML :

<div class="btn-group" data-toggle="buttons-checkbox"> 
    <pre>{{checkboxes}}</pre> 
    <companies ng-repeat="check in checkboxes"> 
     <button type="button" class="btn" ng-model="check.truth" 
     btn-checkbox-true="check.value" btn-checkbox>{{check.value}}</button> 
    </companies> 
</div> 

및 JS :

$scope.checkboxes = [{"value":"1","truth":false},{"value":"2","truth":false}, {"value":"3","truth":false},{"value":"4","truth":false}]; 


$scope.data = [{"name":"Some Name","value":"1"},{"name":"Another Name","value":"2"},{"name":"Cool Name","value":"3"},{"name":"Funky Name","value":"4"}] 

답변

0

당신은 사용자 정의 필터

<tr ng-repeat="d in data | myfilter:checkboxes"> 

app.filter('myfilter', function() { 
    return function (data, values) { 
     var vs = []; 
     angular.forEach(values, function (item) { 
      if (!! item.truth) { 
       vs.push(item.value); 
      } 
     }); 

     if (vs.length === 0) return data; 

     var result = []; 
     angular.forEach(data, function (item) { 
      if (vs.indexOf(item.value) >= 0) { 
       result.push(item); 
      } 
     }); 
     return result; 
    } 
}); 

Working Demo

를 만들 수 있습니다
+0

문자열에 대해 어떻게 작동합니까 (예 : 이름 열)? 왜 doesnt'if (vs == item.value) {result.push (item);'작동합니까? – DennisKo

+0

@ scr-'vs'는 배열이고,'vs.indexOf (item.value)> = 0'은 배열에 문자열이 들어 있는지 검사합니다. – zsong

+0

이름 열에 대해서는 작동하지 않습니다. http://plnkr.co/edit/TBXyBiYta84fyqNu4wJW?p=preview – DennisKo

관련 문제