gpt4 book ai didi

javascript - orderBy angularJS 在 ng-switch 里面 ng-repeat 里面?

转载 作者:行者123 更新时间:2023-11-30 12:42:45 27 4
gpt4 key购买 nike

我有一个 ng-repeat 使用 ng-switch 来分隔 ng-repeat 内部的部分,我想在 ng-switch 组上应用一个 orderby 过滤器,但我似乎无法让它工作:

    <div ng-controller="MyCtrl">
I have {{friends.length}} friends. They are:
<ul>
<li ng-repeat="friend in sortedFriends">
<ng-switch on="$first || friend.age != sortedFriends[$index-1].age">
<div ng-switch-when="true" class="group">Group of {{friend.age}} years old. Previous: <i>{{sortedFriends[$index-1].age}}</i></div>
</ng-switch>
<b>[{{$index + 1}}]</b> {{friend.name}} who is {{friend.age}} years old.
</li>
</ul>
</div>

jsfiddle有什么想法吗?

这就是我想要实现的目标:

   <div ng-controller="MyCtrl">
I have {{friends.length}} friends. They are:
<ul>
<li ng-repeat="friend in sortedFriends | orderBy:predicate">
<ng-switch on="$first || friend.age != sortedFriends[$index-1].age" ng-click="predicate = 'age'; reverse=!reverse">
<div ng-switch-when="true" class="group">Group of {{friend.age}} years old. Previous: <i>{{sortedFriends[$index-1].age}}</i></div>
</ng-switch>
<b>[{{$index + 1}}]</b> {{friend.name}} who is {{friend.age}} years old.
</li>
</ul>
</div>

更新 fiddle : http://jsfiddle.net/CvKNc/58/

最佳答案

讨论结果:

FIDDLE

这按年龄排序并在点击时更改顺序并单击按名称排序的组名称

因为你正在检查喜欢

friend.age != friends[$index-1].age

你不能在 ng-repeat 中使用 orderBy,因为 ng-repeat 使用过滤器返回的有序数组,但你正在检查初始无序数组

最好的方法是使用作用域函数进行排序

$scope.orderFriends = function(ageOrder,nameOrder){
return orderByFilter($scope.friends,[ageOrder,nameOrder]);
}

ageOrder 和 nameOrder 采用 "+age""-name" 等值。

请注意,单击组标题会更改所有组的名称顺序。

我最终会使用分组功能(使用 underscorejs 之类的东西)对 friend 进行分组,并对姓名和年龄使用 orderby 过滤器。这样名称就可以在组内单独排序。 ( FIDDLE ,分组可能可以做得更好,但基本思想就在那里)

关于javascript - orderBy angularJS 在 ng-switch 里面 ng-repeat 里面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23843932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com