[英]Wicket - Replace Label By TextField and vice versa
我試圖用ListView中的TextField替換Label。 幾周前,我在互聯網上看到了一個類似的檢票口示例,但我不記得該鏈接了。 我已經向要通過TextField替換Label的ListItem添加了AjaxEventBehavior-“ onDblClick”,還向TextField添加了AjaxFormComponentUpdatingBehavior-“ onBlur”以便將TextField替換為Label。 不知何故它不起作用。 ListView的列表模型僅包含{“ aaaaaaaaaaaaaaaaa”,“ bbbbbbbbbbbbbbbb”,“ cccccccccccccc”}(在我測試時),因此第一個Label將是“ aaaaaaaaaaaaaaa”,如果我雙擊此Label,則TextField會顯示標簽“ cccccccccccccccc”的位置,這是意外的。 而且“ onBlur”事件也不起作用。 希望我能解釋這些問題。 代碼如下:
public class TaskTypeSettingsPage extends BasePage implements Serializable {
private String val;
public TaskTypeSettingsPage() {
add(new TaskTypeSettingsForm("form"));
}
public void setVal(String val) {
this.val = val;
}
public String getVal() {
return val;
}
private class TaskTypeSettingsForm extends Form {
private static final long serialVersionUID = 10058L;
private Fragment labelFragment;
private Fragment textFragment;
public TaskTypeSettingsForm(String id) {
super(id);
setOutputMarkupId(true);
ListView listView = new ListView("row", Arrays.asList("aaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "cccccccccccccc")) {
@Override
protected void populateItem(ListItem item) {
String str = (String) item.getModelObject();
item.add(new Label("listLabel", str));
item.setOutputMarkupId(true);
labelFragment = new Fragment("frag", "labelFragment", this.getPage());
Label label = new Label("label", str);
label.setOutputMarkupId(true);
labelFragment.setOutputMarkupId(true);
labelFragment.add(label);
item.add(labelFragment);
textFragment = new Fragment("frag", "textFragment", this.getPage());
TextField text = new TextField("text", new PropertyModel(TaskTypeSettingsPage.this, "val"));
text.setOutputMarkupId(true);
textFragment.setOutputMarkupId(true);
textFragment.add(text);
item.add(new AjaxEventBehavior("onDblClick") {
@Override
protected void onEvent(AjaxRequestTarget target) {
labelFragment.replaceWith(textFragment);
labelFragment = textFragment;
target.addComponent(textFragment);
}
});
text.add(new AjaxFormComponentUpdatingBehavior("onBlur") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
textFragment.replaceWith(labelFragment);
textFragment = labelFragment;
target.addComponent(labelFragment);
}
});
}
};
add(listView);
}
}
}
和
<html>
<body>
<wicket:extend>
<div class="heading"><wicket:message key="extras.taskType" /></div>
<form wicket:id="form" autocomplete="off">
<table>
<tr wicket:id="row">
<td>
<span wicket:id="frag"></span>
</td>
</tr>
</table>
<wicket:fragment wicket:id="labelFragment"><span wicket:id="label"></span>
</wicket:fragment>
<wicket:fragment wicket:id="textFragment"><input type="text" wicket:id="text">
</wicket:fragment>
</form>
</wicket:extend>
</body>
</html>
任何信息或示例代碼將對我非常有幫助。 謝謝。 編輯:我找到了鏈接: 示例,但源代碼不可用。
您可以替換整個組件,但還必須考慮到標簽和文本字段都不能使用相同的標記。 但是您始終可以用另一個片段替換一個片段,因此,如果將字段和標簽分別包裹在一個片段中,則可以隨時在它們之間切換。
但是,最好為此目的使用專用組件,我似乎還記得在核心Wicket或Wicket Extensions中執行此操作的Ajax字段組件。 它稱為AjaxEditableLabel
您要記住的示例可能就是這個可編輯的標簽示例 。
有一個具有ViewOrEdit組件的Visural Wicket庫。 聽起來好像您在尋找什么。
ListView組件可能不是表單的最佳依據。 參見http://wicketinaction.com/2008/10/building-a-listeditor-form-component/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.