【问题标题】:build a dynamic multi parameter filter list with AngularJS使用 AngularJS 构建动态多参数过滤器列表
【发布时间】:2014-07-29 06:08:11
【问题描述】:

我有一个 json 酒单,我的愿望是用 AngularJS 构建一个代码来过滤结果

json 文件

[
{ "name": "Wine a", "type": "red", "country": "france", "style": "strong" },
{ "name": "Wine a", "type": "white", "country": "italie", "style": "medium" },
{ "name": "Wine a", "type": "white", "country": "france", "style": "light" },
{ "name": "Wine a", "type": "rose", "country": "usa", "style": "strong" },
{ "name": "Wine a", "type": "champagne", "country": "chili", "style": "medium" },
{ "name": "Wine a", "type": "red", "country": "brazil", "style": "strong" },
{ "name": "Wine a", "type": "red", "country": "france", "style": "strong" }

]

复选框

<div ng-controller="MainCtrl">
    <input ng-model="red" type="checkbox" >red</input>
    <input ng-model="white" type="checkbox" >white</input>
    <input ng-model="rose" type="checkbox" >rose</input>
    <input ng-model="champagne" type="checkbox" >champagne</input>

    <input ng-model="france" type="checkbox" >france/input>
    <input ng-model="italie" type="checkbox" >italie</input>
    <input ng-model="brazil" type="checkbox" >brazil</input>
    <input ng-model="chili" type="checkbox" >chili</input>

    <input ng-model="strong" type="checkbox" >strong</input>
    <input ng-model="medium" type="checkbox" >medium</input>
    <input ng-model="light" type="checkbox" >light</input>

</div>

我的愿望

我的愿望是建立一个动态的多参数过滤器列表,列表的结果将与复选框的值匹配。

  • 如果我检查红色和烈性,将出现在列表中,只有带有这些参数的 json 列表中的葡萄酒(红色和烈性葡萄酒的列表)。

  • 如果我检查红色、白色和法国,将出现在列表中,只有带有这些参数的 json 列表中的葡萄酒(法国红葡萄酒和白葡萄酒的列表)。

我尝试了许多解决方案,但没有结果。好像很难写代码!!

【问题讨论】:

    标签: json angularjs checkbox filter


    【解决方案1】:

    您可以为此构建自定义过滤器并将其与您的过滤器复选框绑定...

    这里是PLUNKER 演示我为所有葡萄酒类型所做的...

    首先构建一个自定义过滤器...这是我的 winetype 过滤器示例...

    app.filter('winetypefilter', function () {
      return function(input, filter) {
        var result = [];
        angular.forEach(input, function (wine) {
            angular.forEach(filter, function (isfiltered, type) {
                if (isfiltered && type === wine.type) {
                    result.push(wine);
                }
            });
        });
        return result;
      };
    });
    

    如你所见,它只是遍历 wine 数组并过滤掉你想要的......

    创建过滤器后,只需将其放入您的 html 中的 ng-repeat...

    <ul>
      <li ng-repeat="wine in wines | winetypefilter:winetypes">
        {{wine.name}} is a {{wine.type}} with {{wine.style}} style from {{wine.country}}
      </li>
    </ul>
    

    如您所见,我们的自定义过滤器需要一个参数,例如 winetypes,它是我们控制器中的复选框值数组和 html 代码中绑定的复选框...

    $scope.winetypes = {red : true,white : true,rose : true, champagne : true};
    

    这里是它的 html...

    <li ng-repeat="(type, value) in winetypes">
      <input type="checkbox" ng-model="winetypes[type]" /> {{type}}
    </li>
    

    您可以为其他属性创建其他过滤器,或者将它们全部放入一个过滤器中,这是您的选择...

    更新

    当然你可以动态启用禁用你的过滤器...有很多方法可以做到这一点我想到的最简单的解决方案就是发送第三个参数作为布尔值来检查过滤器是否启用...

    这里是更新的过滤器示例...

    app.filter('winetypefilter', function () {
      return function(input, filter, isEnable) {
        // if isEnable then filter out wines
        if (isEnable) {
          var result = [];
          angular.forEach(input, function (wine) {
              angular.forEach(filter, function (isfiltered, type) {
                  if (isfiltered && type === wine.type) {
                      result.push(wine);
                  }
              });
          });
          return result;
        } 
        // otherwise just do not any filter just send input without changes
        else{
          return input
        }
      };
    });
    

    这里更新了PLUNKER...

    【讨论】:

    • WickY26,非常感谢您的回答。非常好的解决方案!!很好解释。我已经为其他属性创建了其他过滤器,工作正常!!明智的选择让所有复选框都被选中,并过滤结果,取消选中它们!!再次感谢!!它对我有很大帮助,因为我花了几个小时试图创建这段代码,但没有成功。在你的帮助下,我理解了更好的 AngularJs 编码。问候,Stéphane
    • 真的是一个很棒的社区。我正在学习 AngularJS,一开始听起来有点奇怪。但在你的帮助下,我明白了很多。一个问题,可以使用复选框禁用过滤器。例如,现在有 2 个过滤器,winetype 和 wine country。链接到每个过滤器的复选框可以禁用它?就像代替
    • 将是
    • ,如果一个函数禁用国家过滤器带有复选框。你是从土耳其来的吗??问候
  • @Stéphane 抱歉回答迟了,我不在……我更新了我的解决方案,你可以检查一下……是的,我来自土耳其:D
  • @Stéphane 用于此类实用程序,如果您不需要非常具体的代码,我强烈建议您使用 lodash 之类的实用工具,这里是 lodash 过滤文档 lodash.com/docs#filter。也有一个美好的一周:)
  • 如果您查看过滤器示例,则在开始时会检查是否启用过滤器...因此,当您禁用时(isEnable 为 false)过滤器不会过滤掉任何内容并发送数组是...它与选中或未选中的内容无关,因为当第一个标志(isEnable)为假时,它从不查看过滤器参数......
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签