简体   繁体   English

使用AngularJS中的复选框按多个条件过滤数据

[英]Filter data by multiple criteria using checkboxes in AngularJS

I'm fairly new to AngularJS and I've been looking to setup a filter function that basically filters a list of upcoming events for the company that I work for. 我是AngularJS的新手,我一直在寻求建立一个过滤器功能,该功能基本上为我工作的公司过滤即将发生的事件的列表。 I was wondering if there is a way to filter the data based on multiple checkboxes. 我想知道是否有一种方法可以基于多个复选框过滤数据。

I basically need to set it up to have 3 checkboxes: 我基本上需要将其设置为具有3个复选框:

  • Headline Event 头条新闻
  • Comedy Event 喜剧活动
  • Free Event 免费活动

If Comedy Event is selected display comedy events. 如果选择了喜剧事件,则显示喜剧事件。 If Headline event is selected display Headline event. 如果选择标题事件,则显示标题事件。 If Free event is selected display free events. 如果选择免费事件,则显示免费事件。 Or if Headline and Comedy are selected show both. 或者,如果选择了“标题”和“喜剧”,请同时显示两者。

I just have not been able to get anything working properly. 我只是无法使任何东西正常工作。

In the JSON file the event types are organized like so: 在JSON文件中,事件类型的组织方式如下:

    "entertainment_type_is_headline": "0",
    "entertainment_type_is_free": "1",
    "entertainment_type_is_comedy": "0",

Here is the link to the data file: https://www.codepile.net/pile/RlrmdDZX 这是数据文件的链接: https : //www.codepile.net/pile/RlrmdDZX

I'm assuming it goes like so... if the event has entertainment_type_is_headline = "1" display all events where entertainment_type_is_headline is = "1" ... but I just can't wrap my head around it... 我假设情况是这样的...如果事件的娱乐性_type_is_headline =“ 1”,则显示所有事件,其中娱乐性的type_is_headline =“ 1” ...但是我无法将其包裹住...

I've seen very small simple examples and large overly-complex examples but nothing seems to work for me. 我见过非常小的简单示例和过于复杂的大型示例,但似乎对我没有任何帮助。

I can think of 2 solutions 我可以想到2个解决方案

1) Pass an object to you filter expression 1)将一个对象传递给您的过滤表达式

<input type="checkbox" ng-model="filter. entertainment_type_is_headline"
       ng-true-value="1" ng-false-value="0">
<input type="checkbox" ng-model="filter.entertainment_type_is_free"
       ng-true-value="1" ng-false-value="0">
<input type="checkbox" ng-model="filter.entertainment_type_is_comedy"
       ng-true-value="1" ng-false-value="0">
<div ng-repeat="event in events | filter:{entertainment_type_is_headline: filter.entertainment_type_is_headline, entertainment_type_is_free: filter.entertainment_type_is_free,entertainment_type_is_comedy: filter.entertainment_type_is_comedy }"></div>

2) Create a custom filter 2)创建一个自定义过滤器

js JS

$scope.filterEvent = function(row){
    if(row.entertainment_type_is_headline.indexOf($scope.filter.entertainment_type_is_headline) >= 0 && ...)
        return true;
    else 
        return false;

} 

html HTML

<div ng-repeat="event in events | filter: filterEvent"></div>

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM