0

我需要 Apache Wicket 应用程序中的可编辑文本。由于文本必须看起来非常“像普通表”,只有在用户双击文本等后才能进行编辑,所以使用普通的 TextFields 并不是一个真正的选择。

所以我决定选择新的 HTML5 属性contenteditable,它可以很好地完成整体工作。使用以下标记和 Java 代码,我得到一个看起来像静态文本的标签,但是在用户单击文本后,文本是可编辑的:

<div wicket:id="id" contenteditable></div>

...

item.add(new Label("id", "dummy content"));

但是现在我显然需要在用户实际编辑文本时捕获一些事件,因为新文本应该存储回数据库中。在线手册建议使用oninput它,因为它看起来比 更可靠(例如关于时间问题)onkeyuponkeydown等等。

使用常规 HTML5 尝试该事件可以正常工作:

<div wicket:id="id" contenteditable oninput='alert("oninput");'></div>

我现在的问题是,我怎样才能获得 Wicket 标签来支持oninput?覆盖它并创建一个自定义标签将是一个非常好的解决方案(如果我真的必须这样做的话),但是对于 Wicket 来说,我太陌生了,不知道从哪里开始以及如何创建正确的标记等等。

4

1 回答 1

6

Since a div is not a form element, it doesn't get submitted when you post a form. So you have two options:

  • in onInput fill a hidden form element with the content and submit that using a form
  • send the content to the server using Ajax

Both require you to do some magic using a (Ajax)Behavior.

You can use Wicket's HiddenField to create the hidden field, and in onInput perform the update of the HiddenField's value.

You can encapsulate this by creating your own ContentEditableFormComponent by using FormComponentPanel as a starting point.

于 2013-10-24T11:02:30.987 回答