在Angularjs中分组警报

时间:2017-05-04 17:33:16

标签: angularjs

 我想根据notificationSubject对警报进行分组。如果 > notificationSubject键值长度(dosMinus)> 1意味着它会 >显示在第一个div和notificationSubject键值长度(DOS >等)== 1表示它将显示在第二个div中例如,以下 > json包含notificationSubject = dosMinus有2次和 >保留每一条记录。我想展示' dosMinus'在第1区和 >第二个div中的剩余警报。 ** - >



var myapp = angular.module('myapp', []);
myapp.controller('myctrl', ['$scope', function ($scope) {
    $scope.alerts = {
        "DISPLAY_ALERT_DETAILS": [
            {
                "alertRaisedUserId": 1215
                , "source": 1
                , "clientTimestamp": 1492732800000
                , "severity": "low"
                , "createdByMe": "Y"
                , "notificationSubject": "dosMinus"
                , "notificationDetails": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
    }

        , {
                "alertRaisedUserId": 1215
                , "source": 1
                , "clientTimestamp": 1492732800000
                , "severity": "low"
                , "createdByMe": "Y"
                , "notificationSubject": "dosMinus"
                , "notificationDetails": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
    }

        , {
                "alertRaisedUserId": 1215
                , "source": 1
                , "clientTimestamp": 1492992000000
                , "severity": "low"
                , "createdByMe": "Y"
                , "notificationSubject": "DOS"
                , "notificationDetails": "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"
    }
        , {
                "alertRaisedUserId": 1215
                , "source": 1
                , "clientTimestamp": 1492992000000
                , "severity": "informational"
                , "createdByMe": "Y"
                , "notificationSubject": "EO"
                , "notificationDetails": "Lorem Ipsum is simply dummy text of the printing"
    }
        , {
                "alertRaisedUserId": 1215
                , "source": 1
                , "clientTimestamp": 1492992000000
                , "severity": "informational"
                , "createdByMe": "Y"
                , "notificationSubject": "Late Sales Orders"
                , "notificationDetails": "It has survived not only five centuries"
    }
        , {
                "alertRaisedUserId": 1215
                , "source": 1
                , "clientTimestamp": 1492992000000
                , "severity": "informational"
                , "createdByMe": "Y"
                , "notificationSubject": "Late Purchase Orders"
                , "notificationDetails": "It was popularised in the 1960s with "
    }
        , {
                "alertRaisedUserId": 1215
                , "source": 1
                , "clientTimestamp": 1492992000000
                , "severity": "informational"
                , "createdByMe": "Y"
                , "notificationSubject": "Demand"
                , "notificationDetails": "It was popularised in the 1960s with the release of Letraset sheets containing "
    }
        , {
                "alertRaisedUserId": 1215
                , "source": 1
                , "clientTimestamp": 1492992000000
                , "severity": "informational"
                , "createdByMe": "Y"
                , "notificationSubject": "Spend"
                , "notificationDetails": "more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
    }
        , {
                "alertRaisedUserId": 1215
                , "source": 1
                , "clientTimestamp": 1492992000000
                , "severity": "informational"
                , "createdByMe": "Y"
                , "notificationSubject": "Inventory"
                , "notificationDetails": "It was popularised in the 1960s with the release of Letraset "
    }
    ]
    }
}]);

<html ng-app="myapp">

<head> </head>

<body ng-controller="myctrl">
    <div> 1. Div (Display dosMinus)</div>
    <div> 2. Div (Display DOS....Etc)</div>
</body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

根据您的评论:

  

我想在1st div和&gt;中显示'dosMinus'在第二个div中保留警报。

以下是使用while的简单代码:

ng-if

修改

根据不选择。每个<html ng-app="myapp"> <head> </head> <body ng-controller="myctrl" ng-repeat="a in alerts.DISPLAY_ALERT_DETAILS"> <div> 1. Div <span ng-if="a.notificationSubject == 'dosMinus'">{{a.notificationSubject}}</span></div> <div> 2. Div <span ng-if="a.notificationSubject != 'dosMinus'">{{a.notificationSubject}}</span></div> </body> </html> 出现的情况,你必须得到每个notificationSubject的数量,并根据有选择地使用ng-if的div2 div1。

这里设置和获取计数的JS代码:

var count = {};
$scope.alerts = ...//your JSON

for(var i=0;i<$scope.alerts.DISPLAY_ALERT_DETAILS.length;i++) {
    setCount($scope.alerts.DISPLAY_ALERT_DETAILS[i].notificationSubject);
}

function setCount(notSub) {
    if(count[notSub]) {
        count[notSub]++;
    }
    else {
        count[notSub] = 1;
    }
}
$scope.getCount = function(notSub) {
    return count[notSub];
}

并有条件地使用div,您可以从HTML中调用$scope.getCount来计算。

<body ng-controller="myctrl" ng-repeat="a in alerts.DISPLAY_ALERT_DETAILS">
  <div> 1. Div <span ng-if="getCount(a.notificationSubject)>1">{{a.notificationSubject}}</span></div>
  <div> 2. Div <span ng-if="getCount(a.notificationSubject) == 1">{{a.notificationSubject}}</span></div>
</body>