gpt4 book ai didi

javascript - ng-重复 : sort by time portion of the date only

转载 作者:行者123 更新时间:2023-11-30 16:19:00 25 4
gpt4 key购买 nike

假设您有一组数据,其中包含一个实际上是日期/时间戳的属性。可能有任意天数,但在这种情况下唯一重要的日期部分是日期的时间部分。

如何仅使用 TIME 对 ng-repeat 表达式中的集合进行排序?它需要事先进行某种操作吗?

我目前正在尝试在从数据库填充后立即使用 moment.js 替换时间戳属性。

angular.forEach($scope.timeSlots, function (timeSlot, index) {
timeSlot.BeginTime = moment(timeSlot.BeginTime).format("hh:mm A");
timeSlot.EndTime = moment(timeSlot.EndTime).format("hh:mm A");
});

在模板中使用此值时,排序时仍会考虑日期。因此,根据添加时间的日期,它们可能会乱序。

<tr ng-repeat="timeSlot in timeSlots | orderBy: 'BeginTime'">

我还尝试将一个新属性完全添加到 timeSlot 对象,然后按其排序。不幸的是,时间戳的日期部分似乎仍然存在。

Angular Controller :

angular.forEach($scope.timeSlots, function (timeSlot, index) {
timeSlot.BeginTimeDisplay = moment(timeSlot.BeginTime).format("hh:mm A");
timeSlot.EndTimeDisplay = moment(timeSlot.EndTime).format("hh:mm A");
});

Angular 模板:

<tr ng-repeat="timeSlot in timeSlots | orderBy: 'BeginTimeDisplay'">

最佳答案

为什么不自己计算时间戳:

var date = new Date;
date.setTime(timeSlot.BeginTime);

var seconds = date.getSeconds();
var minutes = date.getMinutes();
var hours = date.getHours();

timeSlot.ts = hours*60*60+minutes*60+seconds;

然后 <tr ng-repeat="timeSlot in timeSlots | orderBy: 'ts'">

关于javascript - ng-重复 : sort by time portion of the date only,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35032290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com