Auto-generate Div Ids for Bootstrap Accordion Menu binding with Knockout js

前端 未结 1 833
孤街浪徒
孤街浪徒 2020-12-21 23:52

I am creating a side menu using bootstrap accordion collapse and making use of knockout js binding. My problem is in assigning the right HTML element ids for toggle and coll

相关标签:
1条回答
  • 2020-12-22 00:22

    well i already done something like this before hope this helps

        <div class="panel-heading" role="tab" id="headingOne" data-bind="foreach: accordionItems()">
            <a style="text-decoration:none;" data-toggle="collapse" data-parent="#accordion"  aria-expanded="true" aria-controls="collapseOne"   data-bind="attr:{href:'#collapseOne'+$index() }" >
                <p class="panel-title" data-bind="text: nameHead"></p>
            </a> <!--binding a collapse header here.-->
            <div  class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne" data-bind="attr:{id:'collapseOne'+$index()}">
                <ul class="Newsbar panel-body" data-bind="foreach: list">
                    <li><a data-bind="attr: { href: url }, text: linkText"></a></li>
                </ul>
            </div>
        </div>
    

    Let me explain here :

    You just need to create dynamic id and href in oder to work with this stuff . Well lucky you have one loop over if incase there exists multiple loops use $parentContext.index() and so on .

    You just need to use attr to create dynamic id and href using $index() which gives you unique id everytime it loops .

    0 讨论(0)
提交回复
热议问题