0

I am using scala template and Play 2.0 framework for my project. Let's say I have a user form with fields like name (textfield), age (dropdown). While creating the user I filled name as dave and selected age as 25.

Now on my edit screen, I want my values to be prefilled, i know how to do it with textfield (i.e. set value as userForm('name')) but what about the dropdown? how to do it.

Dave Ranjan
  • 2,966
  • 24
  • 55
  • I think this has already been answered: http://stackoverflow.com/questions/12798174/populate-a-html-drop-down-list-in-play-framework – Shawn Downs Apr 01 '15 at 11:37
  • No @shawn, I just went through the link, it is telling how to create a dropdown using scala template helper, but not to set the prefilled values. – Dave Ranjan Apr 01 '15 at 13:31
  • 1
    What kind of dropdown are you talking about ? HTML's `select` tag or things like Bootstrap's dropdown? for `select` tag, link suggested is valid one... describe what do you mean by _prefilled_? – biesior Apr 01 '15 at 20:24
  • Well, I am using `@select` (which is a scala helper ) and prefilled simply means, if a user had entered his age as 25 (via dropdown) then on the edit screen his age should be shown as 25 when the page loads. Thankyou in advance. – Dave Ranjan Apr 03 '15 at 06:04

2 Answers2

0

Thanks Shawn Downs and biesior.

Well, we can use @select scala helper class to show the pre-filled result. like.

 @select(userForm("age"),models.Age.values().toList.map(v => (v.getDisplayName(), v.getDisplayName())),'id->"age")

To show other options I have used an enum of possible values of age.

Dave Ranjan
  • 2,966
  • 24
  • 55
0

In your model there will be 2 fields

Model code

Class User{
   public String name;
   public int age;
}

Controller code

.
.
.
Form<User> userForm = Form.form(User.class);
User user = new User();
user.name = "Albert";
user.age = 19;

userForm.fill(user);
.
.
.

Util code

package utils;

import java.util.HashMap;
import java.util.LinkedHashMap;

public class DropdownUtils {

    public static HashMap<String, String> getAgeList(int ageLimit){
        LinkedHashMap<String, String> ageList = new LinkedHashMap<>();

        for(Integer i=0; i<=ageLimit; i++){
            ageList.put(i.toString(), i.toString());
        }

        return ageList;
    }
}

View code

.
.
.
<form>
@helper.inputText(userForm("name"))
@helper.select(userForm("age"),
                helper.options(utils.DropdownUtils.getAgeList(25)))
</form>
.
.
.
Himanshu Goel
  • 574
  • 1
  • 8
  • 26