samedi 29 juin 2013

Change Wicket component behavior

Behaviors are kind of plug-ins for Components. They allow functionality to be added to a component and get essential events forwarded by the component. They can be bound to a concrete component (using the bind method which is called when the behavior is attached), but they don't need to. They can modify the components markup by changing the rendered ComponentTag. These are some of them : 
  1. AbstractAjaxTimerBehavior

  2. A behavior that generates an AJAX update callback at a regular interval. java doc

    WicketComponenet.add(new AbstractAjaxTimerBehavior(Duration.milliseconds(300)){
    @Override
    protected void onTimer(AjaxRequestTarget target) {
    // TODO Auto-generated method stub
    System.out.println("ajax here!");
    target.add(ComponentToModify);
    }});
    



  3. AjaxEventBehavior

  4. An ajax behavior that is attached to a certain client-side (usually javascript) event, such as onClick, onChange, onKeyDown, etc. Example:
    WebMarkupContainer div=new WebMarkupContainer(...);
             div.setOutputMarkupId(true);
             div.add(new AjaxEventBehavior("onclick") {
                 protected void onEvent(AjaxRequestTarget target) {
                     System.out.println("ajax here!");
                 }
             }
    
    This behavior will be linked to the onclick javascript event of the div WebMarkupContainer represents, and so anytime a user clicks this div the onEvent(AjaxRequestTarget) of the behavior is invoked. Java doc

  5. AjaxFormComponentUpdatingBehavior

  6. If you have a form component in wicket with some components and you want to change using ajax some things in that form before submit (when you click or edit on one of the components), then you have to use AjaxFormComponentUpdatingBehavior. This is a behavior that updates the hosting FormComponent via ajax when an event it is attached to is triggered.

    WicketComponenet.add(new AjaxFormComponentUpdatingBehavior("onchange"){ 
    @Override
    protected void onUpdate(AjaxRequestTarget target){
    // TODO 
    System.out.println("ajax here!"); 
    target.add(ComponentToModify);
    }}.setThrottleDelay(Duration.seconds(1))
    );
    ComponentToModify.setOutputMarkupId( true );
    

    The key is that your component must be a FormComponent, otherwise the event won't be triggered. Simple components like CheckBox, TextField, DropDownChoice are all FormComponents. Java Doc
    you should be careful to set setOutputMarkupId(true) of the component that will be modified after defining the behavior !

Aucun commentaire:

Enregistrer un commentaire