2010-12-16 21 views
0

我使用組合框/下拉菜單來選擇用戶語言。如果用戶更改了語言,我想更新所有標籤,但只保留輸入元素。Wicket:如何在不丟失現有輸入的情況下恢復當前表單?

在jQuery中,我會要求標籤的ID,並通過JSON的新法規的清單,然後使用這樣的循環:

var texts = {[ {id:'nameLabel', text:'First Name'}, {id:'familyLabel', text:'Family Name'} ]}; 
for(var i=0; i<texts.length; i++) { 
    var item = texts[i]; 
    $('#'+item.id).text(item.text); 
} 

這將更新所有標籤,而無需修改任何東西。我怎樣在Wicket上做到這一點?

[編輯]我想什麼:

 DropDownChoice<Locale> ddc = new DropDownChoice<Locale>(...); 
     ddc.add(new AjaxFormComponentUpdatingBehavior("onchange") { 
      private static final long serialVersionUID = 1L; 

      @Override 
      protected void onUpdate(AjaxRequestTarget target) { 
       getSession().setLocale(language); 
       for(MarkupContainer label : labels) { 
        target.addComponent(label); 
       } 
      } 
     }); 

這確實改變了標籤,但它也再次呈現所有輸入字段。我發現無法訪問輸入字段的當前值。

[EDIT2]標籤的列表中創建像這樣:

 StringResourceModel usernameLabel = new StringResourceModel("usernameLabel", this, new Model<ValueMap>(map)); 
     labels.add(add(new Label("usernameLabel", usernameLabel))); 
+0

在這一行:'爲(MarkupContainer標籤:標籤)'什麼是標籤,你怎麼弄的? – 2010-12-16 13:37:42

+0

標籤是頁面上所有標籤的列表。代碼見EDIT2。 – 2010-12-16 17:27:31

回答

1

這是錯誤的:

labels.add(add(new Label("usernameLabel", usernameLabel))); 

你不加標籤的實例來「標籤」,它的反覆添加你將它添加到的容器(可能是Page實例)。方法'add()'不返回正在添加的組件,它返回要添加組件的容器。

嘗試將其更改爲:

Label label = new Label("usernameLabel", usernameLabel); 
add(label); 
labels.add(label); 
+0

你是對的。我還需要調用'label.setOutputMarkupId(true);'但現在,它完美的工作! – 2010-12-17 09:48:06

相關問題