Martin Golpashin
Martin Golpashin

Reputation: 1062

Playframework get List of Objects as Options in select

I am using Playframework 2.1.1 and Java. I am trying to fill a selectbox with data I retrieve from a database using the Play formhelpers

Here is some code if the view:

@helper.form(action = routes.Admin.submitUnit) {
    @helper.select(
        field = unitForm("metaunit"),
        options = options(Metaunit.find)
    )

    ...
}

And the Method to retrieve Metaunits from db:

public static List<Metaunit> find(){
    Query query = JPA.em().createQuery("SELECT e FROM Metaunit e");
    return (List<Metaunit>)query.getResultList();
}

When I try to compile it, I get the following Error-Message:

Overloaded method value [apply] cannot be applied to (java.util.List[models.Metaunit])

Any help is appreciated! Thanks

Upvotes: 0

Views: 1355

Answers (1)

biesior
biesior

Reputation: 55798

Take a look into computer-database-jpa sample ie. options() method in Company model in general it returns Map<String, String>.

As you can see in the editForm view, usage is pretty similar to your.

Note: probably you Metaunit is connected with some M-M relation, in that case most probably you will need to use unitForm("metaunit.id") as a field's value

Upvotes: 4

Related Questions