【问题标题】:Dynamically creating Tabs from List with Bootstrap and Thymeleaf使用 Bootstrap 和 Thymeleaf 从列表中动态创建选项卡
【发布时间】:2017-10-05 14:57:45
【问题描述】:

假设我有一个对象列表“Bar”并且每个 Bar 都有两个属性“title”和“content”: 如何为列表中的每个 Bar 动态创建一个带有调色板的选项卡?

此 html 不起作用:打开时的选项卡在单击每个选项卡后依次列出,如下图所示。:

这是有罪的代码:

<!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>

【问题讨论】:

    标签: twitter-bootstrap thymeleaf


    【解决方案1】:

    请删除过多的封闭 div(那些包含 th:each 属性的)。此外,只有一个选项卡窗格的开头应该是active,而不是全部。您应该在th:classappend 中添加一些条件来实现这一点。

    您的&lt;body&gt; 应包含以下内容。它非常适合我:

    <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>
    

    【讨论】:

    • 谢谢。有改进..但窗格的内容没有改变。 :( 仍然是“内容 1”。(可能是头部不同?
    • 已解决。标题不得有空格。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-09-29
    • 2014-07-12
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多