2013-09-27 22 views
0

我嘗試在一個簡單的自定義組件中設置一個pagelink的動態css類值,並且找不到任何方法。帶動態css類的掛毯pagelink

我的組件......

<!-- my component template 'testLink' --> 
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"> 
    <!-- maybe I can set here something dynamic like that ... 
     <t:pagelink page="mytest" t:id="myLink" class="${myDynCss}"> 

     ... but in this case I need to pass the parameter what link is handled 
    --> 
    <t:pagelink page="mytest" t:id="myLink"> 
     I want dynamic css class    
    </t:pagelink> 
</html> 

組件Java代碼...

public class TestLink { 
    @Parameter(required=true) 
    private int activeId; 

    @Component 
    PageLink myLink; 

    public int getActiveId() { 
     return activeId; 
    } 

    public void setupRender() 
    { 
     // I try to set some class attribute here but I find no matching function in myLink 
     // myLink.setCssStyle(); 
    } 

    public String getMyDynCss(int currentLinkId) { 
     if (currentLinkId==activeId) 
      return "active"; 
     else 
      return "xxx"; 
    } 
} 

包括該組件的網頁...

<html t:type="layout" title="Test" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" 
     xmlns:p="tapestry:parameter"> 
    <p:app_navigation> 
     <t:testLink activeId="1000"/> 
    </p:app_navigation> 
</html> 

也許一個愚蠢的新手問題,但我仍然有困難以Tapestry方式思考。 歡迎提供任何幫助或有用的提示。

回答

2

從代碼中不太清楚currentLinkId和activeId之間的區別以及currentId來自哪裏。我幾乎假設你有一些你不在這裏分享的Loop設置。但是,如果您可以從封閉組件中獲取這些變量,那麼您幾乎可以在註釋掉的代碼中找到這些變量,您只需要從getMyDynCss()方法中刪除參數即可。像這樣:

的Java:

public class TestLink { 

    @Property 
    @Parameter(required=true) 
    private int activeId; 

    @Property 
    @Parameter(required=true) 
    private int currentId; 

    public String getMyDynCss() { 
     if (currentId == activeId) { 
      return "active"; 
     } 
     else { 
      return "xxx"; 
     } 
    } 
} 

您的TML:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"> 
    <t:pagelink page="mytest" t:id="myLink" class="${myDynCss}"> 
</html> 

你包圍組件:

<html t:type="layout" title="Test" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" 
     xmlns:p="tapestry:parameter"> 
    <p:app_navigation> 
     <t:testLink activeId="1000" currentId="somePropertyFromSomewhere"/> 
    </p:app_navigation> 
</html> 
+0

而最好使用prop:binding而不是$ {...},例如class =「prop:myDynCss」,http://tapestry.apache.org/component-parameters.html#ComponentParameters-dontUseSyntax – sody

+1

@sody我認爲你在prop:binding這裏是不正確的。對於組件上定義的參數綁定,這是正確的,而不是簡單地將值打印到類屬性的DOM屬性中。 – joostschouten

+0

@joostschouten對於遲到的答覆感到遺憾......並且感謝解決方案。我會試試看。與此同時,另一種方式與afterRender事件。 – OkieOth

1

我的解決方案使用的生命週期事件。如果有任何代表活動ID的標識(按照慣例)的鏈接標記爲活動。

我的最後一個組件模板...

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"> 
    <!-- convention: id == 'm' + numeric value for active entry --> 
    <t:pagelink page="mytest" id="m1000"> 
     I'm active 
    </t:pagelink> 

    <t:pagelink page="mytest2" id="m1001"> 
     I'm not active 
    </t:pagelink> 
</html> 

組件的java代碼...

public class TestLink { 
    @Parameter(required=true) 
    private int activeId; 

    // ... looking for a link with the active id ... 
    void afterRender(final MarkupWriter writer) { 
     // works only if the id follows the right convention :-D 
     String activeElemId="m"+activeId; // <-- 
     Element activeLink=writer.getDocument().getElementById(activeElemId); 
     if (activeLink!=null) 
      activeLink.addClassName("active"); 
    }  
} 

,其中包括組件的代碼...

<html t:type="layout" title="Test" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" 
     xmlns:p="tapestry:parameter"> 
    <p:app_navigation> 
     <t:testLink activeId="1000"/> 
    </p:app_navigation> 
</html>