ngOptions two level object display

不羁的心 提交于 2019-12-05 07:52:43
Ilan Frumer

ngOptions doesn't support multi-dimensional arrays. You must flatten your array first.

Read more in this answer.

I used a filter:

app.filter('flatten' , function(){
  return function(array){
    return array.reduce(function(flatten, group){
      group.items.forEach(function(item){
        flatten.push({ group: group.name , name: item.name})
      })
      return flatten;
    },[]);
  }
})

And the markup:

<select ng-model="select"
        ng-options="item.name 
                    group by item.group 
                    for item in model | flatten"></select>
user730009
<select>
    <option ng-repeat-start="m in model" ng-bind="m.name"></option>
    <option ng-repeat-end ng-repeat="item in m.items" ng-bind="item.name"></option>
</select>

You might add something like style="font-weight: bold;" on the first option (which is the group label, by the way) and something like style="padding-left: 15px;" on the second option line, which is another repeat for all the first option line. So basically by doing this you just add 2 levels (without optgroup tag, mind you) to your select.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!