2017-02-24 31 views
0

我有一個彩色濾光片,呈三角這樣:是否有可能隱藏「複選框」使用th:除非?

<input type="checkbox" id="orange" class="orange" value="orange"/>orange 
      <br/> 
      <input type="checkbox" id="peach" value="peach"/>peach 
      <br/> 
      <input type="checkbox" id="terracotta" value="terracotta"/>terracotta 
      <br/> 
      <input type="checkbox" id="coffee" value="coffee"/>coffee 
      <br/> 
      <input type="checkbox" id="browne" value="browne"/>browne 
      <br/> 
      <input type="checkbox" id="rose" value="rose"/>rose 
      <br/> 
      <input type="checkbox" id="red" value="red"/>red 
      <br/> 

我recive顏色從DB。

<div th:each="model : ${allColor}"> 

          <span th:text="${model.color}"/> 

我可以隱藏顏色,使用Thymeleaf,這不是數據庫? 例如現在我沒有玫瑰,咖啡和桃子的顏色,但也許將來我會擁有這種顏色。我想做顏色驗證,如果顏色在DB中,用戶可以在UI複選框中看到,否則複選框隱藏。我讀了大約th:ifth:unless。是否有可能使用Thymeleaf?

如果我嘗試這樣做:

<input type="checkbox" th:if="${model.color==coffee}"id="coffee" value="coffee"/>coffee 

它不工作。

回答

1

除了隱藏顏色之外,還應該創建帶有循環的顏色複選框。就像這樣:

<th:block th:each="model : ${allColor}" th:with="color=${model.color}"> 
    <input type="checkbox" th:id="${color}" th:class="${color}" th:value="${color}"/> <span th:text="${color}" /> 
    <br /> 
</th:block> 

至於爲什麼你原來的嘗試沒有成功,很難說沒有看到更多的HTML。我猜測$ {model.color}是不確定的,因爲你不在循環中?此外,你錯過了'咖啡'周圍的報價。像這樣:`$ {model.color =='coffee'

類似這樣的東西也可以,但我會推薦循環。

<input th:if="${allColor.contains('orange')}" type="checkbox" id="orange" class="orange" value="orange"/>orange<br/> 
<input th:if="${allColor.contains('peach')}" type="checkbox" id="peach" value="peach"/>peach<br/> 
<input th:if="${allColor.contains('terracotta')}" type="checkbox" id="terracotta" value="terracotta"/>terracotta<br/> 
<input th:if="${allColor.contains('coffee')}" type="checkbox" id="coffee" value="coffee"/>coffee<br/> 
<input th:if="${allColor.contains('browne')}" type="checkbox" id="browne" value="browne"/>browne<br/> 
<input th:if="${allColor.contains('rose')}" type="checkbox" id="rose" value="rose"/>rose<br/> 
<input th:if="${allColor.contains('red')}" type="checkbox" id="red" value="red"/>red<br/>}`. 
+0

'<輸入日:如果= 「$ {allColor.contains( '橙色')}」 類型= 「複選框」 ID = 「橙色」 類= 「橙色」 的值= 「橙色」/> orange'在這個變體中,我看不到'checkbox',我看起來像一個'span' – Viking

+0

是'allColor'列表?哦,如果你想讓它工作,你必須將allColor轉換爲字符串列表。 – Metroids

+0