我想根据日期按元素排序。
<ion-item ng-repeat="item in PortfolioIdeaList | orderBy:'CreatedDate'" style="background-color:rgb(25, 26, 0);" ng-click="ShowIdeaDetails(item.Id)">
<div class="row">
<div class="col col-80">
<p> {{item.Title}}</p>
</div>
<div class="col col-20">
<b> {{item.Status}}</b>
</div>
</div>
<div class="row">
<div class="col col-70">
{{item.CreatedDate}}
</div>
<div class="col col-30">
<i class="icon ion-chevron-right icon-accessory"></i>
</div>
</div>
</ion-item>
我从JSON格式的webservices获取标题,创建日期和状态。 创建日期将以字符串格式MM / DD / YYYY HH:MM:SS,例如6/2/1991 2:33:43 PM或12/23/2015 11:55:12 AM。 但它没有正确排序。 如何将字符串转换为日期然后对其进行排序。
答案 0 :(得分:2)
使用您的代码,如果CreatedDate是一个字符串,您的对象将是orderBy字母数字顺序。
如果您想按日期订购,可以:
从服务器获取对象时,将此属性转换为日期
new Date(item.CreatedDate)
创建一个自定义过滤器,将值转换为日期并对其进行排序:
.filter("customOrderBy", function () {
return function (input) {
input.sort(function(a,b){
return new Date(a.CreatedDate) > new Date(b.CreatedDate);
});
return input;
}
});
并在模板中调用它:
ng-repeat="item in PortfolioIdeaList | customOrderBy"
但在所有情况下,您需要将字符串转换为YYYY-MM-DD格式
答案 1 :(得分:0)
:false
为我工作后添加orderBy
<ion-item ng-repeat="item in PortfolioIdeaList | orderBy:'CreatedDate':false" style="background-color:rgb(25, 26, 0);" ng-click="ShowIdeaDetails(item.Id)">