Search through JSON object with ng class (AngularJS)

I have two JSON objects defined in the controller ( NotificationsController ). One with all notifications, and the other only with the identifier of the latest notifications (last 3 days).

Object Notification Format: ( t_notifications )

 [{"0":"1","1":"4","2":"14-APR-16","3":"ALERT 1","ID":"1","ID_USER":"4","DATE":"14-APR-16","NOTIFICATION":"ALERT 1!"},{"0":"2","1":"1","2":"07-APR-16","3":"ALERT 2!","ID":"2","ID_USER":"1","DATE":"07-APR-16","NOTIFICATION":"ALERT 2!"},{"0":"3","1":"1","2":"13-APR-16","3":"ALERT 3!","ID":"3","ID_USER":"1","DATE":"13-APR-16","NOTIFICATION":"ALERT 3!"}] 

Format of the latest notifications object: ( newest_notifications )

 [{"0":"1","ID_NEWNOTIF":"1"},{"0":"3","ID_NEWNOTIF":"3"}] 

I show all notifications in the following form:

 <div class="panel-body" ng-controller="NotificationsCtrl"> <table datatable="ng" class="row-border hover"> <thead> <tr> <th><b>ID</b>&nbsp;</th> <th><b>ID_USER</b>&nbsp;</th> <th><b>DATE</b>&nbsp;</th> <th><b>NOTIFICATION</b>&nbsp;</th> </tr> </thead> <tbody> <tr ng-repeat="data in t_notifications" ng-class="{selected: data.ID == **TO COMPLETE**> <td>{{ data.ID }}</td> <td>{{ data.ID_USER }}</td> <td>{{ data.DATE }}</td> <td>{{ data.NOTIFICATION }}</td> </tr> </tbody> </table> </div> 

I would like to know how to select only the latest notifications in my table - search through the JSON object newest_notifications - using ng-class?

PS: "selected" is already defined with a blue background color.

+6
source share
2 answers

Just use a strict filter

 ... ng-class="{'selected': (newest_notifications | filter : { ID_NEWNOTIF: data.ID } : true).length !== 0 }" 

Fiddle - https://jsfiddle.net/dyxf5xqj/

+3
source

You can use expression against data.DATE . for instance

 <tr ng-repeat="data in t_notifications" ng-class="{selected: data.DATE > someDateInThePast}"> 
0
source

All Articles