0

I have some controller that is connected with some lists, now that list can be empty, I'm trying to provide a user message on page if list is empty. To be more clear, user can have wallet, inside that wallet user can create a transactions, because all of that I have a page Transactions now, if user still didnt create any transaction but visit that page, I want to show him message like You didnt create any transaction.

I found this example, and tried to apply on my problem, but so far it didnt work: How to check null and empty condition using Thymeleaf in one single operation?

This is my controller:

@GetMapping("/userTransactions/{user_id}")
public String getUserTransactions(@PathVariable("user_id") long user_id, TransactionGroup transactionGroup, Model model) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    UserDetailsImpl user = (UserDetailsImpl) authentication.getPrincipal();
    long userId = user.getId();
    model.addAttribute("userId", userId);

    List<Transaction> transactions = transactionRepository.getTransactionsByUserId(user_id);
    List<TransactionGroup> transactionByDate = new ArrayList<>();
    List<Transaction> transOnSingleDate = new ArrayList<>();
    boolean currDates = transactions.stream().findFirst().isPresent();

    if (currDates) {
        LocalDate currDate = transactions.get(0).getDate();

        TransactionGroup transGroup = new TransactionGroup();

        for (Transaction t : transactions) {
            if (!currDate.isEqual(t.getDate())) {
                transGroup.setDate(currDate);
                transGroup.setTransactions(transOnSingleDate);
                transactionByDate.add(transGroup);
                transGroup = new TransactionGroup();
                transOnSingleDate = new ArrayList<>();
            }

            transOnSingleDate.add(t);
            currDate = t.getDate();
        }
        transGroup.setDate(currDate);
        transGroup.setTransactions(transOnSingleDate);

        transactionByDate.add(transGroup);
    } else {
        System.out.println("Empty");
        model.addAttribute("transactionGroup", "NoData");
    }
    model.addAttribute("transactionGroup", transactionByDate);
    return "transactions";
}

And that seems to work fine, I mean, if I didn't create a transaction, message System.out.println("Empty"); will be printed, but message on page is not displayed neither.

This is thymeleaf:

    <div class="date" th:each="singleGroup  : ${transactionGroup}">
    <h1 th:text="${singleGroup .date}"></h1>
    <div class="transaction" th:each="singleTrans  : ${singleGroup.transactions}">
        <h2>Amount: <span th:text="${singleTrans.amount}"></span></h2><br>
        <h2>Note: <span th:text="${singleTrans.note}"></span></h2><br>
        <h2>Wallet name: <span th:text="${singleTrans .walletName}"></span></h2><br>
        <h2>Expense Category: <span th:text="${singleTrans .expenseCategories}"></span></h2><br>
        <h2>IncomeCategory: <span th:text="${singleTrans .incomeCategories}"></span></h2>
        <a class="card__link" th:href="@{/api/transaction/delete/{id}(id=${singleTrans.id})}"
           th:data-confirm-delete="|Are you sure you want to delete ${singleTrans.walletName} wallet?|"
           onclick="if (!confirm(this.getAttribute('data-confirm-delete'))) return false">Delete</a>
        <hr>
    </div>
    <th:block th:if="${transactionGroup=='NoData'}"> No Data Found </th:block>
</div>

And here is the part: <th:block th:if="${transactionGroup=='NoData'}"> No Data Found </th:block> But its not displayed if list is empty

What I'm missing?

Frosty Nah
  • 23
  • 5
  • The model attribute `transactionGroup` looks like it is never equal to `NoData` because even if you set it in your `else` clause, you immediately replace that with `model.addAttribute("transactionGroup", transactionByDate)`. – andrewJames Feb 18 '23 at 20:12
  • Does this answer your question? [How to check if list is empty using thymeleaf?](https://stackoverflow.com/questions/33106391/how-to-check-if-list-is-empty-using-thymeleaf) – andrewJames Feb 18 '23 at 20:12
  • It is same even if I put for example `model.addAttribute("checkEmpty", "NoData");` and then ` No Data Found `. Also, sadly, that doesnt answer my quesiton – Frosty Nah Feb 18 '23 at 20:21
  • It appears to do exactly what you need - but if that is not the case, you can clarify in the question, so we [don't keep suggesting it](https://stackoverflow.com/a/75496211/12567365) to you. – andrewJames Feb 18 '23 at 20:49
  • You're comments are crazy bro :D What to clarify? Nothing is happening, no errors, nothing, just empty page – Frosty Nah Feb 18 '23 at 21:17
  • In these situations it often helps if you show us exactly _how_ you are attempting to use the suggested technique in the linked question, and/or in other answers in this question. Show us your revised code. The reason for this is: It can sometimes happen that the proposed approach is not being used in the correct/expected way. I am **not** saying "you are doing it wrong". I am asking simply for you to show us what you _are_ doing, so we can re-evaluate. – andrewJames Feb 18 '23 at 21:22
  • 1
    Hi, I want to apologize if I was rude to you, actually when I solved this problem I stopped for a second and I was like, yes, that man was right, I should have updated the code with a newer example, because, at the end problem wasn't in the hard reloading page, instead of I put code example inside wrong `
    `, one more time, sorry
    – Frosty Nah Feb 18 '23 at 21:54
  • No problem at all, no apology necessary. And you have a solution, which is why we're here. – andrewJames Feb 18 '23 at 22:14

1 Answers1

1

You can check if your list is empty like this

<div th:if="${#lists.isEmpty(transactionGroup)}"> No Data Found </div>

<div th:if="${#lists.size(transactionGroup) ==0}"> No Data Found </div>
  

In addition to that, your th:block is placed inside the list outer loop and it should be outside. That's why you're not seeing anything when the list is empty.

dsp_user
  • 2,061
  • 2
  • 16
  • 23