问题
I am fetching my content via ajax call and creating all the collapsible panel on run time. but toggle functionality is not working in this way. I am using backbone marionette collection and item view for creating my panel-group.
I checked with static content and it works fine with static data.
I have checked few links but in my case nothing seems to work.
How to make Twitter Bootstrap Collapse work on dynamically loaded html using ajax
https://github.com/twbs/bootstrap/issues/2274
Here is my html looks like:
<div id="accordion" class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<ul class="home_nav" data-toggle="collapse" data-parent="#accordion"
data-target="NewsMaintenanceMenu">
...
</ul>
<span class=""><i class="glyphicon glyphicon-chevron-right"></i></span>
</div>
<div id="NewsMaintenanceMenu" class="panel-collapse collapse">
<div class="panel-body">
<table class="table table-striped">
<tbody>
....
</tbody>
</table>
</div>
</div>
</div>
</div>
回答1:
I have used jQuery
for time being.
$(".panel").on("click", function(e){
var $_target = $(e.currentTarget);
var $_panelBody = $_target.find(".panel-collapse");
if($_panelBody){
$_panelBody.collapse('toggle')
}
})
回答2:
Call .collapse()
on the newly created panel on ajax success. If you don't know how to do it, please provide a code snippet.
回答3:
/*** I created dynamic Id's for dynamic panel-body*************/
<div class="col-sm-4" ng-repeat="Category in categoryList">
<!-- Category -->
<div class="dropdown">
<button class="dropbtn" ng-bind="Category.name"></button>
<div class="dropdown-content">
<a href="" title="">Business <span class="pull-right">13</span></a>
<a href="" title="">Technology <span class="pull-right">13</span></a>
<a href="" title="">Web <span class="pull-right">13</span></a>
<a href="" title="">Ecommerce <span class="pull-right">13</span></a>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<!-- <div class="panel-heading"> -->
<div class="panel-title">
<a href="#{{Category.name}}" data-toggle="collapse" data-parent="#accordion" title="">View More <span class="pull-right"></span></a>
</div>
<!-- </div> -->
<div id="{{Category.name}}" class="panel-collapse collapse ">
<div class="panel-body">
<a href="" title="">Business <span class="pull-right">13</span></a>
<a href="" title="">Technology <span class="pull-right">13</span></a>
<a href="" title="">Web <span class="pull-right">13</span></a>
<a href="" title="">Ecommerce <span class="pull-right">13</span></a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
回答4:
Another way to accomplish this...
function collapseOnClick(collapseTrigger){
/**
* Manually add collapse on click event.
*
* Because dynamically added Bootstrap collapse elements don't
* work automatically for me most of the time.
*
* 'data-target' is a selector for the collapsing element as per
* the Bootstrap documentation.
* https://getbootstrap.com/docs/4.3/components/collapse/#via-data-attributes
*
* @param {jQuery} collapseTrigger Trigger element for the collapse.
*
*/
var target = collapseTrigger.attr("data-target")
collapseTrigger.on('click',function(){
$(target).collapse('toggle')
})
}
来源:https://stackoverflow.com/questions/26421401/bootstrap-collapse-not-working-when-creating-dyanmically