2011-06-10 85 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