Sergei
Sergei

Reputation: 21

playframework: dropdownlist selected value

How to pass a value from one dropdown to a second dropdown.

Example:

Template

#{select 'pl'}

            #{list platforms, as: 'platform'}
                #{option}${platform.description} #{/option}
            #{/list}
#{/select}                          


 #{select 'pl'}        
        #{list cdrs, as:'cdr'}
               #{option}${cdr.description} #{/option}
        #{/lisst}          
 #{/select}        

Upvotes: 2

Views: 2216

Answers (3)

Carlisg
Carlisg

Reputation: 89

If you mean using Javascript to "chain" two selects like Country/City, what I did was moving the code for the second select to its own view (e.g. Cities/selectCities.html)

#{select 'city', items: cities, valueProperty: 'id', labelProperty: 'name' /}

and use an include in the view where I'll have both chained selects

<select name="country" id="select-country">
    <option value="ES">Spain</option>
    <option value="US">United States</option>
</select>

<span id="select-city">
    #{include 'Cities/selectCities.html' /}
</span>

now some javascript in that same view to reaload second select in case first select changes

$('#select-country').change(function() {
    var selectAction = #{jsAction @reloadCities(':country') /};
    $('#select-cities').load(selectAction({country: $(this).val()}));
});

and in the controller we have our reaload cities method rendering only the second select again

public static void reloadCities(String country) {
    List<City> cities = City.find("byCountryCode", country).fetch();

    render("@selectCities", cities);
}

and that's about it, for me it's working using play 1.2.5

Upvotes: 2

i.am.michiel
i.am.michiel

Reputation: 10404

Your question is not really clear but if I understand it right, you want to change a select depending on the value of the selected item in the first select. That is impossible as it is a runtime animation.

You will have to take a look at Ajax/Javascript in order to do things like that.

Upvotes: 1

Pere Villega
Pere Villega

Reputation: 16439

If you mean once is rendered in the browser, you must use Javascript.

Otherwise, use the value keyword as explained in the documentation (here)

Upvotes: 0

Related Questions