Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
html
html
<form wicket:id="form">
  <select name="PhoneVendor" wicket:id="phoneVendor"></select>
  <select name="PhoneModel" wicket:id="phoneModel"></select>
</form>

...

Code Block
private DropDownChoice _phoneModelDDC;
   private MyModel _myModel = new MyModel();

   public MyPage(PageParameters parameters) {
        Form form = new Form("form", new CompoundPropertyModel(_myModel));
        add(form);

        addPhoneVendor(form);
        _phoneModelDDC = addPhoneModel(form);
    }

    private void addPhoneVendor(final Form form) {
        ArrayList phoneVendors = new ArrayList(getMySession().getTerminalVendors());
        form.add(new DropDownChoice("phoneVendor", phoneVendors) {
            /**
             * Whether this component's onSelectionChanged event handler should called using
             * javascript if the selection changes.
             *
             * @return True if this component's onSelectionChanged event handler should
             *         called using javascript if the selection changes
             */
            protected boolean wantOnSelectionChangedNotifications() {
                return true;
            }

.add(new FormComponentUpdatingBehavior() {

            /**
             * Called when a option is selected of a dropdown list that wants to be
             * notified of this event.
             *
             * @param newSelection The newly selected object of the backing model
             */
            protected void onSelectionChangedonUpdate(final Object newSelection) {
                String phoneVendor = (String) newSelectiongetFormComponent().getModelObject();
                _myModel.setPhoneModel(null);   // Reset the phone model when the vendor changes
                phoneModelDDC.setChoices(getTerminalsByVendor(phoneVendor));
            }
        }));
    }

    private DropDownChoice addPhoneModel(Form form) {
        List list = Collections.EMPTY_LIST;
        String phoneVendor = _myModel.getPhoneVendor();
        if (phoneVendor != null) {
            list = getTerminalsByVendor(phoneVendor);
        }

        DropDownChoice phoneModelDDC = new DropDownChoice("phoneModel", list) {
            protected boolean wantOnSelectionChangedNotifications() {
                return true;
            }
        };
        form.add(phoneModelDDC);
        return phoneModelDDC;
    }

...

Code Block
private DropDownChoice _phoneVendorDDC, _phoneModelDDC;
   private MyModel _myModel = new MyModel();

   public MyPage(PageParameters parameters) {
        Form form = new Form("form", new CompoundPropertyModel(_myModel));
        add(form);

        _phoneVendorDDC = getPhoneVendorDDC(form);
        _phoneModelDDC = getPhoneModelDDC(form);

        form.add(_phoneVendorDDC);
        form.add(_phoneModelDDC);
    }

    private DropDownChoice getPhoneVendorDDC(final Form form) {
        ArrayList phoneVendors = new ArrayList(getMySession().getTerminalVendors());
        DropDownChoice phoneVendorDDC = new DropDownChoice("phoneVendor", phoneVendors);
        // Add Ajax Behaviour...
        phoneVendorDDC.add(new AjaxFormComponentUpdatingBehavior("onchange") {
            protected void onUpdate(AjaxRequestTarget target) {
                // Reset the phone model dropdown when the vendor changes
                _myModel.setPhoneModel(null);
                _phoneModelDDC.setChoices(getTerminalsByVendor(_myModel.getPhoneVendor()));
                target.addComponent(_phoneModelDDC);
            }
        });
        return phoneVendorDDC;
    }

    private DropDownChoice getPhoneModelDDC(Form form) {
        List list = Collections.EMPTY_LIST;
        String phoneVendor = _myModel.getPhoneVendor();
        if (phoneVendor != null) {
            list = getTerminalsByVendor(phoneVendor);
        }
        DropDownChoice phoneModelDDC = new DropDownChoice("phoneModel", list);
        phoneModelDDC.setOutputMarkupId(true);  // Needed for Ajax to update it
        return phoneModelDDC;
    }

...