me.at.coding
me.at.coding

Reputation: 17654

Setting selected entry using helper.options?

Im am using Play 2.0.4 and

helper.options(myitems)

in my template (inside of a helper.select)

In this case, how can I define the default selected entry, which shall be one entry out of myitems? Thanks for any hint!

A little bit more about my case:

Imagine a news archive, showing all news titles. This news archive uses pagination, pagination uses GET to pass the next/previous page number.

The play framework however will only correctly select the currently selected "select" item (here: news category) when a POST request was used - while pagination uses GET!

Intended behaviour: While a filter is applied / a specific news category is selected, this shall always be visible to the user by preselecting the currently selected news category in the "select" form.

A "screenshot" for illustration:

enter image description here

So, anyone having a good idea on how to cope with this problem? Any way to tell Play manually which entry from the "select" form it shall select? '_default always adds a new entry instead of selecting one out of the given options ): Would be great, if one wouldn't have to build the complete "select" form manually.

Upvotes: 3

Views: 2396

Answers (4)

Oskar Andersson
Oskar Andersson

Reputation: 61

Still wondering if there is a better option / way to do it.

Well, if your not hell-bent on using play to solve this particular problem, you could always solve it using JavaScript and jQuery:

$(function () {    
    $('#yourSelect_id').val(5);
}

Where your select options each has values and the one option you whish to pre select has value 5.

Upvotes: 0

Andreas E
Andreas E

Reputation: 1

Actually, there is a nicer solution to it. If you call the template having the form partially bound you will achieve your goal. Here's the code for your controller:

Ok(views.html.myForm(myForm.bind(
             Map("fieldName1" -> "value1",
                 "fieldName2" -> "value2"))))

Make sure you map fieldnames to the values of the options you want pre-selected.

Upvotes: 0

me.at.coding
me.at.coding

Reputation: 17654

Ended up with the pragmatic approach:

<select id="myfield" name="myfield" >

    <option class="blank" value="">-- All items --</option>

    @for((key, value) <- MyModel.options) {

        @if(key == GETValuePassedToTemplate) {
            <option value="@key" selected>@value</option>   
        } else {
            <option value="@key">@value</option>                            
        }
    }

</select>   

Still wondering if there is a better option / way to do it.

Upvotes: 0

lambdas
lambdas

Reputation: 4090

Try passing '_default option to select helper:

@import views.html.helper._

@select(form("email"), options(List("first", "third")), '_default -> "second")

It seems, unfortunately, the only way to figure it out is to look up the source.

Update:

Specifying _default property doesn't set selected attribute on option tag. It looks like the only way to preselect entry is to pass prefilled form to the template. For example, suppose you have following form:

case class RegInfo(email: String, color: String)

private val registrationForm = Form(
  mapping(
    "email"    → email,
    "color"    → nonEmptyText(minLength = 5, maxLength = 32)
  )(RegInfo.apply)(RegInfo.unapply)
)

Then in the action prefill form before passing to the view:

def create = Action {
  Ok(html.create(registrationForm.fill(RegInfo("[email protected]", "blue"))))
}

Then in template use helper:

@select(form("color"), options(List("red", "green", "blue")))

And value will be preselected.

Upvotes: 3

Related Questions