0

I am working an event page with many filters. User can filter out events by severity, description, and duration. Severity levels of events can be 'critical', 'warning', 'ok'. Can anyone show me how to count the number of filtered events with each severity?. I expect something like this

Number of Critical Events: 4

Number of Warning Event: 7

Number of Ok Event: 2

When the number of filtered events change, these numbers will change accordingly. Thank you very much.

Json

{
   'severity': 'Critical',
   'description': 'content1',
   'duration': 1
}
{
   'severity': 'Warning',
   'description': 'content2',
   'duration': 2
}
{
   'severity': 'ok',
   'description': 'content3'
   'duration': 3
}

HTML

<input type='text' ng-model='search.description'>
<input type='text' ng-model='search.severity'>
<input type='text' ng-model='search.duration'>

<div ng-repeat='eventData in eventsData | filter:search:strict>
   <div>eventData.severity</div>
   <div>eventData.description</div>
   <div>eventData.duration</div>
</div>
1

1 Answer 1

0

You can achieve that by filtering the eventsData array combined with the search filter then getting the length.

E.g.

{{(eventsData | filter:search:strict | filter:'Critical').length}}
{{(eventsData | filter:search:strict | filter:'Warning').length}}
{{(eventsData | filter:search:strict | filter:'ok').length}}

Here's the JsFiddle link.

Your HTML

<div ng-controller="YourCtrl">
    <input type="text" ng-model="search.description" />
    <br />
    <br />
    <input type="text" ng-model="search.severity" />
    <br />
    <br />
    <input type="text" ng-model="search.duration" />
    <br />
    <br />
    <p>Number of Critical Events: {{(eventsData | filter:search:strict | filter:'Critical').length}}</p>
    <p>Number of Warning Events: {{(eventsData | filter:search:strict | filter:'Warning').length}}</p>
    <p>Number of Ok Events: {{(eventsData | filter:search:strict | filter:'ok').length}}</p>
    <br />

    <div ng-repeat="eventData in eventsData | filter:search:strict">
        <div> {{eventData.severity}} </div>
        <div> {{eventData.description}} </div>
        <div> {{eventData.duration}} </div>
    </div>
</div>

Your JS

'use strict';
var app = angular.module('myApp', []);
app.controller('YourCtrl', ['$scope', function($scope) {

        $scope.eventsData = [
            {
                'severity': 'Critical',
                'description': 'content1',
                'duration': 1
            }, {
                'severity': 'Warning',
                'description': 'content2',
                'duration': 2
            }, {
                'severity': 'ok',
                'description': 'content3',
                'duration': 3
            }, {
                'severity': 'Warning',
                'description': 'content2',
                'duration': 2
            }, {
                'severity': 'ok',
                'description': 'content3',
                'duration': 3
            }
        ];
    }]);

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.