簡體   English   中英

按日期列出表中的所有數據

[英]List all data from table by date

我很想為此找到一個解決方案,但到目前為止找不到任何相關和有用的東西。

我有一個表Transaction

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "transaction_id")
private Long id;

@Column(name = "user_id", nullable = false)
private Long userId;

@Column(name = "wallet_name", nullable = false)
private String walletName;

@NotNull(message = "Please, insert a amount")
@Min(value = 0, message = "Please, insert a positive amount")
private Double amount;

private String note;

@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = "date")
private LocalDate date;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "wallet_id", nullable = false)
private Wallet wallet;

@Enumerated(EnumType.STRING)
@Column(name = "transaction_type", columnDefinition = "ENUM('EXPENSE', 'INCOME')")
private TransactionType transactionType;

@Nullable
@Enumerated(EnumType.STRING)
@Column(name = "expense_categories", columnDefinition = "ENUM('FOOD_AND_DRINK', 'SHOPPING', 'TRANSPORT', 'HOME'," +
        " 'BILLS_AND_FEES', 'ENTERTAINMENT', 'CAR', 'TRAVEL', 'FAMILY_AND_PERSONAL', 'HEALTHCARE'," +
        " 'EDUCATION', 'GROCERIES', 'GIFTS', 'BEAUTY', 'WORK', 'SPORTS_AND_HOBBIES', 'OTHER')")
private ExpenseCategories expenseCategories;

@Nullable
@Enumerated(EnumType.STRING)
@Column(name = "income_categories", columnDefinition = "ENUM('SALARY', 'BUSINESS', 'GIFTS', 'EXTRA_INCOME', 'LOAN', 'PARENTAL_LEAVE', 'INSURANCE_PAYOUT', 'OTHER')")
private IncomeCategories incomeCategories;

現在,為了在 Thymeleaf 上顯示該數據,我創建了 controller 和 model,我將傳遞給 Thymeleaf。

看起來像這樣:

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

如您所見, TransactionUser相關聯,因此用戶實際上可以創建一個Transaction ,在 controller 上,我只是從每個 User 獲取所有交易,這就是我在Thymeleaf上顯示它的方式:

<div th:each="transactions : ${transactions}">
    <h2>Amount: <span th:text="${transactions.amount}"></span></h2>
    <br>
    <h2>Note: <span th:text="${transactions.note}"></span></h2>
    <br>
    <h2>Date: <span th:text="${transactions.date}"></span></h2>
    <br>
    <h2>Wallet name: <span th:text="${transactions.walletName}"></span></h2>
    <br>
    <h2>Expense Category: <span th:text="${transactions.expenseCategories}"></span></h2>
    <br>
    <h2>IncomeCategory: <span th:text="${transactions.incomeCategories}"></span></h2>
</div>

不要介意我格式化它的方式,重點是到目前為止工作正常,我在頁面上顯示了所有數據,但我想實現一件事:

如您所見,在創建交易時用戶還選擇了一個日期,而我想要什么?

假設用戶今天有兩筆交易,我的意思是18/01/2023

我想要一個標題,例如<div並傳遞今天的日期和從今天開始的每筆交易,然后向下傳遞,例如15/01/2023 ,因為用戶在該日期也有交易。

這是一個示例,如您所見,我有今天部分,因為用戶在今天進行了交易,也是在昨天進行了交易,然后又在 1 月 14 日進行了交易,所以我想像這樣將每個交易分開。 例子

對不起 SS,我嘗試添加代碼片段但無法在此更新的 SO 上找到選項。

我不知道從哪里開始,我發現了一些選項,比如從升序和降序日期排序,但我想避免這樣的事情,並盡我最大的努力實現這一目標。 所以這里的任何資源、教程或任何幫助都可以。 謝謝。

如果你想按日期分解,你應該事先在 Java 中進行。 您應該傳遞一個List<TransactionGroup>而不是傳遞List<Transaction> ,其中 TransactionGroup 包含一天的所有交易。 它可能看起來像:

class TransactionGroup {
  private LocalDate date;
  private List<Transaction> transactions;
  /* Getters and setters */
}

那么你的 html 就變得簡單了。

<div th:each="group : ${transactionGroup}">
    <h1 th:text="${group.date}" />

    <div th:each="transaction : ${group.transactions}">
        <h2>Amount: <span th:text="${transactions.amount}"></span></h2><br>
        <h2>Note: <span th:text="${transactions.note}"></span></h2><br>
        <h2>Wallet name: <span th:text="${transactions.walletName}"></span></h2><br>
        <h2>Expense Category: <span th:text="${transactions.expenseCategories}"></span></h2><br>
        <h2>IncomeCategory: <span th:text="${transactions.incomeCategories}"></span></h2>
    <div>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM