Thursday, 24 October 2013

Making Bootstrap UI Accordion work with Bootstrap 3

The latest templates of Angular UI Bootstrap don’t play well with Bootstrap 3. A few days back, I tried using the accordion directive of Bootstrap UI and was disappointed with the outcome. It happened so because of the CSS classes. There are significant differences between the CSS classes in version 2.x and 3.0.

In the templates file, there are two templates defined for accordion, accordion.html and accordion-group.html. To make the accordion work with Bootstrap 3, we need to modify the CSS classes used in the directive. Following are the modified templates:

angular.module("template/accordion/accordion-group.html", []).run(["$templateCache", function ($templateCache) {
    $templateCache.put("template/accordion/accordion-group.html",
      "<div class=\"panel panel-default\">\n" +
      "  <div class=\"panel-heading\" ><a class=\"accordion-toggle\" ng-click=\"isOpen = !isOpen\" accordion-transclude=\"heading\">{{heading}}</a></div>\n" +
      "  <div class=\"panel-collapse collapse in\" collapse=\"!isOpen\">\n" +
      "    <div class=\"panel-body\" ng-transclude></div>  </div>\n" +
      "</div>");
}]);

angular.module("template/accordion/accordion.html", []).run(["$templateCache", function ($templateCache) {
    $templateCache.put("template/accordion/accordion.html",
      "<div class=\"panel-group\" ng-transclude></div>");
}]);


Make these changes and use the accordion directive just as in Bootstrap 2.
<div data-accordion="" data-close-others="true">
        <div data-accordion-group="" data-heading="Heading 1">
            Contents in first group
        </div>
        <div data-accordion-group="" data-heading="Heading 2">
            Contents in second group
        </div>
</div>


Out of other directives, some of them work well with Bootstrap 3 classes, but some of them don’t. Tweaking other directives may not be this easy. But there are ways to make them work.

Update (Jan 2014): Bootstrap UI for Bootstrap 3 is officially released. Refer to the project's source for the template.

Happy coding!

7 comments:

  1. Thanks for this. Also, if you look at the example here: http://getbootstrap.com/javascript/#collapse then the accordion-toggle should also be wrapped in a h4.panel-title

    ReplyDelete
    Replies
    1. Yes. That will make the heading look more distinct. Thanks for pointing!

      Delete
    2. Hi Ravi, Thanks for this. We were struggling with this issue and was holding up our upgrade to Bootstrap 3.0. May be you should contribute this to the Angular Boostrap project. The WIP template in Github didn't work for us. Thanks again.

      Delete
    3. Hi Aby,

      Really glad that it helped you guys. I will try to contribute to the Bootstrap UI project, but before that I need to learn a lot more :)

      Delete
  2. The link you provide in your update is broken for me.

    ReplyDelete
    Replies
    1. Jonathan,

      That branch is removed from GitHub project as well. I will update the link.

      Delete
    2. It looks like the updates supporting Bootstrap 3 have been merged into the main repository now: https://github.com/angular-ui/bootstrap/commit/444c488d62e1b653a1b6c98f0bbeed4040835131

      Latest release appears to be 0.9.0 which now supports Bootstrap 3.

      Thanks for the help!

      Delete