Play Framework: how to change the active class when changing the route

I am using Play Framework 2 for Java and Bootstrap Helper in my project, and I want to apply the active class for the link in the sidebar. I use the side navigation bar for navigation, and by default, one link always has an active class when loading the page, so therefore at each moment of time only one link is highlighted as an active link, but how to change class="active" on the route or change the link, is there any way to check the route path - our html scala template file.

Here is my sidebar navigation code.

 <ul class="nav nav-list"> <li class="active"><a href="/menu1">Menu1</a></li> <li><a href="/menu2">Menu2</a></li> <li><a href="/menu3">Menu3</a></li> </ul> 

Here is my routes file

 GET /menu1 com.demo.project.controllers.DemoController.menu1() GET /menu2 com.demo.project.controllers.DemoController.menu2() GET /menu3 com.demo.project.controllers.DemoController.menu3() 

Please give me some suggestion about this!

+6
source share
1 answer

Finally, I got a solution for my problem, I shared my solution with others. First I created a method inside my html scala template.

 @activeLink(currentPath:String) = @{ if(request.path.equals(currentPath)) "active" } 

Now we need to call the method above inside the class attribute of the anchor tag.

 <ul class="nav nav-list"> <li class="@activeLink("/menu1")"><a href="/menu1">Menu1</a></li> <li class="@activeLink("/menu2")"><a href="/menu2">Menu2</a></li> <li class="@activeLink("/menu3")"><a href="/menu3">Menu3</a></li> </ul> 

Here is my final decision. If someone has a different solution or approaches differently, so please share them as well.

+7
source

All Articles