简体   繁体   English

如果数组具有匹配的字符串,则返回角度定制过滤器

[英]Angular custom filter that returns if the array has the matching string

I've been scratching my head for awhile to get this right. 为了解决这个问题,我已经挠了一段时间。 I feel like I am almost there but can't get the last piece right to make it work. 我觉得自己快到了,但是无法正确完成最后一部分。 Here's what I am trying to do. 这就是我想要做的。 I am creating a custom filter that will loop through my json array to see if there's "status" key and if that "status" key contains "SUCCESS" field, it will return all the object that matches that array. 我正在创建一个自定义过滤器,该过滤器将循环遍历json数组以查看是否有“ status”键,并且该“ status”键包含“ SUCCESS”字段,它将返回与该数组匹配的所有对象。 If the "status" key isn't there, then don't show the array object. 如果“状态”键不存在,则不要显示数组对象。 I can see that my loop looks correct since it's figuring out which object array contains the key word but when I try to print out my results, I don't see anything. 我可以看到我的循环看起来正确,因为它正在找出哪个对象数组包含关键字,但是当我尝试打印出结果时,我什么也没看到。 Please help! 请帮忙!

Here's the demo. 这是演示。
https://jsfiddle.net/missggnyc/6r9oophu/20/ https://jsfiddle.net/missggnyc/6r9oophu/20/

HTML HTML

<div ng-app="mySessions">   
    <div ng-controller="sessionController">
  <table>
    <tr>
      <td>Session ID</td>
      <td>Session Name</td>

    </tr>
    <tr ng-repeat="cs in cSessions | successSessionFilter"> 
      <td>{{cs.id}}</td>
      <td>{{cs.session_name}}</td>          
    </tr>
  </table>
</div>

JS JS

var app = angular.module('mySessions', []);
app.controller("sessionController", ['$scope', function($scope){
        $scope.cSessions = [
    {"id":2,"session_name":"BD20","session_description":"","status":["INPROGRESS"]}, 
    {"id":3,"session_name":"CS03","session_description":"","status":["INPROGRESS", "SUCCESS"]},
    {"id":4,"session_name":"MIS99","session_description":"","status":["FAIL","NOTINPROGRESS"]},
     {"id":5,"session_name":"XGY7","session_description":"","status":["SUCCESS", "NOTINPROGRESS"]},
      {"id":6,"session_name":"DCCC2","session_description":""},
        {"id":7,"session_name":"IUOS33","session_description":""}
    ];
}]);
app.filter('successSessionFilter', function(){
        return function(cObj){          
            angular.forEach(cObj, function(value, key){
                angular.forEach(value, function(v, k){
                    //console.log('k is ' + k + ' and v is '+ v);
                    if(k === 'status'){
                        if(v.indexOf("SUCCESS") > -1){ //if it exists
                            //console.log("SUCCESS exists  "+ v.indexOf("SUCCESS"));
                            //console.log(v);
                            return true;
                        }
                        else { //it the status field isn't there, then don't show the object
                            return false;
                        }                       
                    }
                    else 
                        return false;
                })          
            })
    // console.log(cObj);
        }
    });

Here we go 开始了

app.filter('successSessionFilter', function(){
  return function(items){           
    var filtered = items.filter(function(item){
      if(item['status'] && item['status'].indexOf('SUCCESS') != -1){
        return item;
      }
    })
    return filtered;
  }
});

Demo https://jsfiddle.net/ogycjbkL/ 演示https://jsfiddle.net/ogycjbkL/

Simpler way. 更简单的方法。 Just modify tr ng-repeat to 只需将tr ng-repeat修改为

<tr ng-repeat="cs in cSessions | filter:'SUCCESS'"> 
          <td>{{cs.id}}</td>
          <td>{{cs.session_name}}</td>          
</tr>

See it in action: https://jsfiddle.net/hm4h2esj/ 实际观看: https : //jsfiddle.net/hm4h2esj/

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

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