简体   繁体   中英

Play Framework 1.2.4: Selected Option for #{select} template

I am passing a Model and a List<String> from controller to .html file using the render method invocation. The Model passed contains some values that has to be populated in different components present in the UI and the List<String> is used for binding with the combo-box.

For binding the List<String> with combo-box in the .html using the below code and this is working fine:

#{select 'employee.role', items:userRoles, valueProperty:'userRoles', labelProperty:'userRoles'/}

What I want to achieve:

Suppose the List<String> contains the roles as Trainer , Educator , Trainee , Staff and the Model passed has a role property with value Staff . When the page loads, then Staff should be in selected state in the combo-box display.

Problem

I am aware of how to iterate through a List in Play framework template but I am not aware of how to keep a value selected in the drop-down based on one of the property values present in the Model

Kindly help me on this.

From the documentation :

This tag can generate options using items attribute.

  • items (optional) - list of objects, used to create options
  • value (optional) - selected element in items (note that multiple selections are not supported)
  • labelProperty (optional) - for each item, attribute used as option's label
  • valueProperty (optional) - for each item, attribute used as option's value. id is used by default

So, the code should be:

#{select 'employee.role', items: userRoles, value: model.role /}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM