AngularJS订单截止日期 [英] AngularJS orderBy date

查看:78
本文介绍了AngularJS订单截止日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的内容:

  <div ng-repeat="i in inv">
      <p>{{i.dueDate}}</p>
  </div>

我想先按最早的日期订购。我知道有一种方法可以解决,但我不知道。有人可以帮忙吗?

I'd like to order by oldest date first. I know there's a way to do it but I can't figure it out. Can anyone help?

以下是示例js:

 $scope.inv = [
    {
        name: "Paul",
        dueDate: "5/21/2014"
    },
    {
        name: "Dan",
        dueDate: "5/22/2014"
    },
    {
        name: "Randy",
        dueDate: "1/12/2015"
    }
 ];


推荐答案

您必须定义一个定制器函数,然后使用它在 orderBy 表达式中。例如:

You have to define a customizer function, then use it in orderBy expression. For example:

$scope.dueDateFormatter = function(i) {
   var dateParts = i.dueDate.split(/\//);
   return dateParts[2] 
       + '-' + (dateParts[0] < 10 ? '0' + dateParts[0] : dateParts[0]) 
       + '-' + (dateParts[1] < 10 ? '0' + dateParts[1] : dateParts[1]);
};

<div ng-repeat="i in inv | orderBy:dueDateFormatter">
   <p>{{i.dueDate}}</p>
</div>

演示 。该函数基本上对日期字符串进行重新排序,因此年在第一位,月在后,天在后。如果使用 moment.js 或类似的库,则可以使用其解析器。

Demo. The function basically reorders the date string, so that Year comes first, Month next and Day last. If you use moment.js or similar library, you can use their parsers instead.

这篇关于AngularJS订单截止日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆