2016-12-31 36 views
0

角度orderBy與日期字符串? 我正在嘗試使用orderBy來排序ng-repeat。 我們的數據當前使用valueList過濾器不工作。如何使用日期字符串進行角度排序?

我相信他們是按字母數字排序,而不是按日期排序,因爲我的'matchDate'字段是一個字符串。 我的問題是,如何在這個領域的最佳轉換日期正確排序

$scope.valueList= 
[ 
    { 
     "_id" : ObjectId("5862c276d9913952fa80aa11"), 
     "matchDate" : "31 December, 2016", 
     "scoreStatus" : "OPEN" 
    }, 
    { 
     "_id" : ObjectId("58679badd991390f83fbb994"), 
     "matchDate" : "30 December, 2016", 
     "scoreStatus" : "CLOSE" 
    }, 
    { 
     "_id" : ObjectId("58679badd991390f83fbb994"), 
     "matchDate" : "28 December, 2016", 
     "scoreStatus" : "OPEN" 
    } 
] 

這是我的HTML

<div ng-repeat="eachValue in valueList | orderBy: 'matchDate'"> 
    {{eachValue.matchDate}} 
</div> 
+0

如果我沒有誤解的問題 - 這是你以後在做什麼? http://stackoverflow.com/questions/25515431/ng-repeat-filtering-data-by-date-range –

回答

0

您可以按日期這樣的例子進行排序:

<!DOCTYPE html> 
<html lang="en-US"> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
    <body> 
    <script type="text/javascript"> 
     angular.module('app',[]).controller('test',function($scope){ 
     $scope.valueList=[{ 
     "_id" : "5862c276d9913952fa80aa11", 
     "matchDate" : "31 December, 2016", 
     "scoreStatus" : "OPEN" 
     },{ 
     "_id" : "58679badd991390f83fbb994", 
     "matchDate" : "30 December, 2016", 
     "scoreStatus" : "CLOSE" 
     },{ 
     "_id" : "58679badd991390f83fbb994", 
     "matchDate" : "28 December, 2016", 
     "scoreStatus" : "OPEN" 
    }] 

     $scope.myValueFunction = function(date) { 
      return new Date(date) 
     }; 
    }) 
    </script> 
    <div ng-app="app" ng-controller="test"> 
     <div ng-repeat="eachValue in valueList | orderBy: myValueFunction"> 
     {{eachValue.matchDate}} 
     </div> 
    </div> 
    </body> 
</html> 
+0

如果這個日期是unsorted它不會工作 –

0

我建議使用這樣的自定義過濾器:

app.filter('orderByDate', function() { 
    return function(items) { 
    // write your logic for order by date 
    // then return results as items items= results; 
    return items; 
    }; 
}); 

爲排序依據日期寫邏輯,你可以使用moment.js

<div ng-repeat="eachValue in valueList | orderByDate> 
    {{eachValue.matchDate}} 
</div> 

,如果你有問題聯繫

+0

我該如何轉換這個日期格式到片刻請幫助我 –

+0

試試這個時刻(「2016年12月30日」)。 –

相關問題