如何在 java 播放框架中检查数据库 table 是否为空
How to check if database table is empty in java play framework
我可以从数据库中检索数据,但不能添加条件,当数据库 table 为空时会显示警报。
这是我的代码:
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()) {
.........
我可以从数据库中检索数据,但不能添加条件,当数据库 table 为空时会显示警报。
这是我的代码:
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()) {
.........