How to pass variables as parameters to #Url.Action ?
I populate a Menu control using #Url.Action.
List:
- childitem (it's populated from stored procedure)
Fields:
- TX_MENU: "My menu item"
- TX_ACTION: "Index"
- TX_CONTROLLER: "Buzon"
I'd like to populate my Menu control assigning
href=#Url.Action("SomeAction", "SomeController")
How could I pass variables as parameters with #.
I tried this code, but failed:
<ul>
#foreach (var itemchild in childitem)
{
<li class='last'><a href='#Url.Action(#itemchild.TX_ACTION, #itemchild.TX_CONTROLLER)'>#itemchild.TX_MENU</a>
</li>
}
</ul>
#itemchild.TX_ACTION and #itemchild.TX_CONTROLLER are not recognized as variables inside #Url.Action.
You should white it like this:
<ul>
#foreach (var itemchild in childitem)
{
<li class='last'><a href='#Url.Action(itemchild.TX_ACTION, itemchild.TX_CONTROLLER)'>#itemchild.TX_MENU</a>
</li>
}
</ul>
Don't escape your variables with # symbol when you in Url.Action() helper
Related
In a Grails 3.3 application, I'm trying to create a menu configuration and I'd like to use application.groovy for that.
application.groovy:
mainmenuconfig {
menuitem_1 {
id = 'menuitem-1'
name='Home'
sub=null
}
menuitem_2 {
id ='menuitem-2'
name='Stammdaten'
sub = submenuitem_2 {
menuitem_2_1 {
id = 'menuitem-2-1'
name ='Stamm-A'
sub=null
}
}}
}
Retrieving the config via grailsApplication.config.get('mainmenuconfig') would give me the following:
[menuitem_1:[id:menuitem-1, name:Home, sub:null], menuitem_2:[id:menuitem-2, name:Stammdaten, submenuitem_2:[menuitem_2_1:[id:menuitem-2-1, name:Stamm-A]], sub:null]]
If I look at getClass() it says it's a org.grails.config.NavigableMap
Now, for my understanding, generating an <UL> ... <LI> tree should be done inside the View layer. For iterating through that structure, I will need recursion, because it can be n levels deep.
If I'm looking from main.gsp, I know the spot where I want to insert the menu tree, but how do I bring the data there and where do I do that recursion? Will I need a menu controller that is being called from the GSP? IMHO the GSP shouldn't do such calls. And on the other side, no controller should generate <UL> ... <LI> trees. I need the glue piece.
This sounds like a good use for an interceptor. You can have it add to your model after every action is finished. Then your view will have access to menu in its model and can build out the menu. It's fine to do recursion in your view if it's for display purposes.
class NavigationMenuInterceptor {
NavigationMenuInterceptor() {
matchAll()
}
boolean after() {
model.menu = grailsApplication.config.getProperty('mainmenuconfig', Map)
true
}
}
To render your menu, you can use a template that recursively renders itself. Depending on your desired HTML output it might be something like below.
In the main body:
<ul>
<g:each in="${menu}" var="menuitem">
<g:render template="menuitem" model="[menuitem: menuitem]"/>
</g:each>
</ul>
In your template _menuitem.gsp:
<li id="${menuitem.id}">
${menuitem.name}
</li>
<g:if test="${menuitem.sub}">
<li>
<ul>
<g:render template="menuitem" model="[menuitem: menuitem.sub]"/>
</ul>
</li>
</g:if>
I've got a partial view generating a list of other partial views, and when I submit to controller, I only get the data I want from the last added item. I've obviously done something silly, but I cant see what.
#for (int i = 0; i < Model.Listofthings.Count(); i++)
{
<ul>
<li>
#{Html.RenderPartial("Thing", Model.Listofthings[i]);}
</li>
</ul>
}
And inside the Thing partial we have:
#model FancyObject
<script>
$(function () {
$('a#selectUser').click(function () {
$('form#selectUserForm').submit();
});
});
< link rel="stylesheet" href="~/Content/Site.css" />
< form id="selectUserForm" method="post" action="#Url.Action("SelectUser", "Home")">
#Html.HiddenFor(x => x.SomethingImportant)
<a href="#" id="selectUser" class="HyperLink1">
<i class="fa fa-female"></i>
<span class="nav-text">
#Model.AProperty
</span>
</a>
Now when I submit the form, I only get the "SomethingImportant" from the last item created by the above. What am I doing wrong?
You are getting the last item because the hidden field in your Thing partial view get the same name on every foreach iteration.
You can either grab a collection of all hidden values in your controller action by declaring parameter as a list or any collection.
public ActionResult GetMyValues(List<string> SomethingImportant)
Or you can differentiate name of hidden field on every iteration and get them separately in your controller.
In an ng-repeat I want to add a hide-xs attribute on an element based on the current scope.
How can I do that?
I basically want to do something like (this obviously don't work):
<li ng-repeat="item in items" hide-xs="{{ item.showAlways ? 'false' : 'true' }}">
{{item.title}}
</li>
Edit
I ended up doing this (as suggested by DieuNQ) but if anybody know how to do it using directive and not class I would take it
<li ng-repeat="item in items" ng-class="{'hide-xs': !item.showAlways }}">
{{item.title}}
</li>
hide-xs not works like that (it does not depend on true or false). It just add class to your tag. Try this:
In your html:
<li ng-repeat="item in items" ng-model="item.showAlways" ng-class="someClass">
...
</li>
In your controller:
$scope.item.showAlways ? $scope.someClass == '' : $scope.someClass == 'hide-xs';
I am building a sidebar navigation dynamically and I have the following issue:
Option 1 // add class active
Option 1.1 // add class active
Option 1.1.1 // add class active
Option 1.1.1.1 // add class active
option 1.1.1.1.1 // selected link id = 7 (this is guid in real app)
Option 1.1.2
Option 1.2
Option 2
Option 3
Basically the navigation is in the following format:
<ul>
<li>
link
</li>
<li>
link
<ul>
<li>
link
</li>
</ul>
</li>
</ul>
And here is what I try to do recursively.
#BuildNavigation(Model)
#helper BuildNavigation(IEnumerable<Menu> menu)
{
foreach (Menu item in menu)
{
<li>
#item.Title
#if (item.Items.Any())
{
<ul class="nav sub-menu">
#BuildNavigation(item.Items)
</ul>
}
</li>
}
}
This works fine, the sidebar is built. But how can I add the active classes to the <li> based on the current selection?
I can add a property to the Menu class bool IsActive and then within the view I can check if that menu is active but how can I add the active class to all parents <li> of that selected link?
I have an html template that filters a list by the column property of the objects of that list like so:
<ul>
<li card-view
card-id="state.card"
ng-repeat="state in ctrl.game.states | filter:{column:'backlog'} "
ng-include="cardview.html">
</li>
</ul>
If I modify the column property in one of the elements of that list, the display does not update.
How can I make that happen?
Here's one option that uses an imaginary placeholder tag and avoids the |filter replacing it with an ng-if, but I hope someone has a better answer than this one.
<ul>
<xx ng-repeat="state in ctrl.game.states">
<li card-view
card-id="state.card"
ng-if="state.column == 'backlog'"
ng-include="cardview.html">
</li>
</xx>
</ul>
Doing the ng-if and ng-repeat on the same element didn't work.