AngularJS orderBy 不适用于自定义函数
AngularJS orderBy is not working with custom function
目前,我已经创建了自定义函数以从 Moment 中找到 fromNow,我想根据函数返回的值对数据进行排序。
我想将 Table 元素放在顶部,从现在开始的天数 较短
在下面的 table 31 Days From Today 中,第 2 列较小,我想将此元素放在顶部,我尝试了以下方法但是没用
谁能帮帮我
Table
AngularJS 排序方式
<tr ng-repeat="x in Holidays|orderBy:findFromNow">
<td>{{$index+1}}</td>
<td>{{x.Day}}</td>
<td>
<span class="label label-warning">{{findFromNow(x.Date)}} Days From Today</span></td>
</tr>
函数
$scope.findFromNow = function (inputDate) {
var m = moment(inputDate, "D-MMM-YY");
var today = moment().startOf('day');
var days = Math.round((today - m) / 86400000);
if (days > 0) {
return 999;
} else {
return (days * (-1));
}
}
节假日JSON
[
{
"Day": "Monday",
"Date": "2-Jan-17"
},
{
"Day": "Monday",
"Date": "20-Feb-17"
},
{
"Day": "Monday",
"Date": "29-May-17"
},
{
"Day": "Monday",
"Date": "3-Jul-17"
},
{
"Day": "Tuesday",
"Date": "4-Jul-17"
},
{
"Day": "Monday",
"Date": "4-Sep-17"
},
{
"Day": "Thursday",
"Date": "23-Nov-17"
},
{
"Day": "Friday",
"Date": "24-Nov-17"
},
{
"Day": "Monday",
"Date": "25-Dec-17"
}
]
您应该在使用前编写单独的函数或检查 inputDate 的类型,因为在 orderBy 中,inputDate 是 Object 而不是 Date
$scope.sortDate = function(inputDate){
var m = moment(inputDate.Date, "D-MMM-YY");
var today = moment().startOf('day');
var days = Math.round((today - m) / 86400000);
if (days > 0) {
return 999;
} else {
return (days * (-1));
}
}
这是 plnkr :
希望对您有所帮助
目前,我已经创建了自定义函数以从 Moment 中找到 fromNow,我想根据函数返回的值对数据进行排序。
我想将 Table 元素放在顶部,从现在开始的天数 较短
在下面的 table 31 Days From Today 中,第 2 列较小,我想将此元素放在顶部,我尝试了以下方法但是没用
谁能帮帮我 Table
AngularJS 排序方式
<tr ng-repeat="x in Holidays|orderBy:findFromNow">
<td>{{$index+1}}</td>
<td>{{x.Day}}</td>
<td>
<span class="label label-warning">{{findFromNow(x.Date)}} Days From Today</span></td>
</tr>
函数
$scope.findFromNow = function (inputDate) {
var m = moment(inputDate, "D-MMM-YY");
var today = moment().startOf('day');
var days = Math.round((today - m) / 86400000);
if (days > 0) {
return 999;
} else {
return (days * (-1));
}
}
节假日JSON
[
{
"Day": "Monday",
"Date": "2-Jan-17"
},
{
"Day": "Monday",
"Date": "20-Feb-17"
},
{
"Day": "Monday",
"Date": "29-May-17"
},
{
"Day": "Monday",
"Date": "3-Jul-17"
},
{
"Day": "Tuesday",
"Date": "4-Jul-17"
},
{
"Day": "Monday",
"Date": "4-Sep-17"
},
{
"Day": "Thursday",
"Date": "23-Nov-17"
},
{
"Day": "Friday",
"Date": "24-Nov-17"
},
{
"Day": "Monday",
"Date": "25-Dec-17"
}
]
您应该在使用前编写单独的函数或检查 inputDate 的类型,因为在 orderBy 中,inputDate 是 Object 而不是 Date
$scope.sortDate = function(inputDate){
var m = moment(inputDate.Date, "D-MMM-YY");
var today = moment().startOf('day');
var days = Math.round((today - m) / 86400000);
if (days > 0) {
return 999;
} else {
return (days * (-1));
}
}
这是 plnkr :
希望对您有所帮助