I'm struggling to get a g:each tag to work. What I'm passing to the view is a list of hashmaps (something like this [ [ : ] , [ : ] ] ).
Each hashmap is of the form [location: somelocation , artist: someartist].
The code is the following:
CONTROLLER
in the controller I'm passing the following:
[searchedResults : results.searchedResults]
VIEW
<g:each status="i" in="${searchedResults}" var="results">
if(results.location!=null){
var point${results.location.id} = new google.maps.LatLng(${results.location.lat}, ${results.location.lng});
var myMarkerOptions${results.location.id} = {
position: point${results.location.id},
map: map
};
var marker${results.location.id} = new google.maps.Marker(myMarkerOptions${results.location.id});
}
</g:each>
Any ideas why this wouldn't work?
Thanks!
GrailsGuy is right in that you can't write groovy code in the body of an each tag like that. But let me try and convert it to something for you, since it looks like your doing some javascript in there as well...I think all you need to fix is your if statement
<g:each status="i" in="${searchedResults}" var="results">
<g:if test="${results.location}">
//everything else seems like it would work, assuming your javascript
// code is accurate
</g:if>
</g:each>
Related
I have a list object which is bound to a UI5 List element. However the values are not showing. Please take a look at my code.
UI/xml Code:
<List id="statementList" headerText="Statements"
items="{ path: 'statementListModel>/' }">
<StandardListItem title="{importance}" description="{importance}"/>
</List>
Binding in JS:
var result = JSON.parse(aData.responseData);
that.getView().byId("statementList").setModel(new JSONModel(), "statementListModel");
that.getView().byId("statementList").getModel("statementListModel").setData(result.statementList);
The list object is built like this:
result= {
statementList = [
{
importance = "ASD",
...
},
{
importance = "BDS",
...
}
]
}
However it is just not showing the content. The list has the correct size so the binding somewhat works but the content binding does not work:
Thanks for any help!
You have to add the model name EVERYWHERE:
<StandardListItem title="{statementListModel>importance}" description="{statementListModel>importance}"/>
lets say i have this:
ArrayList maps = [ ]
Map map = [:]
my controller i did that:
List.each {
myList -> map = [key1:value1,key2:value2,key3:value3]
maps << map
}
return render ( template: "myTemplate" , model: [arrayList:maps])
I'm passing this arrayList of maps to my GSP and iterating through it so i assign the values of each map to elements.
i did something like this in my gsp.
<g:each in="${arrayList}" var="map">
<g:select from="${someList}" optionValue="${map.get('key1')}" optionKey="key"/>
<input type="text" id="textBox" value="${map.get('key2')}"/>
</g:each>
i am getting this error ! which says:
ERROR errors.GrailsExceptionResolver - MissingPropertyException occurred when processing request: [POST] .....
No such property: myValue for class: java.util.LinkedHashMap$Entry. Stacktrace follows:
groovy.lang.MissingPropertyException: No such property: myValue for class: java.util.LinkedHashMap$Entry
at Users_**_Projects_**_grails_app_views__myGsp_gsp.run(_myGsp.gsp:6)
at org.grails.plugins.web.rest.api.ControllersRestApi.render(ControllersRestApi.groovy:53)
at se.su.it.vfu.ConfigController$$EPLhPshc.myFunction(myController.groovy:428)
myGsp.gsp:6: is actually the "select" row provided in the gsp code
and 428 in my controller is the return render () row
myValue is actually a map value!
I am iterating through the arrayList and the first map is map1 looks like this
[key1: myValue , key2: otherValue , key3 : someOtherValue]
You have the following in your GSP:
<g:select from="${someList}" optionValue="${map.get('key1')}" optionKey="key"/>
That is going to be the problem. The value that you assign to optionValue should be the name of a property on the elements in someList. That property will be used when generating the "value" of the individual elements in the list. In your case it looks like map.get('key1') evaluates to myValue so the select tag is going to try and retrieve the value of the myValue property for each element in the list.
See http://grails.github.io/grails-doc/3.0.4/ref/Tags/select.html for more details.
I hope that helps.
I am trying to define a CSS class on a depending on a condition, in a razor view. Must be simple, but I am struggling with it.
Code so far is:
<tr class="#{if (id == 0) {#:selected} else {#:notselected}}">
Obviously missing something simple....
Thoughts appreciated....
Try this:
<tr class="#((id == 0) ? "selected" : "notselected")">
What I do in that kind of situations is
#{
string rowClass = id == 0 ? "selected" : "notselected";
}
<tr class="#rowClass">
Anyways, if you want that inline, you could use
<tr class="#(id == 0 ? "selected" : "notselected")">
Say I have a controller action like the following:
def someAction = {
if (someCondition) {
[foo: 1, bar: 2]
} else {
[foo2: 4, bar2: 6, baz2: 6]
}
}
In someAction.gsp I don't know what the keys of the model are. Is there some way that I can iterate over the keys and values of the model without knowing the key names?
All model attributes are available in the request object. You can iterate this object like this:
<g:each var="item" in="${request}">
${item.key} = ${item.value}<br/>
</g:each>
Note that the request object will hold all request attributes, a lot of information that you're probably not interested in.
Another way to accomplish what you want is putting all your model attributes in one map, like this:
if (someCondition) {
[result:[foo: 1, bar: 2]]
} else {
[result:[foo2: 4, bar2: 6, baz2: 6]]
}
This way you can isolate your attributes from other request attributes. In this case you'll have to iterate your model keys using the result map:
<g:each var="item" in="${result}">
Use pageScope:
<ul>
<g:each var="item" in="${pageScope.variables}">
<li>${item.key} = ${item.value}</li>
</g:each>
</ul>
But note that you won't be able to distinguish between the controller model values and those from the framework.
In a controller, I have populated a map that has a string as key and a list as value; in the gsp, I try to show them like this:
<g:each in="${sector}" var="entry" >
<br/>${entry.key}<br/>
<g:each in="${entry.value}" var="item" >
${item.name}<br/>
</g:each>
</g:each>
The problem is that item is considered as string, so I get the exception
Error 500: Error evaluating expression [item.name] on line [11]:
groovy.lang.MissingPropertyException: No such property: name for class:
java.lang.String
Any hints on how to fix it other than doing the find for the item explicitly in the gsp ?
I did the poc which works fine for me
class Item{
String name
}
List items = [new Item(name:"laptop" ,
new Item(name:"mouse")]
Map sector =[:]
sector.manager = items
sector.manager.each{item->
println item.name
}
Even If it doesn't work, Try declaring Map sector as
Map < String, List < Item > > sector =[:]