Open angular-ui-bootstrap accordion based on ID

冷暖自知 提交于 2019-12-11 13:05:51

问题


I have an accordion:

<accordion>
 <accordion-group  ng-repeat="group in groups" heading="{{group.title}}" id="{{group.id}}" is-open="group.open">
   <table class="table">            
       <tr>
         <td>{{group.definition}}</td>
       </tr>
    </table>
 </accordion-group>
</accordion>

and each accordion has a unique ID.

I know how to open an accordion based on its position:

 <button ng-click="groups[0].open = !groups[0].open">Toggle Title 1 based on index</button>

but how do I open an accordion based on its ID?

My current attempt is here: http://plnkr.co/edit/c3GeaWfOgZ2YoQb2kUbW


回答1:


Following on from my comment, try something like this...

In your controller, create a map to store open group information

$scope.accordionGroups = {};

If you want one group to default open, try this

$scope.accordionGroups[groups[0].id] = true;

Then use this in your template

<accordion-group ng-repeat="group in groups"
    heading="{{group.title}}" id="{{group.id}}"
    is-open="accordionGroups[group.id]">

To toggle a group with your button, use this

<button ng-click="accordionGroups.title1 = !accordionGroups.title1">Toggle title1</button>

Plunker



来源:https://stackoverflow.com/questions/29290429/open-angular-ui-bootstrap-accordion-based-on-id

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