Hello,

I have a page in which there are 2 DropDownChoices. Both have to be set with an 
initial value.
The first DDC called  "mapCategory" is populated from a simple List.
The second DDC, "chosenArmy" is populated by a List of objects ("Army") that 
can change, based on the value of the first DDC and other options. 
I used AJAX to change the value of the second DDC based on the first DDC.
In addition, the default value of the second DDC is also set when the user 
selects the value of the first DDC.
"stwMatchPrefs" is the property model set to the form.

When the user  sets the value on the first DDC, the value of the second DDC is 
set without problems, even the selected value is set correctly.
The problem is that if the user changes the value on the first DDC 
(mapCategory) and then submits, the value passed on the second DDC (chosenArmy) 
is always "null", so the value received by "stwMatchPrefs" is null.
If the user submits without changing the value on the first DDC, everything 
work and the value of "stwMatchPrefs" is set correctly.
If the user changes the value on the first DDC (mapCategory) and then submits 
twice, the first time the value of "chosenArmy" is null, but the second submit 
has the right value.

Can anyone help me?
Thanks in advance.



Here's the page, with both DDC
I'm using wicket 1.3.3. Lines 57-65 have the AJAX code.


public class StwFrontPage extends BasePage {
    @SpringBean(name="armyServices")
    private ArmyServices armyServices;
    StwData stwData;
    StwMatchPrefs stwMatchPrefs;

    public StwFrontPage() {
        stwMatchPrefs = new StwMatchPrefs();
        CompoundPropertyModel userProfileModel = new 
CompoundPropertyModel(stwMatchPrefs);
        Form stwPrefsForm = new StwPrefForm("StwPrefsForm",userProfileModel);
        add(stwPrefsForm);

        stwMatchPrefs.setMapCategory(StwMapCategory.SMALL);
        DropDownChoice mapCategory = new DropDownChoice("mapCategory", new 
PropertyModel(stwMatchPrefs, "mapCategory"), StwMapCategory.getList());

        stwPrefsForm.add(mapCategory);

        IChoiceRenderer armyChoicesRenderer = new IChoiceRenderer()
        {
            public Object getDisplayValue(Object o) {
                return ((Army)o).getName();
            }

            public String getIdValue(Object o, int i) {
                return Integer.toString(i);
            }
        };

        stwData = AbismoWicketWebSession.get().getNewAbismoUser().getStwData();
        if(stwData == null){
            stwData = new StwData();
        }
        if(!stwData.isInitialized()){
            stwData.init(AbismoWicketWebSession.get().getNewAbismoUser(),
                        armyServices.getBasicArmy(StwMapCategory.SMALL),
                        armyServices.getBasicArmy(StwMapCategory.MEDIUM),
                        armyServices.getBasicArmy(StwMapCategory.LARGE)
            );
        }
        IModel armyChoices = new AbstractReadOnlyModel()
        {
            public Object getObject()
            {
                List <Army> armys = new ArrayList <Army> ();
                armys.add(stwData.getArmyByCategory(StwMapCategory.SMALL));
                armys.add(stwData.getArmyByCategory(StwMapCategory.MEDIUM));
                armys.add(stwData.getArmyByCategory(StwMapCategory.LARGE));
                
stwMatchPrefs.setChosenArmy(stwData.getArmyByCategory(stwMatchPrefs.getMapCategory()));
                return armys;
            }
        };

        final DropDownChoice chosenArmy = new DropDownChoice("chosenArmy", new 
PropertyModel(stwMatchPrefs, "chosenArmy"), armyChoices, armyChoicesRenderer );
        chosenArmy.setOutputMarkupId(true);
        stwPrefsForm.add(chosenArmy);

        mapCategory.add(new AjaxFormComponentUpdatingBehavior("onchange")
        {
            protected void onUpdate(AjaxRequestTarget target)
            {
                
stwMatchPrefs.setChosenArmy(stwData.getArmyByCategory(stwMatchPrefs.getMapCategory()));
                chosenArmy.setModelObject(stwMatchPrefs.getChosenArmy());
                target.addComponent(chosenArmy);
            }
        });
    }

    public ArmyServices getArmyServices() {
        return armyServices;
    }

    public void setArmyServices(ArmyServices armyServices) {
        this.armyServices = armyServices;
    }

    class StwPrefForm extends BaseForm {
        // PropertyModel is an IModel implementation
        public StwPrefForm(String id, IModel model) {
            super(id, model);
        }

        @Override
        public void onSubmit() {
            StwMatchPrefs stwMatchPrefs = (StwMatchPrefs)getModelObject();
            System.out.println("stwMatchPrefs.getMapCategory() = " + 
stwMatchPrefs.getMapCategory());
            System.out.println("stwMatchPrefs.getChosenArmy() = " + 
stwMatchPrefs.getChosenArmy());
        }
    }
}


      

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to