Skip to content

Commit 511d269

Browse files
author
Simon Rolfe
committed
Add option to sync active tab from active accordion panel
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. To enable tab syncing, pass options as follows: $('#myTab').tabCollapse({ syncTabs: true });
1 parent 035a1da commit 511d269

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

bootstrap-tabcollapse.js

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

@@ -81,7 +80,7 @@
8180
});
8281

8382
if (!$('li').hasClass('active')) {
84-
$('li').first().addClass('active')
83+
$('li').first().addClass('active');
8584
}
8685

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

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

@@ -129,7 +139,7 @@
129139
view.$accordion.append(view._createAccordionGroup(view.$accordion.attr('id'), $heading.detach()));
130140
});
131141

132-
if(this.options.updateLinks) {
142+
if (this.options.updateLinks) {
133143
var parentId = this.$accordion.attr('id');
134144
var $selector = this.$accordion.find('.js-tabcollapse-panel-body');
135145
$selector.find('[data-toggle="tab"], [data-toggle="pill"]').each(function() {
@@ -195,6 +205,9 @@
195205
this.$tabs.after(this.$accordion);
196206
this.$tabs.addClass(this.options.tabsClass);
197207
this.getTabContentElement().addClass(this.options.tabsClass);
208+
if (this.options.syncTabs) {
209+
this.$accordion.on('shown.bs.collapse', { tabCollapse: this }, this.tabSync);
210+
}
198211
};
199212

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

readme.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ You can also use multiple Bootstrap classes in order to, for example, show accor
5252
tabsClass: 'hidden-sm hidden-xs',
5353
accordionClass: 'visible-sm visible-xs'
5454
});
55-
55+
56+
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.
57+
To enable tab syncing, pass options as follows:
58+
$('#myTab').tabCollapse({
59+
syncTabs: true
60+
});
61+
5662
Events
5763
------------
5864

0 commit comments

Comments
 (0)