Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handling to onchange event of AutoCompleteTextField in wicket

I'm writing an autocomplete component for a webapp using Java and Wicket.

Is there a way to handle the onchange event to run some code when the user selects one of the options of the autocomplete list? I tried doing this in the AutoCompleteTextField:

        setOutputMarkupId(true);
        add(new AjaxEventBehavior("onchange") {
            @Override
            protected void onEvent(AjaxRequestTarget target) {
                System.out.println(getInput());
            }
        });

But the getInput method returns null. :(
Is there a way to react to the onchange event and to be able to read what the user has entered?

Thanks for you time and knowledge :)

like image 255
Manuel Araoz Avatar asked Mar 07 '11 18:03

Manuel Araoz


1 Answers

The onchange event is only fired when the focus is moved away from the component. (This is a universal browser/javascript thing.)

You need to hook your handler to the onkeypress event instead.

What you need is not AjaxEventBehavior but AjaxFormComponentUpdatingBehavior:

    add( new AjaxFormComponentUpdatingBehavior( "onchange") {

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            System.out.println( "Value: "+field.getValue() );

        }
    });

Although it works with getInput() too, but usually the somewhat higher level (properly escaped and backed by the model) getValue() is a better fit.

like image 118
biziclop Avatar answered Sep 19 '22 15:09

biziclop