2013-11-27 17 views
0

我有一個輸出標記佔位符標籤設置爲true的容器。我只想在某個文本字段中輸入某個字符串時才顯示它。例如,如果我在文本字段中顯示數字「顯示」,容器會出現,如果我數字「隱藏」它會消失。我做了這段代碼:容器可見性控制改變Wicket 1.4文本字段的行爲

container.setOutputPlaceHolderTag(true); 
container.setOuputMarkupId(true); 

textfield.add(new OnChangeAjaxBehavior() { 

      @Override 
      protected void onUpdate(AjaxRequestTarget target) { 
       form.modelChanged(); 

       if ("SHOW".equals(textfield.getModelObject())) { 
        container.setVisible(true); 
       } else { 

        container.setVisible(false); 
       } 
       target.addComponent(container); 
} 

這段代碼只有在我寫SHOW的時候才起作用,但是當我寫另一個字符串時它並沒有消失。爲了讓它消失,我必須刷新它們所進入的整個表單(而且我不需要它)。 我該如何解決這個問題?

一些細節:我引用的所有組件都在一個窗體中,並且只有當我刷新窗體setVisible(false)時才起作用。從現在開始,只有setVisible(true)才起作用,看起來容器在可見性方面確實如此。

+0

您是否嘗試過的明確文本字段添加到AjaxRequestTarget? –

+0

你是什麼意思,明確添加?什麼? – andPat

+0

target.addComponent(textfield); –

回答

0

使用

container.setOutputPlaceHolderTag(true); 
container.setOuputMarkupId(true); 

textfield.add(new OnChangeAjaxBehavior() { 

      @Override 
      protected void onUpdate(AjaxRequestTarget target) { 
       form.modelChanged(); 

       if ("SHOW".equals(((TextField<String>) getComponent()).getModelObject())) { //change this 
        container.setVisible(true); 
       } else { 

        container.setVisible(false); 
       } 
       target.addComponent(container); 
} 

我得到了這個提示從Getting a Wicket text box's value in an AJAX onchange event

+0

我不認爲有必要改變它,因爲它進入正確的分支也沒有這個改變!我的意思是,如果我的數字顯示它進去,如果我數字HIDE(或另一個字符串)進入其他分支沒有你的改變。有一些容器刷新wicket或setVisible()方法我失蹤或我不知道。 – andPat

+0

好吧,其餘的對我來說還好吧;你在AJAX DEBUG窗口中看到了什麼? /標記中有什麼特別的東西? – ljgw

+0

不幸的是沒有... – andPat

1

此代碼:

public class HomePage extends WebPage { 

    private static final long serialVersionUID = 1L; 

    private String someValue; 
    private WebMarkupContainer container; 

    public HomePage(final PageParameters parameters) { 
     super(parameters); 
     add(container = container()); 
     add(textfield()); 

    } 

    private WebMarkupContainer container() { 
     WebMarkupContainer wmc = new WebMarkupContainer("container") { 

      @Override 
      protected void onConfigure() { 
       super.onConfigure(); 
       setVisible("SHOW".equals(someValue)); 
      } 

     }; 
     wmc.setOutputMarkupPlaceholderTag(true); 
     return wmc; 
    } 

    private TextField textfield() { 
     TextField tf = new TextField("textfield", new PropertyModel(HomePage.this, "someValue")); 
     tf.add(new OnChangeAjaxBehavior() { 

      @Override 
      protected void onUpdate(AjaxRequestTarget art) { 
       //just model update 
       art.add(container); 
      } 
     }); 
     return tf; 
    } 

} 
+0

不幸的是,行爲是一樣的,這沒有解決我的問題,但是一個不錯的,有效的替代方式謝謝! – andPat

+0

你使用什麼瀏覽器? Firefox或Chrome開發人員工具中是否有任何錯誤? –

+0

我使用Firefox 25.0和調試器控制檯打印沒有什麼奇怪的,當我嘗試數字顯示或隱藏或其他字符串時沒有錯誤 – andPat