0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

thymeleaf テスト

Last updated at Posted at 2022-02-24
<div>
    <div th:each="CookingHisData.:${cookHisList}" th:text="${cookHisList}"></div>
</div>

→全表示

[CookingHisData(cookingDate=Mon Feb 14 00:00:00 JST 2022, menuId=null, menuName=ごはん , foodType1Id=null, foodType1Name=null, foodType2Id=null, foodType2Name=null, usedGram=null, username=null, remark=null), CookingHisData(cookingDate=Mon Feb 14 00:00:00 JST 2022, menuId=null, menuName=野菜炒め , foodType1Id=null, foodType1Name=null, foodType2Id=null, foodType2Name=null, usedGram=null, username=null, remark=null), CookingHisData(cookingDate=Tue Feb 15 00:00:00 JST 2022, menuId=null, menuName=野菜炒め , foodType1Id=null, foodType1Name=null, foodType2Id=null, foodType2Name=null, usedGram=null, username=null, remark=null), CookingHisData(cookingDate=Wed Feb 16 00:00:00 JST 2022, menuId=null, menuName=オムライス , foodType1Id=null, foodType1Name=null, foodType2Id=null, foodType2Name=null, usedGram=null, username=null, remark=null)]

div th:each th:text1行

<H4>test2 1行</H4>
<div>
    <div th:each="CookingHisData:${cookHisList}" th:object="${CookingHisData}" th:text="*{menuName}"></div>
</div>

ごはん
野菜炒め
野菜炒め
オムライス
<H4>test3 オブジェクト外出し【エラー】</H4>
<div th:object="${CookingHisData}" >
    <div th:each="CookingHisData:${cookHisList}" th:text="*{menuName}"></div>
</div>
→ SpelEvaluationException: EL1007E:
→ Property or field 'menuName' cannot be found on null

---------------------------------------------------------------------
<H4>test4 オブジェクト2箇所 テキスト2行</H4>
<div th:each="CookingHisData:${cookHisList}">
    <div th:object="${CookingHisData}" th:text="*{cookingDate}"></div>
    <div th:object="${CookingHisData}" th:text="*{menuName}"></div>
</div>

Mon Feb 14 00:00:00 JST 2022
ごはん
Mon Feb 14 00:00:00 JST 2022
野菜炒め
Tue Feb 15 00:00:00 JST 2022
野菜炒め
Wed Feb 16 00:00:00 JST 2022
オムライス

オブジェクト1箇所 div th:text2行

<H4>test5 オブジェクト1箇所 div th:each th:text1行
<div th:each="CookingHisData:${cookHisList}" th:object="${CookingHisData}" >
    <div th:text="*{cookingDate}"></div>
    <div th:text="*{menuName}"></div>
</div>

Mon Feb 14 00:00:00 JST 2022
ごはん
Mon Feb 14 00:00:00 JST 2022
野菜炒め
Tue Feb 15 00:00:00 JST 2022
野菜炒め
Wed Feb 16 00:00:00 JST 2022
オムライス
<H4>test6 条件 false</H4>
<div th:each="CookingHisData:${cookHisList}" th:object="${CookingHisData}" >
    <!-- <div th:if="*{cookingDate}==2021-02-14" th:text="*{cookingDate}"></div> -->
    <div th:if="*{menuName} eq '*ごはん*'" th:text="*{menuName}"></div>
</div>
---------------------------------------------------------------------
<H4>test7 条件 true</H4>
<div th:each="CookingHisData:${cookHisList}" th:object="${CookingHisData}" >
    <!-- char(50) -->
    <div th:if="*{menuName} eq ${'ごはん                                               '}" th:text="*{menuName}"></div>
</div>

ごはん

日付条件

<H4>test8 条件 true</H4>
<div th:each="CookingHisData:${cookHisList}" th:object="${CookingHisData}" >
    <div th:if="*{#dates.format(cookingDate,'yyyyMMdd')} eq '20220214'" th:text="*{menuName}"></div>
</div>

ごはん
野菜炒め

テーブル object位置

<table id='data-table'>
    <tr><th>メニュー名</th><th>メニュー名</th></tr>
    <tr th:object ="${CookingHisData}" th:each="CookingHisData:${cookHisList}">
        <td  th:text="*{menuName}"></td>
        <td  th:text="*{menuName}"></td>
    </tr>
</table>
-------------------------------------------------------------------------------
メニュー名	メニュー名
ごはん	ごはん
野菜炒め	野菜炒め
野菜炒め	野菜炒め
オムライス	オムライス

th:each + if / object 位置

<table id='data-table' class="table table-striped margin20-20-0-10">
    <tr>
        <th>メニュー</th><td></td><th>ユーザー名</th><td></td></tr>
    <tr><th colspan="2">材料</th><th colspan="2">グラム</th>
    </tr>
    <tr th:object ="${CookingHisData}" th:each="CookingHisData:${DetailList}">
        <td  th:if="*{menuNum} eq 1" th:text="*{ingredientsName}" colspan="2"></td>
        <td  th:if="*{menuNum} eq 1" th:text="*{usedGram}" colspan="2"></td>
    </tr>
</table>

------------------------------------------------------------------------
メニュー		ユーザー名	
材料	グラム
米	200

条件に従ってtable表示

条件不一致の場合、行が表示されないため、結果非表示と同じとなる

<!--1-->
<table class="margin20-20-0-10">
    <tr class="delete-row" th:object ="${CookingHisData}" th:each="CookingHisData:${OverViewList}">
        <th th:if="*{menuNum} eq 1">メニュー</th><td th:if="*{menuNum} eq 1" th:text="*{menuName}"></td>
        <th th:if="*{menuNum} eq 1">ユーザー名</th><td th:if="*{menuNum} eq 1" th:text="*{username}"></td></tr>
    </tr>
    <tr  class="delete-row" th:object ="${CookingHisData}" th:each="CookingHisData:${OverViewList}">
        <th th:if="*{menuNum} eq 1" colspan="2">材料</th>
        <th th:if="*{menuNum} eq 1" colspan="2">グラム</th>
    </tr>
    <tr th:object ="${CookingHisData}" th:each="CookingHisData:${DetailList}">
        <td  th:if="*{menuNum} eq 1" th:text="*{ingredientsName}" colspan="2"></td>
        <td  th:if="*{menuNum} eq 1" th:text="*{usedGram}" colspan="2"></td>
    </tr>
</table>

withを使った変数定義と再代入

・定義は可能
・再代入不可

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?