Koekiebox
Koekiebox

Reputation: 5963

Wicket onchange event returns the old value

I am attaching an onchange event using the Wicket framework. The problem is that the old value keeps being returned as the event is fired (Which makes sense). See code below.

What I want to do is get the "new" value of the select / dropbox after it has changed.

How can I achieve this?

    dropdown.add(new AjaxEventBehavior("onchange") {

        /**
         *
         * @param target
         */
        @Override
        protected void onEvent(AjaxRequestTarget target) {
            Component component = getComponent();
            DropDownChoice dropdown = (DropDownChoice) component;

            String value = dropdown.getValue();//This brings back the old value…
        }
    })

Upvotes: 1

Views: 4194

Answers (3)

Rangel Preis
Rangel Preis

Reputation: 394

Take a look at this wicket examples

Dropdown ajax example

Dropdown ajax example demo with code

attention to the method: wantOnSelectionChangedNotifications

Whether this component's onSelectionChanged event handler should be called using javascript window.location if the selection changes. If true, a roundtrip will be generated with each selection change, resulting in the model being updated (of just this component) and onSelectionChanged being called. This method returns false by default. If you wish to use Ajax instead, let wantOnSelectionChangedNotifications() return false and add an AjaxFormComponentUpdatingBehavior to the component using the onchange event.

Upvotes: 1

svenmeier
svenmeier

Reputation: 5681

You'll have to use an AjaxFormComponentUpdatingBehavior, otherwise the new value will not be submitted, then call getModelObject() to get hold of the new selection.

Upvotes: 4

drobson
drobson

Reputation: 955

It may be better to get the value from the underlying model. Also there is no need to use getComponent when you have access to the variable 'dropdown'.

dropdown.add(new AjaxEventBehavior("onchange") {

    /**
     *
     * @param target
     */
    @Override
    protected void onEvent(AjaxRequestTarget target) {

        String value = dropdown.getModelObject();
    }
})

Upvotes: 0

Related Questions