Angularjs:NG选项:如何订购与组和非组选项选项、Angularjs、NG

2023-09-13 04:48:05 作者:不改变是受苦

我有我的组吴选项看起来像这样

I have my group by ng-options looks like this

    <select ng-model="selected" ng-options="d.title group by d.group for d in data"></select>

下面是我的数据

$scope.data = [
    {
        group:"",
        title:"No GroupA"
    },
    {
        group:"Group_1",
        title:"1"
    },
    {
        group:"",
        title:"No GroupB"
    },
    {
        group:"Group_2",
        title:"2"
    },
    {
        group:"",
        title:"No GroupC"
    }
];

问题是,这造成在此选择菜单,而不是顺序相同我的数据列表的底部的OPTGROUP

the problem is that this creates the optgroup on the bottoms of this select menu, not the order as same as my data list.

No GroupA
No GroupB
No GroupC
[Group_1]
 1
[Group_2]
 2

我想制作:

No GroupA
[Group_1]
 1
No GroupB
[Group_2]
 2
No GroupC

下面是小提琴

谢谢!

推荐答案

据我所知,你不能有一个混合的选择 - 内部和外部的群体选项

As far as I know, you can't have a mixed select - options within and without groups.

您可以为这些选项创建一个通用组:

You could create a generic group for these options:

<select ng-model="selected" 
        ng-options="d.title group by (d.group ==='' ? 'No_Group' : d.group) for d in data">
</select>

下面是更新后的小提琴。