2010-08-31 57 views
2

在wicket中,AttributeModifier不會更改AjaxLink的屬性「class」。它應該改變類屬性並改變鏈接的樣子。AjaxLink的Wicket AttributeModifier

public class TestPage extends WebPage { 
    private AjaxLink link1; 
    private AjaxLink link2; 

    public TestPage() { 
     super(); 

     link1 = new AjaxLink("link1") { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void onClick(AjaxRequestTarget target) { 
       switchView("view1"); 
      } 
     }; 

     link2 = new AjaxLink("link2") { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void onClick(AjaxRequestTarget target) { 
       switchView("view2"); 
      } 
     }; 

     link1.setOutputMarkupId(true); 
     link2.setOutputMarkupId(true); 

     link1.add(new AttributeModifier("class", true, new Model<String>("active"))); 
     link2.add(new AttributeModifier("class", true, new Model<String>("inactive"))); 

     add(link1); 
     add(link2); 
    } 

    private void switchView(String viewName) { 
     if (viewName.equals("view1")) { 
      link1.add(new AttributeModifier("class", true, new Model<String>("active"))); 
      link2.add(new AttributeModifier("class", true, new Model<String>("inactive"))); 
     } else if (viewName.equals("view2")) { 
      link1.add(new AttributeModifier("class", true, new Model<String>("inactive"))); 
      link2.add(new AttributeModifier("class", true, new Model<String>("active"))); 
     } 
    } 
} 

相應的HTML文件看起來像:

<html xmlns:wicket> 
<body> 
<wicket:extend> 
<div id="tabs"> 
<ul> 
    <li><a wicket:id="link1">View1</a></li> 
    <li><a wicket:id="link2">View2</a></li> 
</ul> 
</div> 
</wicket:extend> 
</body> 
</html> 

感謝

回答

4

你沒有真正告訴檢票口做在Ajax響應任何東西。

一個可能的解決方法是添加行:

target.addComponent(link1); 
target.addComponent(link2); 

(或做這個函數的調用)同時你的onClick方法。

+0

D'oh!我沒有和wicket一起工作,忘記了addComponent()方法。有用。謝謝 – zinovii 2010-08-31 12:07:34

+0

那麼在這個例子中,target.addComponent()行是否在switchView()之前? – 2016-11-02 11:53:09

3

已經給出了正確的答案,您必須將組件添加到AjaxTarget。然而,創造更多的「Wickety」代碼的緣故,你可以重寫你的類是這樣的:

public class TestPage extends WebPage { 
    private AjaxLink link1; 
    private AjaxLink link2; 

    public TestPage() { 
     super(); 

     final Model<Boolean> link1Model = new Model<Boolean>(Boolean.True); 
     final Model<Boolean> link2Model = new Model<Boolean>(Boolean.False); 

     link1 = new AjaxLink<Boolean>("link1", link1Model) { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void onClick(AjaxRequestTarget target) { 
       this.getModel().setObject(!this.getModel().getObject()); 
       target.addComponent(this); 
      } 
     }; 

     link2 = new AjaxLink<Boolean>("link2", link2Model) { 
      private static final long serialVersionUID = 1L; 

      @Override 
      public void onClick(AjaxRequestTarget target) { 
       this.getModel().setObject(!this.getModel().getObject()); 
       target.addComponent(this); 
      } 
     }; 

     link1.setOutputMarkupId(true); 
     link2.setOutputMarkupId(true); 

     link1.add(new AttributeModifier("class", true, new Model<String>() { 
      public String getObject() { 
       return link1Model.getObject() ? "active" : "inactive"; 
      } 
     })); 
     link2.add(new AttributeModifier("class", true, new Model<String>() { 
      public String getObject() { 
       return link1Model.getObject() ? "active" : "inactive"; 
      } 
     })); 

     add(link1); 
     add(link2); 
    } 
} 

它使用的模型和在檢票建議。然而,有重碼的重複對那裏發生的,所以我想也許建議去一個不同的部件:

public final class ActiveInactiveLink extends AjaxFallbackLink<Boolean> { 
    public ActiveInactiveLink(String id) { 
     super(id, new Model<Boolean>(Boolean.True)); 
     this.add(new AttributeModifier("class", true, new Model<String>() { 
      public String getObject() { 
       Model<Boolean> model = ActiveInactiveLink.this.getModel(); 
       return model.getObject() ? "active" : "inactive"; 
      } 
     })); 
     this.setOutputMarkupId(true); 
    } 

    @Override 
    public void onClick(AjaxRequestTarget target) { 
     this.getModel().setObject(!this.getModel().getObject()); 
     target.addComponent(this); 
    } 

    @Override 
    public void setModel(IModel<Boolean> model) { 
     if(model == null) 
      return; 
     this.model = model; 
    } 

} 

public class TestPage extends WebPage { 
     private AjaxLink link1; 
     private AjaxLink link2; 

     public TestPage() { 
      link1 = new ActiveInactiveLink("link1"); 
      link2 = new AjaxLink<Boolean>("link2"); 

      add(link1); 
      add(link2); 
     } 
    } 

所有這些東西是不是測試或編譯的,當然,所以沒有保證。 :)

此外,從檢票郵件列表的傢伙肯定會拿出一些更優雅......

享受。