Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set active tab from active accordion panel #40

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions bootstrap-tabcollapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
' <div class="panel-body js-tabcollapse-panel-body">' +
' </div>' +
' </div>' +
'</div>'

'</div>';
}
};

Expand Down Expand Up @@ -81,7 +80,7 @@
});

if (!$('li').hasClass('active')) {
$('li').first().addClass('active')
$('li').first().addClass('active');
}

var $panelBodies = this.$accordion.find('.js-tabcollapse-panel-body');
Expand Down Expand Up @@ -117,6 +116,17 @@
return $tabContents;
};

TabCollapse.prototype.tabSync = function (event) {
var tabId = event.data.tabCollapse.$accordion.find('.panel-collapse.in').attr('id');
if (tabId === undefined || tabId === null) {
return;
}
tabId = tabId.substring(0, tabId.length - 9); //remove -collapse from identifier

event.data.tabCollapse.getTabContentElement().find('.active').removeClass('active');
$('#' + tabId).addClass('active in');
}

TabCollapse.prototype.showAccordion = function(){
this.$tabs.trigger($.Event('show-accordion.bs.tabcollapse'));

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

if(this.options.updateLinks) {
if (this.options.updateLinks) {
var parentId = this.$accordion.attr('id');
var $selector = this.$accordion.find('.js-tabcollapse-panel-body');
$selector.find('[data-toggle="tab"], [data-toggle="pill"]').each(function() {
Expand Down Expand Up @@ -195,6 +205,9 @@
this.$tabs.after(this.$accordion);
this.$tabs.addClass(this.options.tabsClass);
this.getTabContentElement().addClass(this.options.tabsClass);
if (this.options.syncTabs) {
this.$accordion.on('shown.bs.collapse', { tabCollapse: this }, this.tabSync);
}
};

TabCollapse.prototype._createAccordionGroup = function(parentId, $heading){
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ You can also use multiple Bootstrap classes in order to, for example, show accor
tabsClass: 'hidden-sm hidden-xs',
accordionClass: 'visible-sm visible-xs'
});

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
});

Events
------------
Expand Down