Wicket ajax keyevent

I am new to Wicket and I want to create an ajax behavior that triggers my Javacode if the Functionkey is pressed.

My idea is to create a behavior that sends Javascript to the browser that only the F-Key calls an ajax callback.

public class HomePage extends WebPage { public HomePage(final PageParameters parameters) { super(parameters); add(new AbstractDefaultAjaxBehavior(){ @Override protected void respond(AjaxRequestTarget target) { //retrieve the Parametervalue from request final Request request = RequestCycle.get().getRequest(); final String jsKeycode = request.getRequestParameters() .getParameterValue("keycode").toString(""); //test output target.appendJavaScript("alert('from wicket ajax. you pressed "+jsKeycode+"')"); } @Override public void renderHead(Component component, IHeaderResponse response) { super.renderHead(component, response); //Append JavaScriptcode response.render(OnDomReadyHeaderItem.forScript( "\n\n" + "$(document).keydown(" + "function(event){\n" + //120, 121 Example for F9 and F10 "if((event.keyCode == 120) || (event.keyCode == 121)){\n" + "event.preventDefault();\n" + "window.alert('F-Key pressed');\n" + //perform ajax-callback with keyCode "}\n" + "});\n")); } }); 

Now my problem: What do I need to encode that the ajax callback will work with the key code pressed as a parameter?

+4
source share
2 answers

osmdamv give me a hint to find "Wicketsolution" for my problem. Now here is my code to catch Keypress and send only in some cases ajaxrequest to wicketserver.

In this example, another user may be able to adapt this code to their needs.

 public HomePage(final PageParameters parameters) { super(parameters); add(new AjaxEventBehavior("keydown"){ @Override protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { super.updateAjaxAttributes(attributes); IAjaxCallListener listener = new AjaxCallListener(){ @Override public CharSequence getPrecondition(Component component) { //this javascript code evaluates wether an ajaxcall is necessary. //Here only by keyocdes for F9 and F10 return "var keycode = Wicket.Event.keyCode(attrs.event);" + "if ((keycode == 120) || (keycode == 121))" + " return true;" + "else" + " return false;"; } }; attributes.getAjaxCallListeners().add(listener); //Append the pressed keycode to the ajaxrequest attributes.getDynamicExtraParameters() .add("var eventKeycode = Wicket.Event.keyCode(attrs.event);" + "return {keycode: eventKeycode};"); //whithout setting, no keyboard events will reach any inputfield attributes.setAllowDefault(true); } @Override protected void onEvent(AjaxRequestTarget target) { //Extract the keycode parameter from RequestCycle final Request request = RequestCycle.get().getRequest(); final String jsKeycode = request.getRequestParameters() .getParameterValue("keycode").toString(""); target.appendJavaScript("alert('from wicket ajax. you pressed "+jsKeycode+"')"); } }); 

Edit:

Insert attributes attribute.setAllowDefault (true). Now it works correctly.

+7
source

you should use AjaxEventBehavior using decorators

  @Override protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { super.updateAjaxAttributes(AjaxRequestAttributes attributes); IAjaxCallListener listener = new IAjaxCallListener() { @Override public CharSequence getBeforeHandler(Component c) { return handler; } ..... }; attributes.getAjaxCallListeners().add(listener); 

}

0
source

All Articles