Thymleaf中th:each標籤遍歷list如何獲取index

簡單介紹:傳遞給後臺一個String類型的list,須要獲取到list的每個元素,而後進行篩選,獲得正確的文本值,看代碼就明白了html

代碼:java

//後臺java代碼
//failList是一個String類型的list,存放的是狀態碼00 01 02 03 04 05 06中的某幾種
map.addAttribute("failMsgList",failMsgList);
return VIEW_PATH + "/failDetail";//跳轉到失敗詳情頁面 

 

//html代碼
<tr th:each="plan : ${planList}" th:id="${plan.planId}"
th:attr="data-plan-status=${plan.planStatus}">
<td th:text="${plan.planName}"></td>
<td th:text="${plan.planCode}"></td>
<div th:switch="${failMsgList.get(__${planStat.index}__)}">
<td th:case="00">後臺系統故障</td>
<td th:case="02">此方案待審覈,不支持下架</td>
<td th:case="01">此方案未上架,不支持下架</td>
<td th:case="04">此方案未上架,不支持下架</td>
<td th:case="03">此方案未上架,不支持下架</td>
<td th:case="06">此方案已經下架</td>
<td th:case="*"></td>
</div>
說明:failList裏的狀態碼的獲取,經過tr循環到第幾行的索引來取

 乾貨:spa

<tr th:each="user,userStat:${users}"> 

 userStat是狀態變量,若是沒有顯示設置狀態變量,thymeleaf會默 認給個「變量名+Stat"的狀態變量。code

對arrayList對象users遍歷,使用user做爲接受參數接收,使用userStat做爲users下標值,經過userStat.index獲得當前所處下標值;htm

狀態變量有如下幾個屬性:對象

  • index:當前迭代對象的index(從0開始計算)
  • count: 當前迭代對象的index(從1開始計算)
  • size:被迭代對象的大小
  • current:當前迭代變量
  • even/odd:布爾值,當前循環是不是偶數/奇數(從0開始計算)
  • first:布爾值,當前循環是不是第一個
  • last:布爾值,當前循環是不是最後一個
    固然,user和userStat能夠本身定義名字,若是沒定義狀態變量,那麼thymleaf會自動給一個「變量名+Stat」。

相關文章
相關標籤/搜索