While working on jstl tags I came across foreach loop:
<c:forEach items="#{data.steps}" var="item" varStatus="status">
<li>#{item}</li>
</c:forEach>
How can I replace the static '1' (status.index == '1') with an variable? Should look like: status.index == '1' == #{data.step}?
Let say you want to replace constant '1' with a variable name 'step' existing in page/request/session/application scope. Here is the code:
<a href="" class='${status.index == step ? "current" : ""}'>
If step is a property of a bean 'beanA' existing page/request/session/application scope scope. You can code like this
<a href="" class='${status.index == beanA.step ? "current" : ""}'>
// beanA has a method getStep().
Related
if array data is
String sampdata[]= new String [22];
sampdata[0] ="a";,sampdata[1] ="b";
sampdata[2] ="c";,sampdata[3] ="d";
sampdata[4] ="e";,sampdata[5] ="f";
sampdata[6] ="g";,sampdata[7] ="h";
sampdata[8] ="i";,sampdata[9] ="j";
sampdata[10] ="k";,sampdata[11] ="l";
sampdata[12] ="m";,sampdata[13] ="n";sampdata[14] ="o";
sampdata[15] ="m";,sampdata[16] ="n";sampdata[174] ="o";
sampdata[18] ="m";,sampdata[19] ="n";sampdata[20] ="o";
sampdata[21] ="m";
and jsp use jstl foreach
<c:forEach var="list" items="${sampdata" begin="0" end="9" step="2" varStatus="status">
<tr>
<td>${list}</td> //a
<td>${list}</td> //td : b
<td></td>
</tr>
</c:forEach>
i mean
check this java array
for (int i=0 ; i<9;i +=2){
System.out.println(i)
System.out.println(i+1) <- i want like this.
}
i want "td:b" view b.
how to fix that?
"foreach" can use like that?
Check documentation on loop scoped variable with nested visibility that
lets authors use the status object to obtain information about the iteration range, step, and current object.
<c:forEach items="${sampdata}" step="2" varStatus="loop">
<c:out value="${sampdata[loop.index]}"/>
<c:out value="${sampdata[loop.index+1]}"/>
</c:forEach>
I need to output html where the values depend on what kind of object I have (I'm transitioning between DB representations). Right now I have my logic in a block of g:if expressions. It's relatively difficult to read and debug.
<g:if test="${o.isKindA}">
<g:set var="x" value="${...}" />
<g:set var="y" value="${...}" />
...
</g:if>
<g:else>
<g:set var="x" value="${...}" />
<g:set var="y" value="${...}" />
...
</g:else>
I am particularly not interested in adding these values X and Y as methods of o. What I'd like to do is set them using a view helper, which I understand == "tag lib" in grails:
// Tag Helper
//
class AmazingTagLib {
def valueXFor = { attrs -> o.isKindA? 1 : 2 }
...
}
// The previous GSP, rewritten
//
...
<g:set var="x" value="${ valueXFor(o) }" />
This is failing, however. When I attempt to use x, it is bound to an empty StreamCharBuffer, presumably because I didn't attach anything to out in the implementation of valueXFor. It should have had the integer returned value of my helper.
How can I use such functional helper methods in my view?
The issue is that by default Tag Libraries are used to render to out. In this case you are looking to return an object/value from your method and you need to tell the TagLib that this method is different than the standard behavior. Adding the following will do the trick:
class AmazingTagLib {
static returnObjectForTags = ['valueXFor']
def valueXFor = { attrs -> o.isKindA? 1 : 2 }
...
}
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")">
Can Anyone help me please.
My intention is to iterate over the two 2Dimentional arrays
Org_Positions_IdTitle which contains a)the ids of raised positions and b)their Titles.
Org_Apps which contains a)student Ids and b)ids of raised positions.
So I want whenever the position Ids in the two arrays are checked equal to display the Title of the position. Thanks a lot for your help guys.
ResultSet ResSet_Org_Positions_IdTitle = OrgInfo.Get_Organisation_Positions_IdTitle(username,password);
ResultSet ResSet_Org_Apps = OrgInfo.Get_Organisation_Apps(username,password);
int i=0;
while(ResSet_Org_Positions_IdTitle.next()){
Org_Positions_IdTitle[i][0] = new String(String.valueOf(ResSet_Org_Positions_IdTitle.getInt("P_Ps_Id")));
Org_Positions_IdTitle[i][1]= new String(ResSet_Org_Positions_IdTitle.getString("Title")); i++;
Org_Positions_IdTitle[i][0]= new String("Last");
//System.out.println(i+":"+Org_Positions_IdTitle[i-1][j-1]+Org_Positions_IdTitle[i-1][j]);
}
int m=0; //Org_Apps A_St_Id A_Ps_Id
while(ResSet_Org_Apps.next()){
Org_Apps[m][0] = new String(String.valueOf(ResSet_Org_Apps.getInt("A_St_Id")));
Org_Apps[m][1]= new String(ResSet_Org_Apps.getString("A_Ps_Id")); m++;
Org_Apps[m][0]= new String("Last");
//System.out.println(i+":"+Org_Positions_IdTitle[i-1][j-1]+Org_Positions_IdTitle[i-1][j]);
}
session.put("Org_Positions_IdTitle", getOrg_Positions_IdTitle());
session.put("Org_Apps", getOrg_Apps());
<s:iterator value="Org_Positions_IdTitle" id="P_Ps_Id" >
<s:iterator value="Org_Apps" id="A_Ps_Id" >
<s:if test="%{#P_Ps_Id==#A_Ps_Id} ">
<s:property value="Title" />
</s:if>
</s:iterator>
</s:iterator>
So it should look something like that:
<s:iterator value="firstArray" var="arr1">
<s:iterator value="secondArray" var="arr2">
<s:if test="#arr1[0] == #arr2[1]">
<s:property value="#arr1[1]" />
</s:if>
</s:iterator>
</s:iterator>
Don't forget that you need setters/getters for your arrays in action class.
BTW id attribute in Struts2 tags was deprecated long time ago, use var instead.
Also consider changing your code to use maps instead of arrays. Then there is no need to iterate it on JSP.
Basically what I want is:
<g:fancyJoin in="${myList}" var="item" separator=", ">
<g:link controller="foo" action="bar" id="${item.id}">${item.label}</g:link>
</g:fancyJoin>
and for
def mylist = [[id:1, label:"first"], [id:2, label:"second"]]
it should output:
first, second
The key difference between this and the existing join tag is that I need it to basically do a collect and apply tags over the initial list before performing the join operation
You shouldn't do this in a GSP. Cluttering your view with loops and conditionals makes it hard to maintain the code and forces you to test with functional tests which are quite slow. If you do this in a taglib you clean up the view and testing is very easy.
You could define a custom tag, something like:
def eachJoin = {attrs, body ->
def values = attrs.remove('in')
def var = attrs.remove('var')
def status = attrs.remove('status')
def delimiter = attrs.remove('delimiter')
values.eachWithIndex {entry, i ->
out << body([
(var ?: 'it') : entry,
(status ?: 'i') : i
])
if(delimiter && (i < values.size() - 1)) {
out << delimiter
}
}
}
Usage:
<g:eachJoin in="${myList}" var="item" delimiter=", ">
<g:link controller="foo" action="bar" id="${item.id}">${item.label}</g:link>
</g:eachJoin>