javascript 带有 AngularJS ng-repeat 的 OrderBy 布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21378689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OrderBy boolean value with AngularJS ng-repeat
提问by Adam K Dean
I'd like to be able to sort by whether a variable is true or false.
我希望能够按变量是真还是假进行排序。
Let's say we have a variable like so:
假设我们有一个像这样的变量:
groups = {
{ name: 'first', value: true },
{ name: 'second', value: false },
{ name: 'third', value: true },
{ name: 'fourth', value: false }
}
And we can loop through it like so:
我们可以像这样循环遍历它:
<div ng-repeat="group in groups">
{{group.name}} {{group.value}}
</div>
Which will give you the following:
这将为您提供以下信息:
first true
second false
third true
fourth false
But if I wanted to sort by a boolean valuethen I could do this:
但是如果我想按布尔值排序,那么我可以这样做:
<div ng-repeat="group in groups | filter:{value:true}">
{{group.name}} {{group.value}}
</div>
<div ng-repeat="group in groups | filter:{value:false}">
{{group.name}} {{group.value}}
</div>
Which would give me the following output (which I want):
这会给我以下输出(我想要的):
first true
third true
second false
fourth false
Is there a way to do this using orderBy
or filter
in a single ng-repeat
?
有没有办法使用orderBy
或filter
在单个中做到这一点ng-repeat
?
回答by SethWhite
orderBy accepts and sorts by booleans.
orderBy 接受布尔值并按布尔值排序。
Order the repeated element by the the 'value' property of 'group':
通过 'group' 的 'value' 属性对重复元素进行排序:
ng-repeat="group in groups | orderBy: 'value'"
This will reverse the order:
这将颠倒顺序:
ng-repeat="group in groups | orderBy: '-value'"
If you would like to also sort by the 'name' property of 'group':
如果您还想按 'group' 的 'name' 属性排序:
ng-repeat="group in groups | orderBy: ['value','name']"
回答by Sandeep vashisth
use variable name instead obeject
使用变量名而不是对象
orderBy:'Event':false
you will get the desired output like -
您将获得所需的输出,例如 -
first true
third true
second false
fourth false