2013-10-16 57 views
0

在grails中我有一個菜單。我試圖突出顯示活動頁面的菜單項:grails當前頁面亮點

link1 
link2 (show as bold if the controller action partially matches this gsp name) 
link3 

我有工作代碼,但它似乎會有更好的方法。

標籤庫工作正常:

def active = { attrs, body -> 
    for (page in attrs.check) { 
     if (params.action.startsWith(page)) { 
      out << "current" 
      break 
     } 
    } 
} 

此選項工作正常,但似乎羅嗦:

<li><a href="${createLink(action: 'contactInfo')}" title="Contact Info" class="<xyz:active check='${['contactInfo']}'/>">Contact Info</a></li> 
<li><a href="${createLink(action: 'aboutYouFamily')}" title="About You" class="<xyz:active check='${['aboutYouFamily', 'aboutYouBackground', 'aboutYouCharacteristics', 'aboutYouLifestyle', 'aboutYouApplicant2']}'/>">About You</a></li> 

這打擊了:

<g:link action='myProfile' class="${<xyz:active check='${['myControllerAction']}'/>}">My Profile</g:link> 

我不相信你可以通過一個taglib作爲參數g:鏈接

我也有多個GSPS /行動將導致,因爲它們是如何命名的鏈接是活躍的要求:

aboutYouLocation 
aboutYouBackground 
aboutYouEducation 

都讓這個鏈接激活一個:

About You 

我可以做一個部分匹配,但我也有一些行動/ gsps開始aboutYour(額外的R)導致我使用傳遞到我的taglib數組。

+0

可能重複[如何更改導航欄中選定項的類屬性](http://stackoverflow.com/questions/14146950/how-to-change-the-class-attribute-of-a -selected-item-in-the-navigation-bar) – Gregg

回答

2

有一個標準的方式與Platform Core plugin這樣做。它將爲您提供一個Navigation API

的grails-app/conf目錄/ AppNavigation.groovy

navigation = { 
    // Declare the "app" scope, used by default in tags 
    app { 
     contact(action: 'contactInfo') 
     about(action: 'aboutYouFamily') 
    } 
} 

*的grails-app /視圖/ _menu.gsp *(模板,你可以在你的佈局中使用或GSP的)

<nav:menu scope="app" id="navigation" /> 

您也可以爲您定製菜單生成的HTML,檢查custom item rendering

+0

這是一個很棒的插件。我希望我早些時候知道它。它清理了我的代碼很多。 – spock99

+0

是的,確實是一個很棒的插件:-)我認爲更多的grails插件/應用程序應該使用它,讓事情更加標準。 –