代码之家  ›  专栏  ›  技术社区  ›  Will Strohl

ng重复:仅按日期的时间部分排序

  •  1
  • Will Strohl  · 技术社区  · 10 年前

    假设您有一组数据,其中包含一个实际上是日期/时间戳的属性。可以有任何天数,但在本例中,日期中唯一重要的部分是日期的时间部分。

    如何使用TIME仅对ng重复表达式中的集合进行排序?这需要事先进行某种操纵吗?

    我目前正在尝试使用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.forEach($scope.timeSlots, function (timeSlot, index) {
        timeSlot.BeginTimeDisplay = moment(timeSlot.BeginTime).format("hh:mm A");
        timeSlot.EndTimeDisplay = moment(timeSlot.EndTime).format("hh:mm A");
    });
    

    角度模板:

    <tr ng-repeat="timeSlot in timeSlots | orderBy: 'BeginTimeDisplay'">
    
    2 回复  |  直到 10 年前
        1
  •  2
  •   Johannes Jander    10 年前

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

    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'">

        2
  •  0
  •   Ikechi Michael    10 年前

    我认为您应该在几秒钟内将时间转换为数字,并保存在BeginTimeDisplay和EndTimeDisplay中,除非您正在显示这些属性,在这种情况下,您应该将它们保存在不同的属性中。如果这样做,orderByangularJS子句应该很容易工作。