Search code examples
twitter-bootstrapthymeleaf

Dynamically creating Tabs from List with Bootstrap and Thymeleaf


Given I have a list of objects "Bar" and each Bar has two property "title" and "content": how is it possible to dynamically create a tab with a palette for each Bar in the list ?

This html doesn't work: the tabs when opened are listed one after the other, like in the picture below, after having clicked on each tab.:

wrong tab Here the guilty code:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>Bar's tab</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous" />
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
  </head>
  <body>
   <div class="container">

      <ul class="nav nav-tabs" >
        <div th:each="bar: ${bar_list}">
          <li class="nav-item">
            <a class="nav-link" th:href="@{'#'+ ${bar.title}}" data-toggle="tab"><label th:text="${bar.title}">bar title</label></a>
          </li>
        </div>
      </ul>

      <div class="tab-content" >
        <div th:each="bar : ${bar_list}">
              <div class="tab-pane fade active" th:id="${bar.title}">
                  <p th:text = "${bar.content}">bar content</p> 
              </div>          
        </div>
      </div> 

    </div>
  </body>
</html>

Solution

  • Please remove exceessive, enclosing divs (those which contain th:each attribute). In addition, only one tab-pane should be active at the beginning, not all of them. You should add some condition inside th:classappend to achieve this.

    Your <body> should contain the following content. It works perfectly for me:

    <div class="container">
        <ul class="nav nav-tabs" >
            <li class="nav-item" th:each="bar, barStat: ${bar_list}">
                <a class="nav-link" th:classappend="${barStat.first} ? 'active'" th:href="@{'#'+ ${bar.title}}" data-toggle="tab"><label th:text="${bar.title}">bar title</label></a>
            </li>
        </ul>
    
        <div class="tab-content" >
            <div class="tab-pane fade" th:classappend="${barStat.first} ? 'show active'" th:id="${bar.title}" th:each="bar, barStat : ${bar_list}">
                <p th:text = "${bar.content}">bar content</p> 
            </div>
        </div> 
    </div>