Skip to content

Commit e622dfd

Browse files
committed
Merge pull request #1 from simonrolfe/master
Add option to sync active tab from active accordion panel
2 parents 8389d8e + bcef890 commit e622dfd

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

bootstrap-tabcollapse.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
' <div class="panel-body js-tabcollapse-panel-body">' +
3838
' </div>' +
3939
' </div>' +
40-
'</div>'
41-
40+
'</div>';
4241
}
4342
};
4443

@@ -82,7 +81,7 @@
8281
});
8382

8483
if (!$('li').hasClass('active')) {
85-
$('li').first().addClass('active')
84+
$('li').first().addClass('active');
8685
}
8786

8887
var $panelBodies = this.$accordion.find('.js-tabcollapse-panel-body');
@@ -118,6 +117,17 @@
118117
return $tabContents;
119118
};
120119

120+
TabCollapse.prototype.tabSync = function (event) {
121+
var tabId = event.data.tabCollapse.$accordion.find('.panel-collapse.in').attr('id');
122+
if (tabId === undefined || tabId === null) {
123+
return;
124+
}
125+
tabId = tabId.substring(0, tabId.length - 9); //remove -collapse from identifier
126+
127+
event.data.tabCollapse.getTabContentElement().find('.active').removeClass('active');
128+
$('#' + tabId).addClass('active');
129+
}
130+
121131
TabCollapse.prototype.showAccordion = function(){
122132
this.$tabs.trigger($.Event('show-accordion.bs.tabcollapse'));
123133

@@ -130,7 +140,7 @@
130140
view.$accordion.append(view._createAccordionGroup(view.$accordion.attr('id'), $heading.detach()));
131141
});
132142

133-
if(this.options.updateLinks) {
143+
if (this.options.updateLinks) {
134144
var parentId = this.$accordion.attr('id');
135145
var $selector = this.$accordion.find('.js-tabcollapse-panel-body');
136146
$selector.find('[data-toggle="tab"], [data-toggle="pill"]').each(function() {
@@ -204,6 +214,9 @@
204214
this.$tabs.after(this.$accordion);
205215
this.$tabs.addClass(this.options.tabsClass);
206216
this.getTabContentElement().addClass(this.options.tabsClass);
217+
if (this.options.syncTabs) {
218+
this.$accordion.on('shown.bs.collapse', { tabCollapse: this }, this.tabSync);
219+
}
207220
};
208221

209222
TabCollapse.prototype._createAccordionGroup = function(parentId, $heading){

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ You can also use multiple Bootstrap classes in order to, for example, show accor
6060
tabsClass: 'hidden-sm hidden-xs',
6161
accordionClass: 'visible-sm visible-xs'
6262
});
63+
64+
By default, accordion panels do not sync with tabs: the correct panel will be shown based on the active tab, but changing the active accordion panel does not change the active tab.
65+
To enable tab syncing, pass options as follows:
66+
67+
$('#myTab').tabCollapse({
68+
syncTabs: true
69+
});
6370

6471
Events
6572
------------

0 commit comments

Comments
 (0)