簡體   English   中英

如何在Java Play框架中檢查數據庫表是否為空

[英]How to check if database table is empty in java play framework

我能夠從數據庫中檢索數據,但不能添加條件,當數據庫表為空時,它將顯示警報。

這是我的代碼:

index.scala.html

@(formList: List[Users],form: Form[Users])

@main("history") {

    @for(i <- Users.all()) {

        @if(i.client.equalsIgnoreCase("potato")) {

            <Table>
                <tbody>
                    <tr>
                        <td><a href="/#"><i >@i.firstname @i.lastname </i></a></td>
                        <td><a href="/#"><i>@i.phonenumber</i></a></td>
                        <td><a href="/#"><i>@i.amount</i></a></td>
                        <td><a href="/#"><i>@i.doneAt</i></a></td>
                        <td><a href="@routes.Application.edit(i.id)"><i><span class="glyphicon glyphicon-pencil "></span></i></a></td>
                        <td><a href="@routes.Application.delete(i.id) "><i><span class="glyphicon glyphicon-trash "></span></i></a></td>
                    </tr>
                </tbody>
            </table>
        }
    }
}

控制器方式:

public static Result history(long id) {
    Form<Users> taskData = form(Users.class);
    return ok(history.render(Users.all(), taskData));
}

tl; dr將User.all()移至控制器並通過模板參數傳遞它,然后添加if語句以在列表為空時呈現警報。


您必須添加一個if語句來檢查用戶列表是否為空,並顯示警報。

@if (Users.all().isEmpty) {
  // Show the alert.
}

為了避免兩次獲取用戶,可以使用defining函數。

@defining(Users.all()) { users =>
  @if (users.isEmpty) {
    // Show the alert.
  }
  @for(user <- users) {
    …
  }
}

但我強烈建議將用戶獲取的內容移至控制器,並使模板接受用戶列表作為參數。 這樣,您將使模板保持簡單。 呈現后,它將僅顯示數據,而不是像從數據庫中獲取記錄那樣進行繁重的工作。

最終結果可能如下所示。

模板

@(users: List[Users], formList: List[Users],form: Form[Users])

@main("history") {

    @if (users.isEmpty) {
        <div class="alert">
            …
        </div>
    }

    @for(i <- users) {

        @if(i.client.equalsIgnoreCase("potato")) {

            <Table>
                <tbody>
                    <tr>
                        <td><a href="/#"><i >@i.firstname @i.lastname </i></a></td>
                        <td><a href="/#"><i>@i.phonenumber</i></a></td>
                        <td><a href="/#"><i>@i.amount</i></a></td>
                        <td><a href="/#"><i>@i.doneAt</i></a></td>
                        <td><a href="@routes.Application.edit(i.id)"><i><span class="glyphicon glyphicon-pencil "></span></i></a></td>
                        <td><a href="@routes.Application.delete(i.id) "><i><span class="glyphicon glyphicon-trash "></span></i></a></td>
                    </tr>
                </tbody>
            </table>
        }
    }
}

控制者

public static Result history(long id) {
    Form<Users> taskData = form(Users.class);
    return ok(history.render(Users.all(), Users.MTN(), taskData));
}

非常感謝@jokka的回答對我有幫助。

我只是在模板index.scala.html中添加以下行

  @if(formList.isEmpty) {
              <div class="alert">
               <p>Sorry no sold airtime found today</p>
                  </div>
                   }
              @for(i <- Users.all()) {
             .........

暫無
暫無

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

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