我收到服务器的回复:
0: {
0: {
category: "value1",
date: "date",
id: 100,
title: "MyTitle",
edituser: "user1",
}
hashKey: "02G",
_proto:{
//not important,
}
},
1: {
1: {
category: "value2",
date: "date",
id: 101,
title: "MyTitle2",
edituser: "user2",
}
hashKey: "02H",
_proto:{
//not important,
}
},
需要在这样的列表中显示:
<tr ng-repeat="news in newsList[currentPage] | orderObjectBy:sortingOrder:reverse">
<td>{{news[$index].id}}</td>
<td>{{news[$index].title}}</td>
<td>{{news[$index].category}}</td>
<td>{{news[$index].edituser}}</td>
<td>{{news[$index].date}}</td>
</tr>
它显示很好,但订购不是很好。使用普通orderBy
不会这样做,所以我创建了一个这样的自定义过滤器(在coffeescript中):
app.filter "orderObjectBy", ->
(items, field, reverse) ->
filtered = []
angular.forEach items, (item) ->
filtered.push item
filtered.sort (a, b) ->
a[field] > b[field]
filtered.reverse() if reverse
filtered
但是它仍然不会排序,是否有任何解决方案以角度排序这种响应对象?
答案 0 :(得分:1)
错误在比较功能中。它应该返回数字。 请参阅:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
这有效:
app.filter('orderObjectBy', () ->
(items, field, reverse) ->
filtered = []
for key, item of items
filtered.push(item)
filtered.sort( (a, b) ->
if a[field] > b[field]
return 1
else
return -1
)
if (reverse)
filtered.reverse()
return filtered
);