2016-08-03 48 views
0

Ich habe die folgenden Thymoleaf-Code. transactionList und accountList ist meine ModellattributeEinstellung Attribut in einem Thymelblatt-Schleife, basierend auf einer zweiten Liste

<tr th:each="transaction : ${transactionList}" th:class="'account_' + ${transaction.accountId}"> 
    <td th:text="${transaction.transactionId}">0</td> 
    <td> 
     <select th:id="'account-' + ${transaction.transactionId}"> 
      <option th:each="account : ${accountList}" 
        th:value="${account.accountId}" 
        th:text="${account.name}" 
        th:selected="${transaction.accountId} == ${account.accountId}"/> 
     </select> 
    </td> 
</tr> 

Mein Problem ist, die Klassennamen für den tr-Tag einstellen. Die derzeit auf

th:class="'account_' + ${transaction.accountId}" 

Aber ich mag es so ändern, dass es die Zeichenfolge ‚account_‘ durch den Index des accountList wo transaction.accountId == account.accountId gefolgt ist.

Also grundsätzlich möchte ich herausfinden, welches Element von accountList accountId gleich transaction.accountId ist.

Also würde ich irgendwie accountList jedes Mal vor der th: Klasse im tr durchlaufen müssen.

Ich könnte sicherlich das zu den in transactionList enthaltenen Objekten hinzufügen und damit erledigt werden, aber es bricht Abstraktion, und ich würde es vorziehen, dies am Frontend zu tun.

Irgendwelche Vorschläge?

Antwort

0

ich nicht eine glatte Art und Weise, dies zu tun finden konnte, so habe ich ein neues Modell Attribut, das accountIds den Index abgebildet:

Map<String, Integer> accountIndexMap = new HashMap<>(); 
    IntStream.range(0, accountList.size()) 
      .forEach(i -> accountIndexMap.put(Long.toString(accountList.get(i).getAccountId()), i)); 

model.addAttribute("accountIndexMap", accountIndexMap); 

dann in meiner thymeleaf Seite änderte ich den th: Klasse zu sein:

<tr th:each="transaction : ${transactionList}" th:class="'account_' + ${accountIndexMap.get('__${transaction.accountId}__')}">