2011-05-07 35 views

回答

9

的onClick(的)重複使用正在操作在存儲器中的同一對象上。如果你沒有使用Ajax,你仍然可以在Link的匿名子類中保留一些狀態。然後,您可以使用onBeforeRender()和onComponentTag()更改每次顯示的方式。

Link<Void> link = new Link<Void>("myLink") { 

    private String customCSS = null; 
    private boolean customEnabled = true; 

    public void onClick() { 
     if (/* test to determine disabled */) { 
      customCSS = "disabled"; 
      customEnabled = false; 
     } else { 
      customCSS = null; 
      customEnabled = true; 
     } 
    } 

    @Override 
    protected void onComponentTag(ComponentTag tag) { 
     super.onComponentTag(tag); 
     if (customCSS != null) 
      tag.put("class", customCSS); 
    } 

    @Override 
    public boolean isEnabled() { 
     return super.isEnabled() && customEnabled; 
    } 
}; 

AttributeModifiers(或其他行爲)不利於這種情況下,因爲,如果你在onclick()方法中添加他們,他們就會開始堆積在同一條鏈路的每個點擊 - 因爲它們都作爲鏈路狀態的一部分。

您的鏈接可以保持狀態的所有方式的軌道,讓你的onClick()方法來實現與重複點擊/禁用/變更/等。

您還可以覆蓋onBeforeRender(),可見性(),而每個鏈接顯示在頁面上同時運行其他方法。無論您單擊按鈕多少次,構造函數onConfigure()和其他構造函數都只運行一次。

+2

謝謝 - 覆蓋isEnabled()訣竅 – 2011-05-08 12:55:14

+1

正如在JavaDoc中所述,重寫isEnabled和isVisible應謹慎使用 - 儘管我一直都在使用它。只要確保操作成本非常低廉。 – jbrookover 2011-05-08 13:31:19

+0

即使在wicket wiki中提到該解決方案,如果wicket支持類似於開箱即用的東西,是不是像重新發明輪子那樣? – Nicktar 2011-05-09 07:53:03

0

我覺得AjaxCallDecorator應該是你需要使用禁用按鈕/改變風格類。

+0

我沒有使用Ajax – 2011-05-07 19:11:08

0

看看SimpleAttributeModifierAttributeAppender。根據你的實際要求,其中一個應該做的伎倆。 SimpleAttributeModifier添加或替換在wicket中具有prepresentation的任何HTML-Tag的屬性(替換css類),而AttributeAppender附加到屬性(添加另一個CSS類)。這應該用於啓用/禁用按鈕,但我沒有嘗試過。

例子:

Label label = new Label("id", "Some silly text.") 
add(label); 
label.add(new SimpleAttributeModifier("class", "my-css-class"); 

對於Ajax,你必須將組件添加到目標也是如此。

更詳細的例子:

Java代碼:

import org.apache.wicket.behavior.AttributeAppender; 
import org.apache.wicket.behavior.SimpleAttributeModifier; 
import org.apache.wicket.markup.html.WebMarkupContainer; 
import org.apache.wicket.markup.html.WebPage; 
import org.apache.wicket.markup.html.form.Button; 
import org.apache.wicket.markup.html.form.Form; 
import org.apache.wicket.markup.html.link.Link; 
import org.apache.wicket.model.Model; 

public class DemoPage extends WebPage { 

    public DemoPage() { 
     Form form = new Form("form"); 
     add(form); 
     final WebMarkupContainer wmc = new WebMarkupContainer("greenText"); 
     form.add(wmc); 
     form.add(new Link("redLink"){ 

      @Override 
      public void onClick() { 
       wmc.add(new SimpleAttributeModifier("class", "redText")); 
      }}); 
     final Button boldButton = new Button("boldButton"){ 

      @Override 
      public void onSubmit() { 
       wmc.add(new AttributeAppender("class", true, new Model<String>("boldText"), " ")); 
      }}; 
     form.add(boldButton); 
     Link disabler = new Link("buttonDisabler") { 

      @Override 
      public void onClick() { 
       boldButton.add(new AttributeAppender("disabled", true, new Model<String>("disabled"), " "));     
      } 

     }; 
     form.add(disabler); 
    } 

} 

相應的HTML:

<html> 
<head> 
<style> 
.redText { 
    color: red; 
    } 
.greenText { 
    color: green; 
    } 
.boldText { 
    font-weight: bold; 
    } 
</style> 
</head> 
<body> 
<form wicket:id="form"> 
<div class="greenText" wicket:id="greenText">This is Green.</div><br /> 
<a href="" wicket:id="redLink">Make it red</a><br /> 
<input type="submit" wicket:id="boldButton" value="Make it bold" /><br /> 
<a href="" wicket:id="buttonDisabler">Disable the button</a> 
</form> 
</body> 
</html> 
+0

在鏈接的onClick()方法內動態添加時,SimpleAttributeModifier似乎不起作用。我也嘗試在構建過程中添加一個AttributeModifier,但它根本沒有效果。 – 2011-05-07 19:30:18

+0

@Michael Borgwardt適合我...我添加了一個工作示例,讓你走... – Nicktar 2011-05-08 10:19:02

0

的問題是,您使用的鏈接。已禁用的鏈接使用<span>而不是<a>/<button>,默認情況下使用<em>。 使用Button組件將在元素中設置'disabled'屬性。

+0

我將鏈接代碼更改爲Button,但setEnabled(false)仍然沒有任何效果。 – 2011-05-07 19:12:34

3

我不認爲這是在Wicket完全好主意。當然,它可以通過弄虛作假來完成,但它的簡單得多之一:

  1. 覆蓋的isEnabled()方法返回從表單/組件的模型得出的值。
  2. 創建組件時附加AttributeModifier,並使用返回上述派生值的模型。

無論您選擇哪一種,原則是讓Wicket「拉」渲染信息而不是明確推送它。

2

Michael Borgwardt提供的答案基本正確。

問題是你使用鏈接。已禁用的鏈接使用<span>而不是 <a>/<button>,默認情況下使用<em>。使用按鈕 組件將在元素中設置'禁用'屬性。

我想補充一點,您需要使用HTML按鈕元素而不是<a>(鏈接)。最初的回答可以是反饋,因爲Wicket中也存在鏈接和按鈕。