Page 1 of 1

Country Selection [FIXED]

Posted: Sun Apr 14, 2013 7:58 am
by fibernut
Hello,
I am trying to create a dropdown box on the billingInfo and shippingInfo pages where customers can their a country from a list.
I have tried may ways to do this but I failed every time, what method would be the best to achieve this?

Thanks

Re: Country Selection

Posted: Tue Apr 16, 2013 4:19 pm
by fibernut
I have tried this but it would not work:

Code: Select all


<input type='text' id="other" name="address.country" class="hidden" />

<select id="choice">
         <option value="other">Please Select Your Country</option>
   <option value="US">United States</option>
    <option value="UK">United Kingdom</option>
</select>

<script>
$('#choice').change(function(){
    var selected_item = $(this).val()

    if(selected_item == "other"){
        $('#other').val("").removeClass('hidden');
    }else{
        $('#other').val(selected_item).addClass('hidden');
    }
});
</script>


Re: Country Selection

Posted: Wed Apr 17, 2013 2:38 am
by denis
Hi,

In your controller you have to add :

Code: Select all

@InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(Country.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                Country country = countryService.findCountryByAbbreviation(text);
                setValue(country);
            }
        });
    }

@ModelAttribute("countries")
    protected List<Country> populateCountries() {
        return countryService.findCountries();
    }


And in your template :

Code: Select all

<select th:field="*{address.country}">
<option th:each="country : ${countries}" th:value="${country.abbreviation}" th:text="${country.name}"></option>
</select>

Re: Country Selection

Posted: Wed Apr 17, 2013 11:18 am
by fibernut
Thanks denis. Which controller do I have to add the above code to?

Re: Country Selection

Posted: Wed Apr 17, 2013 11:24 am
by phillipuniverse
In this case, the CheckoutController because that's what drives those checkout pages.

Re: Country Selection

Posted: Wed Apr 17, 2013 11:49 am
by fibernut
Where in the Heat Clinic CheckoutController should I place the code?

Re: Country Selection

Posted: Thu Apr 18, 2013 11:07 am
by fibernut
I got it working correctly. Thanks Everyone for helping!!