質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Q&A

解決済

1回答

2616閲覧

thymeleaf 三項演算子で値によってclassを付与したい

mimi_129

総合スコア63

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

0グッド

0クリップ

投稿2021/06/26 06:41

編集2021/06/26 06:55

前提・実現したいこと

タイトルの通りです。
classが付与されません。

エンティティに値が入っている事は確認済みです。
どこか記述に間違いがあるのでしょうか?

分かる方おられましたら、
ご回答よろしくお願いします。

イメージ説明

該当のソースコード

@RequestMapping(value = "/clientMaster/list") public String list(Model model, @ModelAttribute("clientMasterListForm") ClientMasterListForm clientMasterListForm, @PageableDefault(page = 0, size = 5) Pageable pageable) { session.setAttribute("clientMasterListForm", clientMasterListForm); Page<ClientMaster> list = clientMasterService.findUsers(clientMasterListForm, pageable); model.addAttribute("list", list.getContent()); model.addAttribute("clientMasterListForm",clientMasterListForm); model.addAttribute("page",PagenationHelper.createPagenation(list)); return "/clientMaster/list"; }
<!doctype html> <html xmlns:th="http://www.thymeleaf.org"> <head th:replace="~{layout/component :: head('顧客')}"></head> <body> <th:block th:replace="~{layout/component :: header}"></th:block> <div class="container"> <th:block th:replace="~{layout/component :: container_top('顧客')}"></th:block> <div class="row justify-content-start"> <div class="w-100 m-2"> <form method="post" th:action="@{/clientMaster/list}" th:object="${clientMasterListForm}"> <table class="table-bordered w-100"> <tr th:classappend="${clientMasterListForm.yoshinlevel == 5} ? 'table-danger' : ''"> <td class="p-2 text-center"> <label class="m-0">顧客名</label> </td> <td class="p-2"> <input class="w-100" type="text" name="name" th:value="${clientMasterListForm.name}"> </td> <td class="p-2 text-center"> <label class="m-0">都道府県名</label> </td> <td class="p-2"> <input class="w-100" type="text" name="prefecture" th:value="${clientMasterListForm.prefecture}"> </td> <td class="p-2 text-center "> <input type="submit" value="検索" class="btn btn-outline-secondary"> </td> </tr> </table> </form> </div> <a href="/clientMaster/edit" class="btn btn-outline-secondary mb-3">新規登録</a> <div class="w-100 m-2"> <!-- ページング --> <form method="post" th:action="@{/clientMaster/pagenate}" th:object="${page}"> <nav aria-label="Page navigation example"> <ul class="pagination"> <li th:if="${!page.first}" class="page-item"> <a th:href="@{/clientMaster/pagenate(page = ${page.number} - 1)}" class="page-link" aria-label="Previous"> <span aria-hidden="true">&laquo;</span> <span class="sr-only">Previous</span> </a> </li> <li th:each="i : ${#numbers.sequence(0, page.totalPages - 1)}" class="page-item"> <a th:if="${i} == ${page.number}" th:text="${i + 1}" class="page-link"><span th:text="${i+1}">1</span></a> <a th:if="${i} != ${page.number}" th:href="@{/clientMaster/pagenate(page = ${i})}" class="page-link"><span th:text="${i+1}">1</span></a> </li> <li th:if="${!page.last}" class="page-item"> <a th:href="@{/clientMaster/pagenate(page = (${page.number} + 1))}" class="page-link" aria-label="Next"> <span aria-hidden="true">&raquo;</span> <span class="sr-only">Next</span> </a> </li> </ul> </nav> </form> <!-- ページング --> </div> <table class="table table-striped table-sm"> <tr> <th>ID</th> <th>顧客名</th> <th>都道府県名</th> </tr> <tr th:each="entity:${list}"> <td ><a th:href="@{'/clientMaster/detail?id=' + ${entity.id}}" th:text="${entity.id}">id</a></td> <td th:text="${entity.name}">name</td> <td th:text="${entity.prefecture}">name</td> </tr> </table> <div class="w-100 m-2"> <!-- ページング --> <form method="post" th:action="@{/clientMaster/pagenate}" th:object="${page}"> <nav aria-label="Page navigation example"> <ul class="pagination"> <li th:if="${!page.first}" class="page-item"> <a th:href="@{/clientMaster/pagenate(page = ${page.number} - 1)}" class="page-link" aria-label="Previous"> <span aria-hidden="true">&laquo;</span> <span class="sr-only">Previous</span> </a> </li> <li th:each="i : ${#numbers.sequence(0, page.totalPages - 1)}" class="page-item"> <a th:if="${i} == ${page.number}" th:text="${i + 1}" class="page-link"><span th:text="${i+1}">1</span></a> <a th:if="${i} != ${page.number}" th:href="@{/clientMaster/pagenate(page = ${i})}" class="page-link"><span th:text="${i+1}">1</span></a> </li> <li th:if="${!page.last}" class="page-item"> <a th:href="@{/clientMaster/pagenate(page = (${page.number} + 1))}" class="page-link" aria-label="Next"> <span aria-hidden="true">&raquo;</span> <span class="sr-only">Next</span> </a> </li> </ul> </nav> </form> <!-- ページング --> </div> <a href="/" class="btn btn-outline-secondary">戻る</a> </div> </div> <footer th:replace="~{layout/component :: footer}"></footer> </body> </html>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2021/06/26 06:44

このコードだけで分かることってほとんどないと思います。 コントローラとデータとってくるところ、実際のデータも含めて提示してください。
m.ts10806

2021/06/26 06:52

画像で提示されてもコピペできませんので、そのあたりはご配慮ください
mimi_129

2021/06/26 06:54 編集

失礼いたしました。 list部の記述を追加しました。
m.ts10806

2021/06/26 07:40

三項演算子は「yoshinlevel 」のところですかね。 この値がどう設定されているのか、どうやって「エンティティに値が入っている事」を確認したのかも記載してください。 要は、他者がコピペで状況再現できないと応えようがないわけです。 三項演算子のところでエラーになっていないのなら、結局は値が正しいかどうかになってきますが、そのあたりを確認できる情報がありません。 clientMasterListFormの定義も必要に思います。 そしてそのFormにどこからどのように値が設定されるか。
guest

回答1

0

自己解決

記述する場所が間違っていました。

エンティティを取得して値を判定させたい場所は、

<table class="table table-striped table-sm"> <tr> <th>ID</th> <th>顧客名</th> <th>都道府県名</th> </tr> <tr th:each="entity:${list}"> <td ><a th:href="@{'/clientMaster/detail?id=' + ${entity.id}}" th:text="${entity.id}">id</a></td> <td th:text="${entity.name}">name</td> <td th:text="${entity.prefecture}">name</td> </tr> </table>

内の、

<tr th:each="entity:${list}"> 部でしたのでこちらで三項演算子を記述したらclassが付与されました。 質問の記述箇所は一覧検索に使うformなので、画面に入った時には値が空の状態で来るのでth:classappendが反応しませんでした。

投稿2021/06/26 07:57

mimi_129

総合スコア63

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問