javaajaxwicket

AjaxFormComponentUpdatingBehavior onkeypress


I have a list of items, above which there is an input fields.
The input field is a filter, it should filter the list based on the text you key in to the input field.

For example : If you type "th", it should filter the list so that all the items should start with "th".

For this I am using AjaxFormComponentUpadingBehavior("onkeypress").
But this does not seem to be working they way it should.
When I key in something it clears up that and takes the cursor to the first letter of the input field.

I have tried onkeyup and onkeydown, and all of them act the same way.
For now I am doing the filter on a link click which works, but I want it to be as seamless as onkeypress.

Is there a way to achieve this? I am using wicket 1.4.x

Here is the code :

        // Customer Filter input field
        customerFilterTxt = new TextField<String>("customerFilterTxt", new PropertyModel<String>(this, "slectedCustomerFilterStr"));
        customerFilterTxt.setOutputMarkupPlaceholderTag(true);
        customerListViewContainer.add(customerFilterTxt);
        
        // Ajax behavior for customer group filter auto complete input filed
        AjaxFormComponentUpdatingBehavior customerGroupFilterBehave = new AjaxFormComponentUpdatingBehavior("onkeypress") {
            private static final long serialVersionUID = 1L;

            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                List<CustomerGroupBean> filterList = new ArrayList<CustomerGroupBean>();
                if(Util.hasValue(selectedCustomerGroupFilterStr)) {
                    String str = selectedCustomerGroupFilterStr.toUpperCase();
                            
                    for(CustomerGroupBean group : custGroupList) {
                        if(group.getRightGroupName().toUpperCase().contains(str)) {
                            filterList.add(group);
                        }
                    }
                            
                    custGroupListView.setList(filterList);
                            
                } else {
                    custGroupListView.setList(custGroupList);
                }
                        
                target.addComponent(customerFilterTxt);
                target.addComponent(custGroupListViewContainer);
            }
        };
        customerGroupFilterTxt.add(customerGroupFilterBehave);

Solution

  • You're adding the input field to the update call within the update method. This instructs Wicket to replace the input field, rendering the text field again. Thats why the cursor jumps to the first position. Why do you add the text field to the update? I don't see any imperative for it. Also you may want to use the event "onkeyup".