使用 ng-class (AngularJS) 搜索 JSON 对象
Searching through JSON Object with ng-class (AngularJS)
我在控制器 (NotificationsController
) 中定义了两个 JSON 对象。一个包含所有通知,另一个包含最新通知的 ID(最近 3 天)。
对象的格式"notifications": (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!"}]
对象格式 "newest notifications": (newest_notifications
)
[{"0":"1","ID_NEWNOTIF":"1"},{"0":"3","ID_NEWNOTIF":"3"}]
我在这样的视图中显示所有通知:
<div class="panel-body" ng-controller="NotificationsCtrl">
<table datatable="ng" class="row-border hover">
<thead>
<tr>
<th><b>ID</b> </th>
<th><b>ID_USER</b> </th>
<th><b>DATE</b> </th>
<th><b>NOTIFICATION</b> </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>
我想知道如何在我的 table 中 select 只有最新的通知 - 搜索 JSON 对象 newest_notifications
- 使用 ng- class?
PS: "selected" 已定义为蓝色背景色。
您可以对 data.DATE
使用表达式。例如
<tr ng-repeat="data in t_notifications" ng-class="{selected: data.DATE > someDateInThePast}">
只需使用严格的 filter
... ng-class="{'selected': (newest_notifications | filter : { ID_NEWNOTIF: data.ID } : true).length !== 0 }"
Fiddle - https://jsfiddle.net/dyxf5xqj/
我在控制器 (NotificationsController
) 中定义了两个 JSON 对象。一个包含所有通知,另一个包含最新通知的 ID(最近 3 天)。
对象的格式"notifications": (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!"}]
对象格式 "newest notifications": (newest_notifications
)
[{"0":"1","ID_NEWNOTIF":"1"},{"0":"3","ID_NEWNOTIF":"3"}]
我在这样的视图中显示所有通知:
<div class="panel-body" ng-controller="NotificationsCtrl">
<table datatable="ng" class="row-border hover">
<thead>
<tr>
<th><b>ID</b> </th>
<th><b>ID_USER</b> </th>
<th><b>DATE</b> </th>
<th><b>NOTIFICATION</b> </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>
我想知道如何在我的 table 中 select 只有最新的通知 - 搜索 JSON 对象 newest_notifications
- 使用 ng- class?
PS: "selected" 已定义为蓝色背景色。
您可以对 data.DATE
使用表达式。例如
<tr ng-repeat="data in t_notifications" ng-class="{selected: data.DATE > someDateInThePast}">
只需使用严格的 filter
... ng-class="{'selected': (newest_notifications | filter : { ID_NEWNOTIF: data.ID } : true).length !== 0 }"
Fiddle - https://jsfiddle.net/dyxf5xqj/