2017-05-29 168 views
0

我發送一個包含對象的數組到我的視圖。 我的目標是爲數組中的每個對象創建一個下拉菜單項。thymeleaf - 從數組中創建動態下拉列表

問題是它只爲第一個對象創建一個項目。

<div class="dropdown"> 
      <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 
       <span class="caret"></span> 
      </button> 
      <ul th:each="u : ${users}" class="dropdown-menu" aria-labelledby="dropdownMenu1"> 
       <li><a th:href="@{/show(id=${u.id})}"><span th:text="${u.name}"></span></a></li> 
      </ul> 
    </div> 

我試過這樣的表,它是工作,但我不想要一張桌子。

<table class="table table-hover"> 
     <thead> 
     <tr> 
      <th>User</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr th:each="u : ${users}"> 
      <td th:text="${u.name}"></td> 
     </tr> 
     </tbody> 
    </table> 

謝謝!

回答

1

the th:每個應該在li標籤內。

<div class="dropdown"> 
       <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 
        <span class="caret"></span> 
       </button> 
       <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> 
        <li th:each="u : ${users}"><a th:href="@{/show(id=${u.id})}"><span th:text="${u.name}"></span></a></li> 
       </ul> 
     </div>