简体   繁体   English

如果日期超过AngularJS ng-repeat,则突出显示行

[英]Highlight rows if date is more than some time in past with AngularJS ng-repeat

I have a small web app using Bootstrap & AngularJS to display table data received remotely via JSON api request. 我有一个使用Bootstrap和AngularJS的小型Web应用程序,以显示通过JSON API请求远程接收的表数据。 Table are generated with ng-repeat. 表是用ng-repeat生成的。 On of the columns contains date of the last update named 'last_seen'. 列中的包含名为“ last_seen”的最新更新的日期。 I want to highlight rows where this field are more than 1 hour in past from the current date. 我想突出显示该字段距当前日期超过1小时的行。

Searching for examples here or on the web most suggests using Date() object or AngularJS 'date' filter. 在此处或在网络上搜索示例,最建议使用Date()对象或AngularJS“日期”过滤器。 Based on these I've managed to achieve desired result with this code: 基于这些,我设法通过以下代码实现了预期的结果:

    <script type="text/javascript">
    var dataApp = angular.module('dataApp', []);
    dataApp.controller('dataShow', function($scope, $http, $filter) {
        $http.get('api.php').success(function(data) {
            $scope.Data = data;
        })
        $scope.DangerDate = new Date() - 1000*60*60; // now - 1 hour
        $scope.dangerDate = $filter('date')($scope.DangerDate, 'yyyy-MM-dd HH:mm:ss');
    });
</script>

' '

    <div ng-app="dataApp" ng-controller="dataShow" class="panel-body">
        <div class="panel panel-default">
            <table class="table table-striped table-hover" id="output">
                <thead>
                    <tr>
                        <th class="{{header}}" ng-repeat="(header, value) in Data[0]"></i></th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="row in Data" ng-class="{ danger: row.last_seen <= dangerDate }">
                        <td ng-repeat="cell in row">{{cell}}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

But these examples didn't compared date directly in HTML template like I did with row.last_seen <= dangerDate . 但是这些示例并没有像我对row.last_seen <= dangerDate那样直接在HTML模板中比较日期。

I also don't really like the way I've used to get 'current date - 1 hour' with JS - creating Date() object, subtracting seconds from it to get that and then using $filter to convert that to text value. 我也不太喜欢用JS获取“当前日期-1小时”的方式-创建Date()对象,从中减去秒数即可得到,然后使用$ filter将其转换为文本值。

Is that ok to do those things or there is a better way? 可以做这些事情还是有更好的方法?

Your proplem isnt about angular but getting date comparison to work in javascript. 您的建议不是关于角度的,但要使日期比较在javascript中起作用。 Below code will be helpful of understanding how such thing works. 下面的代码将有助于您理解此类工作原理。

var d1 = new Date();
var d2 = new Date(d1);

console.log(d1 == d2);   // prints false (wrong!) 
console.log(d1 === d2);  // prints false (wrong!)
console.log(d1 != d2);   // prints true  (wrong!)
console.log(d1 !== d2);  // prints true  (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)

Credits to @moonshadow from the this post ->. 从此帖子->中感谢@moonshadow。 source: Compare two dates with JavaScript 资料来源: 使用JavaScript比较两个日期

After converting your dates to new Date(); 将日期转换为新的Date()之后; objects you can compare them within the 您可以在

ng-class="{ danger: row.last_seen_withdateobject.getTime() <= dangerDate.getTime() }"

Your code would have looked like this. 您的代码将看起来像这样。 Implying the logic. 暗示逻辑。 ( as far as I understood ) Your want to show posts highlighted that are 1 hour or much older than that.What I would do (据我了解)您想显示突出显示的帖子,比1个小时或更早。

set seperately for each row foreaching; 每行分别设置

row.last_seen2 = new Date() - 1000*60*60;
$scope.now = new Date();

ng-class="{ danger: row.last_seen2.getTime() - now.getTime() }"

check out this little fiddle I created for your demonstrating your needs and play with the hours and minutes to see how it works.. https://jsfiddle.net/xykqxmpy/6/ 看看我为您创建的这个小提琴,以展示您的需求,并用小时和分钟来看看它如何工作。.https ://jsfiddle.net/xykqxmpy/6/

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

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