2014-04-01 61 views
0

我有一些複選框,並在檢查,表格過濾器值。但是如果我選擇active和draft,它不會顯示任何內容,因爲它正在實現AND邏輯,所以我需要使用OR邏輯的過濾器。角度過濾器使用OR而不是AND邏輯

<div class="clearfix"> 
     <input data-ng-model="active" data-ng-true-value="active" class="pull-left" type="checkbox" id="active"/> 
     <label class="pull-left" for="active">Active</label> 
</div> 
<div class="clearfix"> 
     <input data-ng-model="draft" data-ng-true-value="draft" class="pull-left" type="checkbox" id="draft"/> 
     label class="pull-left" for="draft">Draft</label> 
</div> 


<table class="table"> 
<tr ng-repeat="campaign in dashboard.campaigns | filter:all | filter:active | filter:draft"> 
     <td>{{campaign.name}}</td> 
     <td>{{campaign.startDate}}</td> 
     <td>{{campaign.endDate}}</td> 
     <td>{{campaign.status}}</td> 
     <td>{{campaign.tasks}}</td> 
</tr> 

編輯: 這裏是解決

app.filter('byStatus', [function() { 
    return function (campaigns, active, draft) { 
     var tempCampaigns = []; 

     angular.forEach(campaigns, function (campaign) { 
      if(campaign.status == active || campaign.status == draft){ 
       tempCampaigns.push(campaign); 
      } 
     }); 
     return tempCampaigns; 
    }; 
}]); 

<tr ng-repeat="campaign in dashboard.campaigns | byStatus:active:draft"> 

THX所有;)

+1

[如何在angularJS中篩選多個值(OR操作)]的可能重複(http://stackoverflow.com/questions/15868248/how-to-filter-multiple-values-or-operation-in-angularjs) – sloth

+0

這只是我正在尋找的東西,thx:D – Arcagully

回答

1

你只需要實現這樣的自定義過濾器:

angular.module('App.filters', []).filter('activeOrDraft', [function() { 
return function (campaigns) { 
    if (!angular.isUndefined(campaigns)) { 
     var tempCampaigns = []; 
     angular.forEach(campaigns, function (campaign) { 
      if (campaign.active || campaign.draft) { 
      tempCampaigns.push(campaign); 
      } 
     }); 
     return tempCampaigns; 
    } else { 
     return campaigns; 
    } 
}; 
}]); 

,並用它來代替filter:active |過濾:草案是這樣的:

| activeOrDraft 

不知道所有的代碼的意思,所以如果需要添加它。

+0

它不是我需要的100%(我沒有問完整的問題,所以這是我的錯),但它讓我有一個地方,我會管理我的方式現在:) THX:D – Arcagully

+0

詢問您是否需要其他信息。 –