0

I'm able to retrieve data from database but not to add condition which will display an alert when database table is empty.

Here is my code:

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>
        }
    }
}

Controller method:

public static Result history(long id) {
    Form<Users> taskData = form(Users.class);
    return ok(history.render(Users.all(), taskData));
}
2
  • Which version of Play are you using? Are you sure that this view/template is related to the action/controller method you posted? What exactly you want to check? Aug 5, 2016 at 19:22
  • i am using play 2.2.6 , i what to check if database table is empty before fetch data from it. Aug 6, 2016 at 12:33

2 Answers 2

2

tl;dr Move User.all() to the controller and pass it via template parameters, then add the if statement to render the alert if the list is empty.


You’ll have to add an if statement to check if the user list is empty, and display the alert if it is.

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

In order to avoid fetching the users twice, you could use the defining function.

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

But I’d strongly suggest to move the user fetching to the controller, and make the template accept the user list as a parameter. That way you’ll keep the template simple. When it gets rendered, it will just present the data, instead of potentially doing heavy-lifting like fetching records from the database.

The end result could look like this.

Template

@(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>
        }
    }
}

Controller

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

Thank you so much @jokka for your answer is helpful for me.

i simply add the following lines in my template index.scala.html

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.