Question

Little problem with DropDownChoice. I build a DropDownChoice with a list of Object. Take for example:

public MyClass 
  private String code;
  private String description;
[...]

than take another Object

public FormModelObject
   private String code;

I can easly build the DropDownChoice selecting what show (description property) and what obtain (code property) using ChoiceRender.

Unfortunally the DropDownChoice return the entire Object (MyClass) and I cannot set the property Code inside FormModelObject (it simply launch a toString() method on MyClass).

How can I obtain it without using Ajax?

Thanks

Was it helpful?

Solution

Well, you could override method onModelChanged() of DropDownChoice and update residenceZipCode with the new value. Something like :

DropDownChoice<City> drop = new DropDownChoice<City>("residenceZipCode",new Model(), cityList,new ChoiceRenderer<City>("name", "zipcode")){
     onModelChanged(){
       String newZipCode =  (String)PropertyResolver.getValue("zipCode",getModelObject());
       PropertyResolver.setValue("residenceZipCode", res, newZipCode, new  PropertyResolverConverter());
     }

};

DropDownChoice must have its own model.

OTHER TIPS

Try with model chaining. You should replace variable code of the form with a property model built like this:

//this is the model of your DropDownChoice
Model<MyClass> myModel = new Model<MyClass>();
//...

PropertModel myCode = new PropertModel(myModel, "code");

Now use myCode instead of code in your form.

Take a look at DDC's type argument: In your case it's City, thus the DDC works with City objects.

If you don't want that, change the type and the compiler will guide you the way:

DropDownChoice<String> drop = new DropDownChoice<String>("residenceZipCode",zipCodesList,new CityChoiceRenderer());

class CityChoiceRenderer implements IChoiceRenderer<String> {
  Object getDisplayValue(String zipCode) {
    return zipCodeToName(zipCode);
  }

  String getIdValue(String zipCode, int index) {
    return zipCode;
  }
}

Its bettere to write some code:

    class City {
      private String zipCode;
      private String name;
    }

    class Residence {
      private String residenceZipCode;
    }

protected Residence res;

public ResidenceForm() {
  super("form", new CompoundPropertyModel<Residence>(res));

  List<City> cityList = someManager().loadAllCity();

  DropDownChoice<City> drop = new DropDownChoice<City>("residenceZipCode",cityList,new ChoiceRenderer<City>("name", "zipcode"));
add(drop);

Now. I cannot change Residence or City and I cannot write code inside onSubmit button's method.

What I want is zipcode inside Residence. What I obtain now is the equivalent of City.toString()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top