grailsInvas0r
grailsInvas0r

Reputation: 655

play 2.0 templates html select with if statement in a map

I want to create a <select> without using the helpers (because the select helper is generating a lot of html)

So, I get a list of cities from the Controller like:

public static List<City> getAllSortedByNameAsc() {
    List<City> cities = new ArrayList<City>();
    cities.addAll(City.find.orderBy("name").findList());
    return cities;
}

In my template, I create the options with this code:

@cities.map { city =>
    <option value="@city.id">@city.name</option>
}

which works, but I also want to have the chosen city as selected value. I tried several things like this:

@cities.map { city =>
    <option value="@city.id" selected="@if(offerForm("city.id").value == city.id){selected}">@city.name</option>
}

But that doesn't work. Can anyone give me a hint?

Upvotes: 1

Views: 1300

Answers (2)

jittakal
jittakal

Reputation: 919

Configuration File: application.conf

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
ebean.default="models.*"

Controller Class => Application.java

package controllers;

import play.*;
import play.mvc.*;
import models.City;
import play.data.Form;
import views.html.*;

public class Application extends Controller {

  final static Form<City> cityForm = form(City.class);

  public static Result index() {
    City pune=new City();
    pune.name="pune";
    pune.save();

    City mumbai=new City();
    mumbai.name="mumbai";
    mumbai.save();

    City city=City.get(2L);

    return ok(index.render(cityForm.fill(city),City.all()));
  }

}

Model Class => City.java:

package models;

import javax.persistence.Entity;
import javax.persistence.Id;
import play.db.ebean.Model;
import java.util.List;
import com.avaje.ebean.validation.NotNull;

@Entity
public class City extends Model{

    @Id
    public Long id;

    @NotNull
    public String name;

    public static Finder<Long, City> find = new Finder(Long.class, City.class);

    public static City get(Long id){
        return find.byId(id);
    }

    public static List<City> all() {
        return find.all();
    }

}

Template File => index.scala.html

@(cityForm: Form[City],cities: List[City])

<!DOCTYPE html>
<html>
    <head><title></title></head>

    <body>
        <div>
            <select>
                @for(city <- cities){
                    <option value="@city.id" @{if(city.id.toString().equals(cityForm("id").value)) "selected='selected'"}/>@city.name</option>
                }
            </select>
        </div>
    </body>
</html>

Upvotes: 1

Brad Mace
Brad Mace

Reputation: 27886

I'm not positive about how the type mapping is working, but I would try

@cities.map { city =>
    <option value="@city.id" selected="@if(offerForm("city.id").value.equals(city.id)){selected}">@city.name</option>
}

rather than using the == operator.

You should also consider whether the helper has a good reason for generating all that HTML. It's generally a pretty smart framework.

Upvotes: 0

Related Questions