2011-06-10 28 views
2

我有一個AjaxEditableLabel如何讓AjaxEditableLabel顯示TextField?

AjaxEditableLabel<String> task = new AjaxEditableLabel<String>("task", new PropertyModel<String>(getModel(), "task")); 

現在我所要做的是在一定條件下這個AjaxEditableLabel將呈現爲TextField。默認情況下,它呈現爲Label。如果模型String任務爲空或空白,則AjaxEditableLabel將爲TextField,否則將爲Label

可能嗎?

謝謝。

編輯:

AjaxEditableLabel<String> task = new AjaxEditableLabel<String>("task", new PropertyModel<String>(getModel(), "task")) { 

    private static final long serialVersionUID = 40L; 

    private TextField<String> editor; 

    protected FormComponent<String> newEditor(MarkupContainer parent, String componentId, IModel<String> model) { 
     editor = (TextField<String>) super.newEditor(parent, componentId, model); 
     Object modelObject = getDefaultModelObject(); 
     if (modelObject == null || "".equals(modelObject)) { 
      editor.setVisible(true); 
     } else { 
      editor.setVisible(false); 
     } 

     return editor; 
    } 

    protected WebComponent newLabel(MarkupContainer parent, String componentId, IModel<String> model) { 
     Label label = (Label) super.newLabel(parent, componentId, model); 
     if (editor.isVisible()) { 
      label.setVisible(false); 
     } 

     return label; 
    } 
}; 

回答

4

如果您在AjaxEditableLabel看它有用於初始化編輯器/標籤兩種方法:newEditornewLabel。在默認的newEditor方法中,它將編輯器設置爲不可見。重寫這兩個方法並添加自定義邏輯以顯示基於模型值的組件應該做你想要的。

+0

謝謝,我在原文中添加了修改後的'AjaxEditableLabel'。 – 2011-06-10 09:45:45