2016-08-03 3 views
0

私は以下のタイムリーフコードを持っています。 transactionListとaccountListは私の問題は、TRタグのクラス名を設定している2番目のリストに基づく、タイムリーフループの属性を設定する

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

私のモデルは属性です。 その現在

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

に設定しかし、私はそれは文字列「account_」accountListのインデックスtransaction.accountId == account.accountIdが続いているように、それを変更したいと思います。

基本的に私はaccountIdがtransaction.accountIdと等しいaccountListの要素を探したいと思います。

だから私はどうにかしてthアカウントの前にループする必要があります:trのクラス。

私は確かにこれをtransactionListに含まれるオブジェクトに追加して終了することができますが、抽象化が解除されます。フロントエンドでこれを行うことをお勧めします。

提案がありますか?

答えて

0

私はこれを行うための巧みな方法を見つけることができませんでしたので、私はインデックスにaccountIdsをマッピングされた新しいモデルの属性を作成しました:

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

はその後、私のthymeleafページに私が目を変更:クラスがあることを。

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