Reputation: 421090
I'm creating a form in which certain text fields should be enabled only if a certain checkbox is checked.
I'm fairly new to Wicket, and I'm curious about the following:
Can I enable/disable the field without an (AJAX) round-trip to the server using existing Wicket classes? I.e. is it possible to add behaviors to the fields which causes the rendered HTML to include JavaScript that enables/disables the fields?
If the answer to the above question is no: Is it "legal" for me to add the required JavaScript code myself in the markup? Or do I run the risk of messing up the form submission so that it is not properly recognized by Wicket?
I'm new to Wicket and I'm not sure what is best practice here. Perhaps the Wicket-way of doing this always involves an AJAX round-trip?
Upvotes: 1
Views: 477
Reputation: 4093
Can I enable/disable the field without an (AJAX) round-trip to the server using existing Wicket classes? I.e. is it possible to add behaviors to the fields which causes the rendered HTML to include JavaScript that enables/disables the fields?
Yes, you can do it without a server round trip. An no, not with a class provided by Wicket, you will have to create that yourself. Here's a behavior that you can add to a component. If the component with this behavior is clicked, the target component passed in the constructor will be disabled via javascript:
public class DisableFormComponentBehavior extends Behavior {
private Component sourceComponent;
private FormComponent targetComponent;
public DisableFormComponentBehavior(FormComponent targetComponent) {
targetComponent.setOutputMarkupId(true);
this.targetComponent = targetComponent;
}
public void bind(Component component) {
super.bind(component);
component.setOutputMarkupId(true);
this.sourceComponent = component;
}
@Override
public void renderHead(Component component, IHeaderResponse response) {
super.renderHead(component, response);
response.render(JavaScriptHeaderItem
.forReference(JQueryResourceReference.get()));
response.render(OnDomReadyHeaderItem.forScript(String
.format("$('#%s').click(function(){$('#%s').prop('disabled', true);});",
component.getMarkupId(), targetComponent.getMarkupId())));
}
}
If the answer to the above question is no: Is it "legal" for me to add the required JavaScript code myself in the markup? Or do I run the risk of messing up the form submission so that it is not properly recognized by Wicket?
Well, the answer to above question is not "no", but it is still perfectly legal to add your own javascript to the Wicket html files. You will have to define the ids of your components in the HTML then, however. I like the solution with Behaviors better, though, because it is more reusable.
Upvotes: 2