使用AngularJS ng-repeat的OrderBy布尔值

时间:2022-10-19 20:38:28

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 value then 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或在单个ng-repeat中过滤?

2 个解决方案

#1


7  

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':

如果您还想按“组”的“名称”属性排序:

ng-repeat="group in groups | orderBy: ['value','name']"

#2


4  

use variable name instead obeject

使用变量名而不是obeject

orderBy:'Event':false

you will get the desired output like -

你将获得所需的输出,如 -

first true    
third true
second false
fourth false

#1


7  

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':

如果您还想按“组”的“名称”属性排序:

ng-repeat="group in groups | orderBy: ['value','name']"

#2


4  

use variable name instead obeject

使用变量名而不是obeject

orderBy:'Event':false

you will get the desired output like -

你将获得所需的输出,如 -

first true    
third true
second false
fourth false

相关文章