You add an AjaxEventBehavior to a AjaxEditableMultiLineLabel that is NOT CORRECT. The AjaxEditableMultiLineLabel is a Panel that contains two components:
- Label
- Editor (textarea)
Any of Ajax behavior has to be add to any of these components. In your case you have to add a behavoir to the editor.
(following full working example is on https://repo.twinstone.org/projects/WISTF/repos/wicket-examples-6.x/browse )
The markup
<h2>Form</h2>
<form wicket:id="form">
<div wicket:id="text"></div>
<input wicket:id="submit" type="submit" value="Send" />
</form>
<hr/>
<h2>Result</h2>
<div wicket:id="result">[result placeholder]</div>
The code snippet
final IModel<String> textModel = Model.of("initial text");
Form<String> form = new Form<>("form", textModel);
add(form);
AjaxEditableMultiLineLabel<String> textArea = new AjaxEditableMultiLineLabel<String>(
"text", textModel) {
private static final long serialVersionUID = 1L;
@Override
protected FormComponent<String> newEditor(MarkupContainer parent,
String componentId, IModel<String> model) {
final FormComponent<String> editor = super.newEditor(parent,
componentId, model);
editor.add(new AjaxFormComponentUpdatingBehavior("keyup") {
private static final long serialVersionUID = 1L;
@Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.println("Ajax update for textArea " + editor.getDefaultModelObjectAsString());
}
});
return editor;
}
};
textArea.setCols(50);
textArea.setRows(20);
form.add(textArea);
form.add(new SubmitLink("submit"));
add(new Label("result", textModel));
WARNING: PREVIOUS CODE IS REALLY VERY SLOW
The code is very slow, becuase each keypress/release makes following:
- Call the server with the content of textarea
- Invoke code on the server side (update, render, etc.)
- Send back the whole result tag as html in an XML response (AJAX)
- Replace the whole result tag
It is slow on localhost, in a real internet is definitely not usable.
SOLUTION
Use a simple jQuery updating (JavaScript, jQuery is built in Wicket 6)
The code snippet with jQuery updating
final IModel<String> textModel = Model.of("initial text");
final Label result = new Label("result", textModel);
result.setOutputMarkupId(true);
add(result);
Form<String> form = new Form<>("form", textModel);
add(form);
AjaxEditableMultiLineLabel<String> textArea = new AjaxEditableMultiLineLabel<String>(
"text", textModel) {
private static final long serialVersionUID = 1L;
private boolean initialized = false;
private FormComponent<String> editor;
@Override
public void onEdit(AjaxRequestTarget target) {
super.onEdit(target);
if(!initialized) {
String editorId = editor.getMarkupId();
String resultId = result.getMarkupId();
StringWriter sw = new StringWriter();
sw.write("$('#");
sw.write(editorId);
sw.write("').keyup(function() { var str = $('#");
sw.write(editorId);
sw.write("').val(); ");
sw.write("$('#");
sw.write(resultId);
sw.write("').html(str); });");
String js = sw.toString();
System.out.println("Appending editor JS " + js);
target.appendJavaScript(js);
initialized = true;
}
}
@Override
protected FormComponent<String> newEditor(MarkupContainer parent,
String componentId, IModel<String> model) {
editor = super.newEditor(parent, componentId, model);
return editor;
}
};
textArea.setCols(50);
textArea.setRows(20);
form.add(textArea);
form.add(new SubmitLink("submit"));